125 lines
4.3 KiB
Python
125 lines
4.3 KiB
Python
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 = ""
|
||
|
||
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"]
|
||
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(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 = "、".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 = ''
|
||
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".join(["{}(?%)".format(i) for i in 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:
|
||
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()
|