Python 用docx模块实现从word中读取表格信息写入另外一个word
#encoding:utf-8
'''
日期:2017.6.12
作者:静的学习日志
功能: 读取word1的表格信息,往word2中写入;转换表格形式。
'''
from docx import Document
doc1=Document(u'D:\Python27_Test\D1.docx')
doc2=Document(u'D:\Python27_Test\Test.docx')
tables = [table for table in doc1.tables];
for table in tables:
#标题
T1=table.rows[0].cells[0].text.encode('gb2312')
#版本号
T2=table.rows[1].cells[1].text.encode('gb2312')
#步骤
T3=table.rows[3].cells[0].text.encode('gb2312')
#期望结果
T4=table.rows[4].cells[0].text.encode('gb2312')
#需求编号
T5=table.rows[5].cells[1].text.encode('gb2312')
#最终结果
T6=table.rows[6].cells[1].text.encode('gb2312')
#执行时间
T7=table.rows[7].cells[1].text.encode('gb2312')
#执行者
T8=table.rows[8].cells[1].text.encode('gb2312')
#测试备注
T9=table.rows[9].cells[1].text.encode('gb2312')
list=[T1,T2,T3,T4,T5,T6,T7,T8,T9]
#在新的文档中创建表格
table1 = doc2.add_table(rows=1, cols=9)
hdr_cells = table1.rows[0].cells
for i in range(9):
hdr_cells[i].text =list[i].decode('gb2312')
doc2.save(u'Test.docx')
浙公网安备 33010602011771号