from openpyxl import load_workbook
from docx import Document
import datetime
from datetime import datetime as datet
wb = load_workbook("收集表.xlsx")
ws = wb['收集表']
from docx.shared import Inches, Cm
#读取表中某行数据
ll = []
for i in ws.columns:
# print(i[1].value)
ll.append(i[1].value)
print(ll)
doc = Document('模版.docx')
tables = doc.tables
table = tables[0]
table1 = tables[1]
table2 = tables[2]
table3 = tables[3]
table4 = tables[4]
a, b, c = datet.now().strftime("%Y-%m-%d %H:%M:%S").split()[0].split('-')
# print(a,b,c)
all_paragraphs = doc.paragraphs
for paragraphs in all_paragraphs:
for run in paragraphs.runs:
run.text = run.text.replace('2088', a)
run.text = run.text.replace('aqee', b)
run.text = run.text.replace('ad', c)
table.cell(0, 1).text = ll[4]
table.cell(0, 3).text = ll[6]
table.cell(0, 5).text = ll[15]
old1 = str(ll[10])
old2 = old1.split()
table.cell(1, 1).text = old2[0]
table.cell(1, 3).text = ll[12]
table.cell(1, 5).text = ll[13]
table.cell(2, 1).text = ll[5]
table.cell(3, 1).text = ll[9]
table.cell(4, 1).text = ll[14]
#
table1.cell(0, 1).text = ll[17]
if ll[19] != None:
table1.cell(0, 3).text = ll[19] #
table1.cell(0, 5).text = ll[18]
table1.cell(1, 1).text = ll[20]
if ll[22] != None:
table1.cell(1, 3).text = ll[22] #
table1.cell(1, 5).text = ll[21]
#
table2.cell(0, 1).text = ll[24]
table2.cell(2, 0).text = ll[25]
table2.cell(2, 1).text = ll[26]
table2.cell(2, 2).text = ll[27]
table2.cell(2, 3).text = ll[28]
table2.cell(2, 4).text = ll[29]
table2.cell(2, 5).text = ll[30]
table2.cell(2, 6).text = ll[31]
table2.cell(2, 7).text = ll[32]
table2.cell(2, 8).text = ll[33]
if ll[34] != None:
table2.cell(2, 9).text = ll[34]
table2.cell(2, 10).text = ll[35]
table3.cell(0, 1).text = ll[9]
table3.cell(1, 1).text = ll[5]
# run = table.cell(1, 6).paragraphs[0].add_run()
# picture = run.add_picture(f'img/{list[0]}.jpg')
# picture.height = Cm(5)
# picture.width = Cm(3)
table4.cell(0, 1).text = ll[36]
table4.cell(1, 1).text = ll[8]
doc.save(f'print/{ll[4]}.docx')
print('已生成文档')
标签:table2,word,python,text,ll,excel,cell,run,table
From: https://www.cnblogs.com/wdyjx/p/17514033.html