批量生成每年度的周报文件-python实现

前提条件:

  1. 一个名为“template.docx”的word模板文档。

  1. 安装python及python-docx模块,这个必装,其余模块缺什么再装就行。

  1. 有问题随时评论我看到就会回复。

实现功能说明:


生成指定年份的 Word 周报文档。 生成的文档内容基于名为“template.docx”的模板文件。

对于指定年份中的每个星期,执行以下操作:
    计算一周的第一天和最后一天。
    将模板文件中开始和结束日期的占位符文本替换为每周的实际开始和结束日期。
    将模板中周数的占位符文本替换为生成文件的实际周数。
    文件名格式为《项目周报_YYYYMMDD-YYYYMMDD第x周》。 
    生成的周报文件将存储在与脚本相同的目录中。

代码


import calendar
import datetime
import os
import shutil
import tempfile
import re
import docx

def from_template(template_path):
    # Create a new temporary directory.
    temp_dir = tempfile.TemporaryDirectory()

    # Copy the template file to the temporary directory.
    shutil.copy(template_path, temp_dir.name)

    # Open the copied template file.
    document = docx.Document(os.path.join(temp_dir.name, template_path))

    # Return the document and the temporary directory.
    return document, temp_dir

# 设置年份
year = 2020

# 设置日期格式
date_format = "%Y年%m月%d日"
mun_date_format = "%Y%m%d"

# 遍历每一周
for week in range(1, 53):
    # 获取当前周的第一天
    first_day = datetime.datetime.strptime("%s-W%s-1" % (year, week), "%Y-W%W-%w")
    # 获取当前周的最后一天
    last_day = first_day + datetime.timedelta(days=6)

    # 转换为字符串(带年月)
    first_day_str = first_day.strftime(date_format)
    last_day_str = last_day.strftime(date_format)
    # 转换为字符串(不带年月)
    mun_first_day_str = first_day.strftime(mun_date_format)
    mun_last_day_str = last_day.strftime(mun_date_format)
    week_str = str(week)

    # 创建文件名
    filename = "项目周报_%s-%s第%s周.docx" % (
        mun_first_day_str,
        mun_last_day_str,
        week
    )

    # 使用模板文件创建新文档
    document, temp_dir = from_template("template.docx")

    # 在文档中追加文本
    #document.add_paragraph(filename)

    # 替换文档中的开始日期
    for paragraph in document.paragraphs:
        paragraph.text = re.sub(r"1975年01月01日", first_day_str, paragraph.text)
        #print(first_day_str)
    # 替换文档中的结束日期
    for paragraph in document.paragraphs:
        paragraph.text = re.sub(r"1975年02月02日", last_day_str, paragraph.text)
        #print(last_day_str)
    # 替换表格中的周数
    # 遍历每一张表格
    for table in document.tables:
        # 遍历表格的每一行
        for row in table.rows:
            # 遍历行的每一个单元格
            for cell in row.cells:
                # 遍历单元格的每一段落
                for paragraph in cell.paragraphs:
                    # 替换文本
                    paragraph.text = re.sub(r"第(\d+)周$", "第" + week_str + "周", paragraph.text)
    # 保存文档
    document.save(filename)

    # Close the temporary directory when you're done.
    temp_dir.cleanup()

模板文件(word文档):


智能化xxxxxx系统(一期)

项目周报

(1975年01月01日至1975年02月02日)

承建单位:xxx有限公司

项目名称

xxxx

项目经理

杜xx

项目周

第1周

工作完成情况

任务编号

工作内容

状态

参与人员

1

工作对接

2

项目管理

3

需求分析

系统设计

开发

测试

发布上线

下周工作计划

任务编号

工作内容

时间安排

1

工作对接

2

项目管理:

3

需求分析

系统设计

开发

测试

发布

问题与解决方法

序号

问题与描述

持续时间

解决方案

是否已解决

1

2

项目经理签字:杜xx

备注:


生成的周报文件效果

同时文件中的这两个部分的日期也会被替换掉

↓↓↓↓↓↓↓↓↓↓↓

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值