python读取excel导入word表格
2017-01-16 14:07
826 查看
参考文档:
openpyxl API
python-docx API
需要注意中文编码问题,特此记录。
openpyxl API
python-docx API
# -*- coding: utf-8 -*- from openpyxl import load_workbook from docx import Document import time import sys import os reload(sys) sys.setdefaultencoding('utf8') # 开始时间 startTime = time.time() # 读取excel2007文件 wb = load_workbook(filename=r'1.xlsx') # word文档 document = Document() document.add_heading('Document Title', 0) table = document.add_table(rows=1, cols=7) hdr_cells = table.rows[0].cells hdr_cells[0].text = u'测试用例编号' hdr_cells[1].text = u'测试用例名称' hdr_cells[2].text = u'测试用例标题' hdr_cells[3].text = u'预置条件' hdr_cells[4].text = u'操作步骤' hdr_cells[5].text = u'预期结果' hdr_cells[6].text = u'实际结果' # 取第三张表 sheetnames = wb.get_sheet_names() ws = wb.get_sheet_by_name(sheetnames[2]) i = 1 # 写入word for row in ws.rows: if type(row[2].value) is long: row_cells = table.add_row().cells row_cells[0].text = u'' + str(i) i = i + 1 row_cells[1].text = u'QYWEB_' + str(row[2].value).zfill(2) + u".py" row_cells[2].text = u'' + str(row[3].value) row_cells[3].text = u'' + str(row[6].value) row_cells[4].text = u'' + str(row[7].value) row_cells[5].text = u'' + str(row[8].value) row_cells[6].text = u'成功' document.add_page_break() document.save('demo.docx')
需要注意中文编码问题,特此记录。
相关文章推荐
- 使用VBA将Excel数据导入到Word的表格中
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- python读取excel文件中所有sheet表格:openpyxl模块(二)
- Python读取Excel表格,并同时画折线图和柱状图的方法
- 将 Excel 表格导入 Word
- python读取excel表格统计不同网段IP个数
- 将word中表格批量导入到Excel
- 用Python读取Word文档并写入Excel(二)
- 把html的表格导入到excel,word中
- 如何将word中的表格导入Excel?
- python读取excel表格——xlrd的使用
- 如何使用免费控件将word表格中的数据导入到excel中
- Python实现批量读取word中表格信息的方法
- 用Python读取Word文档并写入Excel(一)
- python文件操作(2)--分析扫描得到的日志文件把文件状态导入Excel表格
- [转] Windows下使用Python读取Excel表格数据
- java读取word表格导入数据库,支持doc、docx
- 如何使用免费控件将Word表格中的数据导入到Excel中
- Python 导入文件夹中所有excel并按列名读取插入数据库
- Java读取并导出Word中的表格(Excel),导出文件为Excel