week_report/gen_word.py

145 lines
5.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
import ruamel.yaml as Yaml
import logging.config
class WEEK_REPORT:
file_name = ""
str = ""
user_name = ""
project_name = ""
project_leader = ""
template_file_name = ""
week = ""
year = ""
def __init__(self):
with open("config.yaml", "r", encoding="UTF-8") as f1:
yaml = Yaml.YAML(typ='safe', pure=True)
data = yaml.load(f1)
logger.debug("config.yaml: {}".format(data))
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"]
now = datetime.datetime.now()
self.week = f"{now.isocalendar()[1]:02d}"
self.year = f"{now.isocalendar()[0]}"
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))
def get_name(self):
self.file_name = "{}WW{}周工作报告_{}.doc".format(self.year, self.week, 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(self.year, self.week)
# 日期
self.get_current_week()
table.Cell(Row=1, Column=4).Range.Text = self.str
# 所在项目
table.Cell(Row=2, Column=2).Range.Text = "".join(self.project_name)
# 项目负责人
table.Cell(Row=2, Column=4).Range.Text = "".join(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 = "\n".join(["{}:".format(i) for i in self.project_name])
table.Cell(Row=i, Column=3).Range.Text = "\n".join(["{}:".format(i) for i in self.project_name])
# table.Cell(Row=i, Column=2).Range.Font.Bold = True
# table.Cell(Row=i, Column=3).Range.Font.Bold = True
table.Cell(Row=i, Column=2).Range.Font.Name = "Times New Roman"
table.Cell(Row=i, Column=3).Range.Font.Name = "Times New Roman"
table.Cell(Row=i, Column=2).Range.Text = table.Cell(Row=i, Column=2).Range.Text.upper()
table.Cell(Row=i, Column=3).Range.Text = table.Cell(Row=i, Column=3).Range.Text.upper()
# 额外工作记录
str_tmp = "\n" + " ;\n".join([" {}:".format(i) for i in self.project_name]);
# table.Cell(Row=13, Column=1).Range.Text = "周一0h{0}\n周二0h{0}\n周三0h{0}\n周四0h{0}\n周五0h{0}\n周六0h{0}\n周日0h{0}\n".format(str_tmp)
table.Cell(Row=13, Column=1).Range.Text = "周一{0}\n周二{0}\n周三{0}\n周四{0}\n周五{0}\n周六0h{0}\n周日0h{0}\n".format(str_tmp)
# 工作完成情况
table.Cell(Row=15, Column=1).Range.Text = "\n".join(["{}(xx%)".format(i) for i in self.project_name]) + "\n其他(研究、学习、培训、交流、出差)\n"
#table.Cell(Row=15, Column=1).Range.Font.Bold = True
table.Cell(Row=15, Column=1).Range.Font.Name = "Times New Roman"
table.Cell(Row=15, Column=1).Range.Text = table.Cell(Row=15, Column=1).Range.Text.upper()
# 困难问题、经验教训及建议
table.Cell(Row=17, Column=1).Range.Text = ''
# 下周工作重点
table.Cell(Row=19, Column=1).Range.Text = "\n".join(["{}:".format(i) for i in self.project_name])
#table.Cell(Row=19, Column=1).Range.Font.Bold = True
table.Cell(Row=19, Column=1).Range.Font.Name = "Times New Roman"
table.Cell(Row=19, Column=1).Range.Text = table.Cell(Row=19, Column=1).Range.Text.upper()
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:
yaml = Yaml.YAML(typ='safe', pure=True)
config = yaml.load(f.read())
logging.config.dictConfig(config)
logger = logging.getLogger("file_logger")
week_report = WEEK_REPORT()
week_report.copy_file()
week_report.open_file()
week_report.edit_file()
week_report.doc.Save()