com_error:在python中将docx转换为pdf时

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

我目前正在尝试使用Python将一些Word文档转换为PDF,一些文件使用我拥有的代码成功转换,而我在其他文件上收到以下错误消息:

错误1:报告了一些文件

com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'String is longer than 255 characters', 'wdmain11.chm', 41873, -2146819183), None)

错误2:部分报告

com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'Command failed', 'wdmain11.chm', 36966, -2146824090), None)

错误3:部分报告

AttributeError: 'NoneType' object has no attribute 'SaveAs'

下面是我正在使用的代码,知道为什么会发生这种情况:

from docx2pdf import convert


def convert_word_to_pdf(word_path):
    pdf_path = word_path.replace(".docx", ".pdf")
    convert(word_path, pdf_path)


word_path = '*file_path*'
convert_word_to_pdf(word_path)

感谢您的支持

我希望上面的Python代码能够将所有文件转换为PDF。

python win32com pythoncom docx2pdf
1个回答
0
投票

我遇到错误 1“字符串长度超过 255 个字符”。就我而言,我将此追踪到长文件路径。

因此,解决方法是在调用转换之前将 docx 文件复制到一个简短的命名临时文件夹中。

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