更改架构,不再基于上周的报告,基于指定模板生成

This commit is contained in:
fengbohan 2024-01-02 16:49:31 +08:00
parent 57b47241fc
commit 3d1939330f
4 changed files with 73 additions and 79 deletions

View File

@ -5,6 +5,8 @@
>
- 若已经存在同名文档,则不执行
- 添加 `auto_run_python.bat`双击即可执行python脚本
- 支持跨年
- 支持
## 待添加功能

View File

@ -1,3 +1,4 @@
user_name: 冯博涵
project_name: nb2337
project_leader: 罗伟
template_file_name: 2017WWXX周工作报告样本.doc

View File

@ -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()

View File

@ -6,7 +6,7 @@ formatters:
handlers:
console:
class: logging.StreamHandler
level: DEBUG
level: INFO
formatter: simple
file:
class: logging.FileHandler