更改架构,不再基于上周的报告,基于指定模板生成
This commit is contained in:
parent
57b47241fc
commit
3d1939330f
@ -5,6 +5,8 @@
|
|||||||
>
|
>
|
||||||
- 若已经存在同名文档,则不执行
|
- 若已经存在同名文档,则不执行
|
||||||
- 添加 `auto_run_python.bat`,双击即可执行python脚本
|
- 添加 `auto_run_python.bat`,双击即可执行python脚本
|
||||||
|
- 支持跨年
|
||||||
|
- 支持
|
||||||
|
|
||||||
## 待添加功能
|
## 待添加功能
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
user_name: 冯博涵
|
user_name: 冯博涵
|
||||||
project_name: nb2337
|
project_name: nb2337
|
||||||
project_leader: 罗伟
|
project_leader: 罗伟
|
||||||
|
template_file_name: 2017WWXX周工作报告样本.doc
|
||||||
|
147
gen_word.py
147
gen_word.py
@ -14,14 +14,11 @@ import logging.config
|
|||||||
|
|
||||||
class WEEK_REPORT:
|
class WEEK_REPORT:
|
||||||
file_name = ""
|
file_name = ""
|
||||||
pre_file_name = ""
|
|
||||||
pre_str = ""
|
|
||||||
str = ""
|
str = ""
|
||||||
user_name = ""
|
user_name = ""
|
||||||
project_name = ""
|
project_name = ""
|
||||||
project_leader = ""
|
project_leader = ""
|
||||||
__word = ""
|
template_file_name = ""
|
||||||
__doc = ""
|
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
with open("config.yaml", "r", encoding="UTF-8") as f1:
|
with open("config.yaml", "r", encoding="UTF-8") as f1:
|
||||||
@ -31,80 +28,9 @@ class WEEK_REPORT:
|
|||||||
self.user_name = data["user_name"]
|
self.user_name = data["user_name"]
|
||||||
self.project_name = data["project_name"]
|
self.project_name = data["project_name"]
|
||||||
self.project_leader = data["project_leader"]
|
self.project_leader = data["project_leader"]
|
||||||
|
self.template_file_name = data["template_file_name"]
|
||||||
self.get_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):
|
def get_current_week(self, date=None):
|
||||||
if date:
|
if date:
|
||||||
duty_date = datetime.datetime.strptime(str(date), '%Y-%m-%d')
|
duty_date = datetime.datetime.strptime(str(date), '%Y-%m-%d')
|
||||||
@ -116,10 +42,73 @@ class WEEK_REPORT:
|
|||||||
monday -= one_day
|
monday -= one_day
|
||||||
while friday.weekday() != 4:
|
while friday.weekday() != 4:
|
||||||
friday += one_day
|
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")
|
self.str = datetime.datetime.strftime(monday, "%#m.%#d") + "~" + datetime.datetime.strftime(friday, "%#m.%#d")
|
||||||
logger.debug("str: {}".format(self.str))
|
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__":
|
if __name__ == "__main__":
|
||||||
with open("log_config.yaml", 'r') as f:
|
with open("log_config.yaml", 'r') as f:
|
||||||
@ -130,4 +119,6 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
week_report = WEEK_REPORT()
|
week_report = WEEK_REPORT()
|
||||||
week_report.copy_file()
|
week_report.copy_file()
|
||||||
week_report.update_file()
|
week_report.open_file()
|
||||||
|
week_report.edit_file()
|
||||||
|
week_report.doc.Save()
|
||||||
|
@ -6,7 +6,7 @@ formatters:
|
|||||||
handlers:
|
handlers:
|
||||||
console:
|
console:
|
||||||
class: logging.StreamHandler
|
class: logging.StreamHandler
|
||||||
level: DEBUG
|
level: INFO
|
||||||
formatter: simple
|
formatter: simple
|
||||||
file:
|
file:
|
||||||
class: logging.FileHandler
|
class: logging.FileHandler
|
||||||
|
Loading…
x
Reference in New Issue
Block a user