week_report/gen_word.py

125 lines
4.3 KiB
Python
Raw Normal View History

2023-08-16 15:27:12 +08:00
import time
import datetime
from docx import Document
from docx.shared import Inches
import win32com
from win32com.client import Dispatch, constants
import os
import sys
2024-01-02 16:25:12 +08:00
import ruamel.yaml as Yaml
2023-08-17 11:08:58 +08:00
import logging.config
2023-08-16 15:27:12 +08:00
2024-01-02 16:25:12 +08:00
2023-08-16 15:27:12 +08:00
class WEEK_REPORT:
file_name = ""
str = ""
2023-08-17 09:53:08 +08:00
user_name = ""
2024-01-02 16:25:12 +08:00
project_name = ""
project_leader = ""
template_file_name = ""
2024-01-02 16:25:12 +08:00
2023-08-16 15:27:12 +08:00
def __init__(self):
2023-08-17 09:53:08 +08:00
with open("config.yaml", "r", encoding="UTF-8") as f1:
2024-01-02 16:25:12 +08:00
yaml = Yaml.YAML(typ='safe', pure=True)
data = yaml.load(f1)
2023-08-17 11:08:58 +08:00
logger.debug("config.yaml: {}".format(data))
2023-08-17 09:53:08 +08:00
self.user_name = data["user_name"]
2024-01-02 16:25:12 +08:00
self.project_name = data["project_name"]
self.project_leader = data["project_leader"]
self.template_file_name = data["template_file_name"]
2023-08-16 15:27:12 +08:00
self.get_name()
def get_current_week(self, date=None):
if date:
duty_date = datetime.datetime.strptime(str(date), '%Y-%m-%d')
monday, friday = duty_date, duty_date
else:
monday, friday = datetime.date.today(), datetime.date.today()
one_day = datetime.timedelta(days=1)
while monday.weekday() != 0:
monday -= one_day
while friday.weekday() != 4:
friday += one_day
self.str = datetime.datetime.strftime(monday, "%#m.%#d") + "~" + datetime.datetime.strftime(friday, "%#m.%#d")
logger.debug("str: {}".format(self.str))
2023-08-16 15:27:12 +08:00
def get_name(self):
2023-08-17 09:53:08 +08:00
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))
2024-01-02 16:25:12 +08:00
logger.info("new file name: {};".format(self.file_name))
if os.path.isfile(self.file_name):
2023-08-17 11:08:58 +08:00
logger.info("{} is already exist.".format(self.file_name))
self.open_file()
sys.exit(1)
2023-08-16 15:27:12 +08:00
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))
2023-08-16 15:27:12 +08:00
def edit_file(self):
logger.info("process: edit file ...")
2024-01-02 16:25:12 +08:00
table = self.doc.Tables(1)
# 工作周
table.Cell(Row=1, Column=2).Range.Text = "{}WW{}".format(time.strftime('%Y'), time.strftime('%W'))
# 日期
2023-08-16 15:27:12 +08:00
self.get_current_week()
2024-01-02 16:25:12 +08:00
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
2024-01-02 16:25:12 +08:00
# 删除上周工作内容
for i in range(7,12):
2024-01-02 16:25:12 +08:00
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 = ''
2024-01-02 16:25:12 +08:00
# 下周工作重点
table.Cell(Row=19, Column=1).Range.Text = ''
2024-01-02 16:25:12 +08:00
def close_file(self):
logger.info("process: copy file ...")
self.doc.Close() # 关闭 word 文档
self.word.Quit() # 关闭 office
2024-01-02 16:25:12 +08:00
2023-08-16 15:27:12 +08:00
if __name__ == "__main__":
2023-08-17 11:08:58 +08:00
with open("log_config.yaml", 'r') as f:
2024-01-02 16:25:12 +08:00
yaml = Yaml.YAML(typ='safe', pure=True)
config = yaml.load(f.read())
2023-08-17 11:08:58 +08:00
logging.config.dictConfig(config)
logger = logging.getLogger("file_logger")
2023-08-16 15:27:12 +08:00
week_report = WEEK_REPORT()
week_report.copy_file()
week_report.open_file()
week_report.edit_file()
week_report.doc.Save()