Python读取txt内容写入xlsx格式的excel中
2017-08-09 16:28
1541 查看
Python处理03版xls的excel 需要用到xlrd、xlwt来读写,而处理07版xlsx的excel需要用到openpyxl,首先读取txt内容,写入xlsx中,然后在读取xlsx中的内容。
# -*- coding: utf-8 -*- """ Created on Wed Aug 9 16:03:01 2017 @author: weilian """ import openpyxl import codecs from openpyxl.utils import get_column_letter def txt_to_xlsx(filename,outfile): fr = codecs.open(filename,'r') wb = openpyxl.Workbook() ws = wb.active ws = wb.create_sheet() ws.title = 'Sheet1' row = 0 for line in fr: row +=1 line = line.strip() line = line.split('\t') col = 0 for j in range(len(line)): col +=1 #print (line[j]) ws.cell(column = col,row = row,value = line[j].format(get_column_letter(col))) wb.save(outfile) #读取xlsx内容 def read_xlsx(filename): #载入文件 wb = openpyxl.load_workbook(filename) #获取Sheet1工作表 ws = wb.get_sheet_by_name('Sheet1') #按行读取 for row in ws.rows: for cell in row: print (cell.value) #按列读 for col in ws.columns: for cell in col: print (cell.value) if __name__=='__main__': inputfileTxt = 'test.txt' outfileExcel = 'text_result.xlsx' txt_to_xlsx(inputfileTxt,outfileExcel) read_xlsx(outfileExcel)
相关文章推荐
- Python读取txt内容写入xls格式的excel中
- Python读取txt内容写入xls格式excel中的方法
- Java读取txt文本内容并写入Excel
- python 将百度搜索风云榜的关键内容提取并写入txt文件和读取验证
- python读取txt数据写入excel
- python将文件夹里所有的txt文件内容写入excel
- java读取excel(xlsx格式)内容为string
- 用python读取json格式内容并保存到excel中
- python 将txt内容写入excel
- 从Excel中读取内容写入txt文件中
- python读取 .txt 文本内容以及将程序执行结果写入txt文件
- java 读取excel,写入excel(只限于xlsx格式)
- python数据清理——将excel单元格的内容写入到txt
- java 从后台下载xlsx格式excel 发现不可读取内容的解决方案
- Python 读取EXCEL(XLS、CSV)写入txt文件
- 【学习笔记】python读取json内容转换成excel格式
- POI 3.8读取2003与2007格式EXCEL(xls、xlsx)
- python读取excel中表结构生成sql语句,存入txt文件
- 读取Excel数据和写入txt文件以及读取配置文件工具类
- POI读取加密的EXCEL(兼容XLS,XLSX格式)