Search code examples
pythondocxpython-docx

Closing a docx file if it's open


I'm working with docx files and to prevent the PermissionError: [Errno 13] Permission denied error, I tried to add os.close() in my code but as I saw, it doesn't accept the file path, it accepts file descriptor as a parameter.So I tried that:

file_path = 'my file path'
mydoc = docx.Document()
mydoc.add_paragraph('text')
try:
    mydoc.save(file_path)
    return
except PermissionError:
    fd = os.open(file_path, os.O_WRONLY)
    os.close(fd)
    mydoc.save(file_path)
    return

But when I run it, it passes the first PermissionError error because of the error handling, but when it tries to do fd = os.open(file_path, os.O_WRONLY), I got the same error. So is there a possible way to close a docx file if it's open?

Edit:

Here is the entire traceback

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\17326\PycharmProjects\newEksi\main2.py", line 194, in arat
    mydoc.save(dosya_yolu)
  File "C:\Users\17326\PycharmProjects\newEksi\venv\lib\site-packages\docx\document.py", line 167, in save
    self._part.save(path_or_stream)
  File "C:\Users\17326\PycharmProjects\newEksi\venv\lib\site-packages\docx\parts\document.py", line 111, in save
    self.package.save(path_or_stream)
  File "C:\Users\17326\PycharmProjects\newEksi\venv\lib\site-packages\docx\opc\package.py", line 172, in save
    PackageWriter.write(pkg_file, self.rels, self.parts)
  File "C:\Users\17326\PycharmProjects\newEksi\venv\lib\site-packages\docx\opc\pkgwriter.py", line 32, in write
    phys_writer = PhysPkgWriter(pkg_file)
  File "C:\Users\17326\PycharmProjects\newEksi\venv\lib\site-packages\docx\opc\phys_pkg.py", line 141, in __init__
    self._zipf = ZipFile(pkg_file, 'w', compression=ZIP_DEFLATED)
  File "C:\Users\17326\AppData\Local\Programs\Python\Python38-32\lib\zipfile.py", line 1251, in __init__
    self.fp = io.open(file, filemode)
PermissionError: [Errno 13] Permission denied: 'C:/Users/17326/Desktop/entries.docx'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\17326\AppData\Local\Programs\Python\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "C:\Users\17326\PycharmProjects\newEksi\main2.py", line 197, in arat
    fd = os.open(dosya_yolu, os.O_WRONLY)
PermissionError: [Errno 13] Permission denied: 'C:/Users/17326/Desktop/entries.docx'

Solution

  • There is no such thing as an "open" file in python-docx. When you read in a file to edit it with document = Document("my-file.docx"), python-docx reads in the file and that's it. Yes, it is open for a split second while it's being read in, but it does not remain open. The open/close cycle ends before the Document() call returns.

    Same when you're saving the file. When you call document.save("my-output-file.docx"), the file is opened, written, and closed, all before the .save() method returns.

    So it's not like Word itself where you open a file, work on it for a while and then save and close it. You're just reading a "starting" file into memory, making changes to that in-memory object, and then later writing the in-memory representation (almost always to a different file).

    The comments are on the right track. Look for a permission problem not allowing you to write a file in that location that is not connected to an open file, unless you have the file in question open in Word or something at the time your program runs.