From 8012d3a07e24743330c43564bed15c41b6bdbafa Mon Sep 17 00:00:00 2001 From: fengbohan <1953356163@qq.com> Date: Wed, 16 Aug 2023 16:51:24 +0800 Subject: [PATCH] =?UTF-8?q?add=20func:=E5=A6=82=E6=9E=9C=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=EF=BC=8C=E5=88=99=E6=89=93=E5=BC=80=E6=96=87?= =?UTF-8?q?=E6=A1=A3=EF=BC=8C=E4=B8=8D=E6=89=A7=E8=A1=8C=E5=90=8E=E7=BB=AD?= =?UTF-8?q?=E5=A4=84=E7=90=86=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gen_word.py | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/gen_word.py b/gen_word.py index e3efe57..e8881f5 100644 --- a/gen_word.py +++ b/gen_word.py @@ -18,16 +18,27 @@ class WEEK_REPORT: __doc = "" def __init__(self): self.get_name() + def get_name(self): self.file_name = "{}WW{}周工作报告_冯博涵.doc".format(time.strftime('%Y'), time.strftime('%W')) self.pre_file_name = "{}WW{}周工作报告_冯博涵.doc".format(time.strftime('%Y'), str(int(time.strftime('%W'))-1)) print("pre file name: {}; \nnew file name: {};".format(self.pre_file_name, self.file_name)) if(os.path.isfile(self.file_name)==True): print("{} 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): s = self.word.Selection s.Find.Execute("{}WW{}".format(time.strftime('%Y'), str(int(time.strftime('%W'))-1)), @@ -38,25 +49,17 @@ class WEEK_REPORT: s.Find.Execute(self.pre_str, False, False, False, False, False, True, 1, False, self.str, 1) + def update_file(self): - self.word = Dispatch('Word.Application') - # 或者使用下面的方法,使用启动独立的进程: - # word = DispatchEx('Word.Application') - - # 如果不声明以下属性,运行的时候会显示的打开word - self.word.Visible = 1 # 0:后台运行 1:前台运行(可见) - self.word.DisplayAlerts = 0 # 不显示,不警告 - - self.doc = self.word.Documents.Open(os.getcwd() + "\\{}".format(self.file_name)) # 打开一个已有的word文档 - - self.edit_file() # 编辑文档 - - self.doc.Save() # 保存 + self.open_file() + self.edit_file() + self.doc.Save() # doc.SaveAs(os.getcwd() + "\\funOpenExistFile.docx") # 另存为 - # self.doc.Close() # 关闭 word 文档 - # self.word.Quit() # 关闭 office - # date: "2022-08-09" + 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') @@ -68,7 +71,6 @@ class WEEK_REPORT: self.pre_str = start_time.strftime("%#m.%#d") + "~" + end_time.strftime("%#m.%#d") print("pre_str: {}".format(self.pre_str)) - # date: "2022-08-09" def get_current_week(self,date=None): if date: duty_date = datetime.datetime.strptime(str(date), '%Y-%m-%d') @@ -80,9 +82,6 @@ class WEEK_REPORT: monday -= one_day while friday.weekday() != 4: friday += one_day - - # return monday, friday - # 返回时间字符串 # 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") print("str: {}".format(self.str))