下面是周报模板复制/生成器代码:
## Weekly Report Generator ##
## By Alexander Ezharjan ##
### Configs ###
template_file = 'template.docx'
week_start = (2023, 6, 19)
week_end = (2023, 6, 25)
fin_date_tag = "number: YOUR_STAFF_ID)" # 落款时间标记
###############
import os
import datetime
from docx import Document
import shutil
num_copies = int(input("请输入复制文件的数量: "))
# 读取模板文件
template_doc = Document(template_file)
# 定义新的起止时间和间隔时间
start_date = datetime.date(*week_start)
end_date = datetime.date(*week_end)
delta = datetime.timedelta(days=7)
# 复制文件
for i in range(num_copies):
# 构造新文件名
new_file = os.path.splitext(template_file)[0] + '_' + str(i+1) + '.docx'
# 复制模板文件到新文件
# os.system('cp ' + template_file + ' ' + new_file) # on Linux
shutil.copy(template_file, new_file) # on Windows
# 打开新文件
new_doc = Document(new_file)
new_end_date = datetime.date.today()
# 修改时间范围
for paragraph in new_doc.paragraphs:
if "–" in paragraph.text:
date_range = paragraph.text.split('–')
old_start_date = datetime.datetime.strptime(date_range[0].strip(), "%Y.%m.%d").date()
old_end_date = datetime.datetime.strptime(date_range[1].strip(), "%Y.%m.%d").date()
new_start_date = start_date + i * delta
new_end_date = end_date + i * delta
# date_range[0] = new_start_date.strftime("%Y.%m.%d") # with zero ahead of Month/Day<10
date_range[0] = new_start_date.strftime("%Y.%#m.%e").strip()
date_range[1] = new_end_date.strftime("%Y.%#m.%e")
paragraph.text = "–".join(date_range)
# 修改撰写日
found_end = False
for paragraph in new_doc.paragraphs:
if found_end:
paragraph.text = new_end_date.strftime("%Y.%#m.%d")
paragraph.runs[-1].bold = True
break
if fin_date_tag in paragraph.text:
found_end = True
# 保存新文件
new_doc.save(new_file)
print("文件已复制完成!")
作者:艾孜尔江
标签:end,file,Duplicator,.%,datetime,date,Template,复制器,new From: https://www.cnblogs.com/ezhar/p/17488362.html