python 指定word生成
1.代码
代码如下(示例):
from docx import *
import pandas as pd
import re
def datetime():
b = []
a = list(pd.date_range(start='2022-12-23', end='2023-01-31').strftime("%Y-%m-%d"))
for i in range(len(a)):
b.append( re.sub("-", ".", str(a[i])) )
print(b)
# print(a)
# a=list(map(str,a))
# print(len(a))
return b
def dealrows(date):
document = Document()
table = document.add_table(40, 7, style="Table Grid")
for i in range(40):
if i == 0 :
heading_cells = table.rows[i].cells
heading_cells[0].text = '消毒时间'
heading_cells[1].text = '楼层'
heading_cells[2].text = '消毒剂'
heading_cells[3].text = '消毒方式'
heading_cells[4].text = '浓度'
heading_cells[5].text = '消毒员方式'
heading_cells[6].text = '监督员'
else:
heading_cells = table.rows[i].cells
heading_cells[0].text = date[i]
heading_cells[1].text = '生工系5楼'
heading_cells[2].text = '含氯消毒片'
heading_cells[3].text = '喷洒'
heading_cells[4].text = '500mg/L'
return document
def saveword(word1):
word1.save(r'C:\Users\tuxia\Desktop\Test1.docx')
return True
if __name__ == "__main__":
date = datetime()
word1 = dealrows(date)
word2 = saveword(word1)