使用 python-docx 时出现随机保存错误

问题描述 投票:0回答:1

我正在尝试替换 SharePoint 文档中的一些链接,因此我在本地下载文档,编辑它们并将它们上传回来。问题是 python-docx 的 save() 方法会导致脚本在随机点崩溃。它有与路径相关的 OSError,但是路径没问题,os.path.exists 说它存在,所以绝对没有像尝试打开不存在的路径这样的选项。

代码又大又复杂,很难做最小的例子,但我可以提供一些日志,也许会有帮助

Search for links to replace in D:\...\<filename>.docx 
Starting searching for and replacing links in <filename>.docx... 
path 'D:\...\<filename>.docx' exists: True 
Replaced hyperlink link 'https://docs.google.com/spreadsheets/d/<id>/edit#gid=590117954' with 'https://<domain>.sharepoint.com/sites/<name>/_layouts/15/Doc.aspx?sourcedoc=%...D&file=<file>' in paragraph #189. 
path 'D:\...\<filename>.docx' exists: True 
[Errno 22] Invalid argument: 'D:\\...\\<filename>.docx' 
Traceback (most recent call last):   
File "D:\...\sharepoint_links_replacer.py", line 156, in <module>
        raise e   File "D:\...\sharepoint_links_replacer.py", line 100, in <module>
        processed_links = update_file(matched_doc, in_doc_matched_links, site)   
File "D:\...\utils\sharepoint\sharepoint_mfa.py", line 487, in update_file
        replaced_links = update_doc(file_name, in_doc_links)   
File "D:\...\utils\editors.py", line 1160, in update_doc
        replaced_links = docx_replace(file_name, links_dicts)   
File "D:\...\utils\editors.py", line 818, in docx_replace
        doc.save(local_file_path)   
File "C:\Program Files\Python310\lib\site-packages\docx\document.py", line 154, in save
        self._part.save(path_or_stream)   
File "C:\Program Files\Python310\lib\site-packages\docx\parts\document.py", line 115, in save
        self.package.save(path_or_stream)   
File "C:\Program Files\Python310\lib\site-packages\docx\opc\package.py", line 174, in save
        PackageWriter.write(pkg_file, self.rels, self.parts)   
File "C:\Program Files\Python310\lib\site-packages\docx\opc\pkgwriter.py", line 32, in write
        phys_writer = PhysPkgWriter(pkg_file)   File "C:\Program Files\Python310\lib\site-packages\docx\opc\phys_pkg.py", line 141, in
    __init__
        self._zipf = ZipFile(pkg_file, 'w', compression=ZIP_DEFLATED)   
File "C:\Program Files\Python310\lib\zipfile.py", line 1249, in
    __init__
        self.fp = io.open(file, filemode) OSError: [Errno 22] Invalid argument: 'D:\\...\\<filename>.docx'

主要思想是我的3个步骤:

  • 使用 GraphAPI 从 SharePoint 获取字节文件内容,并使用上下文管理器“with”内的 file.write() 将其写入本地文件。
  • 解析文档以获取一些链接
  • 匹配和替换链接。

挤压发生在第三步。我正在逐个链接替换链接(自定义函数 docx_replace),并在每次替换后立即保存文档的本地版本(相信我,这是必要的)。在某个时刻,在随机文档上,它会失败并显示相同的消息。

科技

Bayoo-docx v.2.19(这是 python-docx 的分支,支持commets,不确定它们继承的是哪个版本的 python-docx。

PS函数的最小代码示例

python python-docx
1个回答
0
投票

我会仔细查看这部分错误:

Invalid argument: 'D:\\...\\<filename>.docx'

您是否忘记在某处替换

"<filename>"
占位符?我认为
"<filename>.docx"
不会是一个有效的文件名,因为“>”和用于重定向的字符等。

© www.soinside.com 2019 - 2024. All rights reserved.