Python Docx 和 Tkinter,'Document' 没有属性 'write'
Python Docx and Tkinter, 'Document' has no attribute 'write'
我正在做一个项目,其中 tkinter 应用程序提出问题,并根据我的输入,通过用我的条目替换占位符文本来编辑 word 文件。我遇到了 'Document' 没有属性 'write'.
的错误
如果我在我的文本中搜索除占位符以外的任何内容,脚本会工作,并且会显示 windows。但是,如果我的占位符在文本中,解释器会遇到一个问题,我认为这一定是重写的问题。我最近使用 类 重新完成了我的程序,并且在此之前我已经开始工作了。我是否遗漏了 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'
保存参数必须是文件名:
doc.save('TemplateTest.docx')
我正在做一个项目,其中 tkinter 应用程序提出问题,并根据我的输入,通过用我的条目替换占位符文本来编辑 word 文件。我遇到了 'Document' 没有属性 'write'.
的错误如果我在我的文本中搜索除占位符以外的任何内容,脚本会工作,并且会显示 windows。但是,如果我的占位符在文本中,解释器会遇到一个问题,我认为这一定是重写的问题。我最近使用 类 重新完成了我的程序,并且在此之前我已经开始工作了。我是否遗漏了 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'
保存参数必须是文件名:
doc.save('TemplateTest.docx')