关闭docx文件如果已打开

问题描述

我正在使用 docx文件,并且为了防止出现PermissionError: [Errno 13] Permission denied错误,我尝试在代码添加os.close(),但正如我所见,它不接受文件路径,它接受文件描述符作为参数。所以我尝试了:

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

但是当我运行它时,由于错误处理,它传递了第一个PermissionError错误,但是当它尝试执行fd = os.open(file_path,os.O_WRONLY)时,我遇到了相同的错误。那么,有没有办法关闭 docx文件(如果打开)?

编辑:

这是整个追溯

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'

解决方法

python-docx中没有“打开”文件之类的东西。当您读入文件并用document = Document("my-file.docx")对其进行编辑时,python-docx会读入文件,就是这样。是的,在读入时它会瞬间打开,但不会保持打开状态。打开/关闭周期在Document()调用返回之前结束。

与保存文件时相同。调用document.save("my-output-file.docx")时,将在.save()方法返回之前全部打开,写入和关闭文件。

因此,与打开文件,处理文件一段时间然后保存并关闭文件的方式不像Word本身。您只是将“开始”文件读入内存,对内存中的对象进行更改,然后再写入内存中的表示形式(几乎总是写入其他文件)。

评论在正确的轨道上。寻找一个权限问题,不允许您在未连接到打开文件的位置写入文件,除非您在Word或程序运行时打开了有问题的文件。

,

python-docx 可以从所谓的类文件对象打开文档。它还可以保存到类似文件的对象。当您想通过网络连接或从数据库获取源或目标文档并且不想(或不允许)与文件系统交互时,这会很方便。实际上,这意味着您可以传递一个打开的文件或 StringIO/BytesIO 流对象来打开或保存文档,如下所示:

f = open('foobar.docx','rb')
document = Document(f)
f.close()

# or

with open('foobar.docx','rb') as f:
    source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()
...
target_stream = StringIO()
document.save(target_stream)
,

我试图通过展示一些代码来添加其他人的解释。

file_path = 'my file path'
mydoc = docx.Document()
mydoc.add_paragraph('text')
hasError = False
try:
    fd = open(file_path)
    fd.close()
    mydoc.save(file_path)
except PermissionError:
    raise Exception("oh no some other process is using the file or you don't have access to the file,try again when the other process has closed the file")
    hasError = True
finally:
    if not hasError:
        mydoc.save(file_path)
return