add func:如果文档存在,则打开文档,不执行后续处理脚本

This commit is contained in:
fengbohan 2023-08-16 16:51:24 +08:00
parent 1ac889d31a
commit 8012d3a07e

View File

@ -18,16 +18,27 @@ class WEEK_REPORT:
__doc = "" __doc = ""
def __init__(self): def __init__(self):
self.get_name() self.get_name()
def get_name(self): def get_name(self):
self.file_name = "{}WW{}周工作报告_冯博涵.doc".format(time.strftime('%Y'), time.strftime('%W')) 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)) 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)) print("pre file name: {}; \nnew file name: {};".format(self.pre_file_name, self.file_name))
if(os.path.isfile(self.file_name)==True): if(os.path.isfile(self.file_name)==True):
print("{} is already exist.".format(self.file_name)) print("{} is already exist.".format(self.file_name))
self.open_file()
sys.exit(1) sys.exit(1)
def copy_file(self): def copy_file(self):
os.system('copy {} {}'.format(self.pre_file_name, self.file_name)) 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): def edit_file(self):
s = self.word.Selection s = self.word.Selection
s.Find.Execute("{}WW{}".format(time.strftime('%Y'), str(int(time.strftime('%W'))-1)), 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, s.Find.Execute(self.pre_str,
False, False, False, False, False, True, 1, False, False, False, False, False, False, True, 1, False,
self.str, 1) self.str, 1)
def update_file(self): def update_file(self):
self.word = Dispatch('Word.Application') self.open_file()
# 或者使用下面的方法,使用启动独立的进程: self.edit_file()
# word = DispatchEx('Word.Application') self.doc.Save()
# 如果不声明以下属性运行的时候会显示的打开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() # 保存
# doc.SaveAs(os.getcwd() + "\\funOpenExistFile.docx") # 另存为 # 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): def get_last_week(self, date=None):
if date: if date:
today = datetime.datetime.strptime(str(date), '%Y-%m-%d') 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") self.pre_str = start_time.strftime("%#m.%#d") + "~" + end_time.strftime("%#m.%#d")
print("pre_str: {}".format(self.pre_str)) print("pre_str: {}".format(self.pre_str))
# date: "2022-08-09"
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')
@ -80,9 +82,6 @@ class WEEK_REPORT:
monday -= one_day monday -= one_day
while friday.weekday() != 4: while friday.weekday() != 4:
friday += one_day friday += one_day
# return monday, friday
# 返回时间字符串
# return datetime.datetime.strftime(monday, "%Y-%m-%d"), datetime.datetime.strftime(friday, "%Y-%m-%d") # 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")
print("str: {}".format(self.str)) print("str: {}".format(self.str))