我正在开发一个tkinter应用程序提出问题的项目,并根据我的输入,通过用我的条目替换占位符文本来编辑word文件。我遇到一个错误,其中'Document'没有属性'write'。
如果我在文本中搜索除占位符以外的任何内容,则脚本可以正常工作,并且我会看到窗口。但是,如果我的占位符在文本中,则解释器会遇到我认为必须写入的问题。我最近使用类重新完成了我的程序,然后我就让它工作了。我错过了 init 功能吗?
classes.py
from tkinter import *
from docx import Document
import os
os.chdir("\\Users\\Nick\Desktop")
doc = Document('TemplateTest.docx')
paragraphs = doc.paragraphs
class WebWindow:
def __init__(self, master):
self.master = master
master.title("NAV Report")
self.label = Label(master, text="This is our first GUI!")
self.label.pack()
self.entry = Entry(master, text="Enter")
self.entry.pack()
self.close_button = Button(master, text="Close", command=master.quit)
self.close_button.pack()
self.submit = Button(master, text="Submit", command=self.WebsiteChange())
self.submit.pack()
def WebsiteChange(self):
for paragraph in doc.paragraphs:
if '^Websitename' in paragraph.text:
paragraph.text = self.entry.get()
print(paragraph.text)
doc.save(doc)
pass
print ("test")
root = Tk()
WebWindow(root)
root.mainloop()
完整跟踪错误
Traceback (most recent call last):
File "C:/Users/Nick/PycharmProjects/classes/class.py", line 36, in <module>
WebWindow(root)
File "C:/Users/Nick/PycharmProjects/classes/class.py", line 22, in __init__
self.submit = Button(master, text="Submit", command=self.WebsiteChange())
File "C:/Users/Nick/PycharmProjects/classes/class.py", line 31, in WebsiteChange
doc.save(doc)
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\site-packages\docx\document.py", line 142, in save
self._part.save(path_or_stream)
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\site-packages\docx\parts\document.py", line 129, in save
self.package.save(path_or_stream)
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\site-packages\docx\opc\package.py", line 160, in save
PackageWriter.write(pkg_file, self.rels, self.parts)
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\site-packages\docx\opc\pkgwriter.py", line 33, in write
PackageWriter._write_content_types_stream(phys_writer, parts)
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\site-packages\docx\opc\pkgwriter.py", line 45, in _write_content_types_stream
phys_writer.write(CONTENT_TYPES_URI, cti.blob)
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\site-packages\docx\opc\phys_pkg.py", line 155, in write
self._zipf.writestr(pack_uri.membername, blob)
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\zipfile.py", line 1581, in writestr
self.fp.write(zinfo.FileHeader(zip64))
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\zipfile.py", line 680, in write
n = self.fp.write(data)
AttributeError: 'Document' object has no attribute 'write'
Exception ignored in: <bound method ZipFile.__del__ of <zipfile.ZipFile [closed]>>
Traceback (most recent call last):
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\zipfile.py", line 1595, in __del__
self.close()
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\zipfile.py", line 1608, in close
self._write_end_record()
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\zipfile.py", line 1711, in _write_end_record
self.fp.write(endrec)
File "C:\Users\Nick\AppData\Local\Programs\Python\Python35-32\lib\zipfile.py", line 680, in write
n = self.fp.write(data)
AttributeError: 'Document' object has no attribute 'write'
答案 0 :(得分:1)
save参数必须是文件名:
doc.save('TemplateTest.docx')