From 3d1939330f363a38f6ffd7b036a2ab16df26e78f Mon Sep 17 00:00:00 2001 From: fengbohan <1953356163@qq.com> Date: Tue, 2 Jan 2024 16:49:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=94=B9=E6=9E=B6=E6=9E=84=EF=BC=8C?= =?UTF-8?q?=E4=B8=8D=E5=86=8D=E5=9F=BA=E4=BA=8E=E4=B8=8A=E5=91=A8=E7=9A=84?= =?UTF-8?q?=E6=8A=A5=E5=91=8A=EF=BC=8C=E5=9F=BA=E4=BA=8E=E6=8C=87=E5=AE=9A?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=E7=94=9F=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ReadMe.md | 2 + config.yaml | 1 + gen_word.py | 147 +++++++++++++++++++++++------------------------- log_config.yaml | 2 +- 4 files changed, 73 insertions(+), 79 deletions(-) diff --git a/ReadMe.md b/ReadMe.md index 0ddb401..6069088 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -5,6 +5,8 @@ > - 若已经存在同名文档,则不执行 - 添加 `auto_run_python.bat`,双击即可执行python脚本 +- 支持跨年 +- 支持 ## 待添加功能 diff --git a/config.yaml b/config.yaml index 9c65239..bb62221 100644 --- a/config.yaml +++ b/config.yaml @@ -1,3 +1,4 @@ user_name: 冯博涵 project_name: nb2337 project_leader: 罗伟 +template_file_name: 2017WWXX周工作报告样本.doc diff --git a/gen_word.py b/gen_word.py index 2252939..7a343ee 100644 --- a/gen_word.py +++ b/gen_word.py @@ -14,14 +14,11 @@ import logging.config class WEEK_REPORT: file_name = "" - pre_file_name = "" - pre_str = "" str = "" user_name = "" project_name = "" project_leader = "" - __word = "" - __doc = "" + template_file_name = "" def __init__(self): with open("config.yaml", "r", encoding="UTF-8") as f1: @@ -31,80 +28,9 @@ class WEEK_REPORT: self.user_name = data["user_name"] self.project_name = data["project_name"] self.project_leader = data["project_leader"] + self.template_file_name = data["template_file_name"] self.get_name() - def get_name(self): - self.file_name = "{}WW{}周工作报告_{}.doc".format(time.strftime('%Y'), time.strftime('%W'), self.user_name) - if time.strftime('%W') == "01": - self.pre_file_name = "{}WW{}周工作报告_{}.doc".format(str(int(time.strftime('%Y')) - 1), str(52), - self.user_name) - else: - self.pre_file_name = "{}WW{}周工作报告_{}.doc".format(time.strftime('%Y'), - str(int(time.strftime('%W')) - 1), self.user_name) - logger.info("pre file name: {}".format(self.pre_file_name)) - logger.info("new file name: {};".format(self.file_name)) - if os.path.isfile(self.file_name): - logger.info("{} is already exist.".format(self.file_name)) - self.open_file() - sys.exit(1) - - def copy_file(self): - os.system('copy {} {}'.format(self.pre_file_name, self.file_name)) - - def open_file(self): - self.word = Dispatch('Word.Application') - # 或者使用下面的方法,使用启动独立的进程: - # word = DispatchEx('Word.Application') - self.word.Visible = 1 # 0:后台运行 1:前台运行(可见) - self.word.DisplayAlerts = 0 # 不显示,不警告 - self.doc = self.word.Documents.Open(os.getcwd() + "\\{}".format(self.file_name)) - - def edit_file(self): - table = self.doc.Tables(1) - - # 工作周 - table.Cell(Row=1, Column=2).Range.Text = "{}WW{}".format(time.strftime('%Y'), time.strftime('%W')) - - # 日期 - self.get_last_week() - self.get_current_week() - table.Cell(Row=1, Column=4).Range.Text = self.str - - # 所在项目 - table.Cell(Row=2, Column=2).Range.Text = self.project_name - - # 项目负责人 - table.Cell(Row=2, Column=4).Range.Text = self.project_leader - - # 删除上周工作内容 - for i in range(7,12): - logger.info("process {} row ...".format(i)) - table.Cell(Row=i, Column=2).Range.Text = '' - table.Cell(Row=i, Column=3).Range.Text = '' - table.Cell(Row=13, Column=1).Range.Text = '' - table.Cell(Row=15, Column=1).Range.Text = '项目工作:\n' - - def update_file(self): - self.open_file() - self.edit_file() - self.doc.Save() - # doc.SaveAs(os.getcwd() + "\\funOpenExistFile.docx") # 另存为 - - def close_file(self): - self.doc.Close() # 关闭 word 文档 - self.word.Quit() # 关闭 office - - def get_last_week(self, date=None): - if date: - today = datetime.datetime.strptime(str(date), '%Y-%m-%d') - else: - today = datetime.datetime.today() - end_time = today - datetime.timedelta(days=today.isoweekday()) - datetime.timedelta(days=2) - start_time = end_time - datetime.timedelta(days=4) - # return start_time.strftime("%Y-%m-%d"), end_time.strftime("%Y-%m-%d") - self.pre_str = start_time.strftime("%#m.%#d") + "~" + end_time.strftime("%#m.%#d") - logger.debug("pre_str: {}".format(self.pre_str)) - def get_current_week(self, date=None): if date: duty_date = datetime.datetime.strptime(str(date), '%Y-%m-%d') @@ -116,10 +42,73 @@ class WEEK_REPORT: monday -= one_day while friday.weekday() != 4: friday += one_day - # return datetime.datetime.strftime(monday, "%Y-%m-%d"), datetime.datetime.strftime(friday, "%Y-%m-%d") self.str = datetime.datetime.strftime(monday, "%#m.%#d") + "~" + datetime.datetime.strftime(friday, "%#m.%#d") logger.debug("str: {}".format(self.str)) + def get_name(self): + self.file_name = "{}WW{}周工作报告_{}.doc".format(time.strftime('%Y'), time.strftime('%W'), self.user_name) + logger.info("template_file_name: {}".format(self.template_file_name)) + logger.info("new file name: {};".format(self.file_name)) + if os.path.isfile(self.file_name): + logger.info("{} is already exist.".format(self.file_name)) + self.open_file() + sys.exit(1) + + def copy_file(self): + logger.info("process: copy file ...") + os.system('copy {} {}'.format(self.template_file_name, self.file_name)) + + def open_file(self): + logger.info("process: open file ...") + self.word = Dispatch('Word.Application') + # 或者使用下面的方法,使用启动独立的进程: + # word = DispatchEx('Word.Application') + self.word.Visible = 1 # 0:后台运行 1:前台运行(可见) + self.word.DisplayAlerts = 0 # 不显示,不警告 + self.doc = self.word.Documents.Open(os.getcwd() + "\\{}".format(self.file_name)) + + def edit_file(self): + logger.info("process: edit file ...") + table = self.doc.Tables(1) + + # 工作周 + table.Cell(Row=1, Column=2).Range.Text = "{}WW{}".format(time.strftime('%Y'), time.strftime('%W')) + + # 日期 + self.get_current_week() + table.Cell(Row=1, Column=4).Range.Text = self.str + + # 所在项目 + table.Cell(Row=2, Column=2).Range.Text = self.project_name + + # 项目负责人 + table.Cell(Row=2, Column=4).Range.Text = self.project_leader + + # 报告人 + table.Cell(Row=3, Column=2).Range.Text = self.user_name + + # 删除上周工作内容 + for i in range(7,12): + + table.Cell(Row=i, Column=2).Range.Text = '' + table.Cell(Row=i, Column=3).Range.Text = '' + + # 额外工作记录 + table.Cell(Row=13, Column=1).Range.Text = '1,周一:' + + # 工作完成情况 + table.Cell(Row=15, Column=1).Range.Text = "项目工作:\n{}\n1,\n其他(非项目工作)\n\n".format(self.project_name) + + # 困难问题、经验教训及建议 + table.Cell(Row=17, Column=1).Range.Text = '' + + # 下周工作重点 + table.Cell(Row=19, Column=1).Range.Text = '' + + def close_file(self): + logger.info("process: copy file ...") + self.doc.Close() # 关闭 word 文档 + self.word.Quit() # 关闭 office if __name__ == "__main__": with open("log_config.yaml", 'r') as f: @@ -130,4 +119,6 @@ if __name__ == "__main__": week_report = WEEK_REPORT() week_report.copy_file() - week_report.update_file() + week_report.open_file() + week_report.edit_file() + week_report.doc.Save() diff --git a/log_config.yaml b/log_config.yaml index 190ca23..b59a4f0 100644 --- a/log_config.yaml +++ b/log_config.yaml @@ -6,7 +6,7 @@ formatters: handlers: console: class: logging.StreamHandler - level: DEBUG + level: INFO formatter: simple file: class: logging.FileHandler