如何用python实现对word的编辑

def openfile(self):
  p1=END
  oname=askopenfilename(filetypes=[("文本文档","*.txt")])
  if oname:
   for line in fileinput.input(oname):
    self.st.insert(p1,line)
   self.t.title(oname)
def savefile(self):
  sname=asksaveasfilename()
  if sname:
   ofp=open(sname,"w")
   ofp.write(self.st.get(1.0,END))
   ofp.flush()
   ofp.close()
   self.t.title(sname)

这是菜单当中的打开命令的代码,如何使其同时实现对txt和doc文件的阅读,或者改为对doc的阅读

阅读 14.5k
2 个回答

采用win32com,操作word,根据word VBA文档来操作,基本上可以用代码实现,word的功能。

编辑还是比较麻烦的。docx的话,可以使用python-docx来写word。 要是读的话,这个似乎也可以。不过我一般都转成txt,再处理txt

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题