Search code examples
pythontkinterattributesoperating-systemdocx

Python Docx and Tkinter, 'Document' has no attribute 'write'


I am working on a project where a tkinter application asks questions, and based on my input, edits a word file by replacing placeholder text with my entries. I have encountered an error where the 'Document' has no attribute 'write'.

If I search for anything other than a placeholder in my text, the script works, and I am presented with the windows. However, if my placeholder is in the text, the interpreter encounters a problem with what I assume must be that of writing over. I have recently re done my program using classes and I had it working before then. Am I missing something with the init function?

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()

Full trace error

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'

Solution

  • The save argument needs to be the file name:

    doc.save('TemplateTest.docx')