Python docx2pdf 属性错误:Open.SaveAs

发布于 2025-01-10 23:30:46 字数 2597 浏览 0 评论 0原文

我正在尝试使用 docx2pdf 库将 docx 文件转换为 pdf,使用以下代码:

from docx2pdf import convert

convert("generated.docx")

此处所写。但我有一个错误:

Traceback (most recent call last):
  File "c:\Users\user\Desktop\folder\script.py", line 29, in <module>
    convert("generated.docx")
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\docx2pdf-0.1.8-py3.10.egg\docx2pdf\__init__.py", line 106, in convert
    return windows(paths, keep_active)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\docx2pdf-0.1.8-py3.10.egg\docx2pdf\__init__.py", line 33, in windows
    doc.SaveAs(str(pdf_filepath), FileFormat=wdFormatPDF)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.SaveAs

我也尝试使用 comtypespywin32 进行转换,但出现了相同的错误。我从此处获取代码。

import sys
import comtypes.client

wdFormatPDF = 17

in_file = os.path.abspath("generated.docx")
out_file = os.path.abspath("generated.pdf")

word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
---------------------------------
Traceback (most recent call last):
  File "c:\Users\user\Desktop\folder\script.py", line 45, in <module>
    doc.SaveAs(out_file, FileFormat=wdFormatPDF)
_ctypes.COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))
import sys
import win32com.client

wdFormatPDF = 17

in_file = os.path.abspath("generated.docx")
out_file = os.path.abspath("generated.pdf")

word = win32com.client.Dispatch('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
---------------------------------
Traceback (most recent call last):
  File "c:\Users\user\Desktop\folder\script.py", line 46, in <module>
    doc.SaveAs(out_file, FileFormat=wdFormatPDF)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.SaveAs

我该如何修复这个错误?或者请建议另一种将 docx 转换为 pdf 的方法。先感谢您

I am trying to convert a docx file to pdf using the docx2pdf library, using the following code:

from docx2pdf import convert

convert("generated.docx")

As written here. But I have an error:

Traceback (most recent call last):
  File "c:\Users\user\Desktop\folder\script.py", line 29, in <module>
    convert("generated.docx")
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\docx2pdf-0.1.8-py3.10.egg\docx2pdf\__init__.py", line 106, in convert
    return windows(paths, keep_active)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\docx2pdf-0.1.8-py3.10.egg\docx2pdf\__init__.py", line 33, in windows
    doc.SaveAs(str(pdf_filepath), FileFormat=wdFormatPDF)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.SaveAs

I also tried converting with comtypes and pywin32, but I get the same error. I take code from here.

import sys
import comtypes.client

wdFormatPDF = 17

in_file = os.path.abspath("generated.docx")
out_file = os.path.abspath("generated.pdf")

word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
---------------------------------
Traceback (most recent call last):
  File "c:\Users\user\Desktop\folder\script.py", line 45, in <module>
    doc.SaveAs(out_file, FileFormat=wdFormatPDF)
_ctypes.COMError: (-2147418111, 'Call was rejected by callee.', (None, None, None, 0, None))
import sys
import win32com.client

wdFormatPDF = 17

in_file = os.path.abspath("generated.docx")
out_file = os.path.abspath("generated.pdf")

word = win32com.client.Dispatch('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()
---------------------------------
Traceback (most recent call last):
  File "c:\Users\user\Desktop\folder\script.py", line 46, in <module>
    doc.SaveAs(out_file, FileFormat=wdFormatPDF)
  File "C:\Users\user\AppData\Local\Programs\Python\Python310\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Open.SaveAs

How can I fix this error? Or please suggest another way to convert docx to pdf. Thank you in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

海螺姑娘 2025-01-17 23:30:46

改变:

word = win32com.client.Dispatch('Word.Application')

import pythoncom
word = win32com.client.Dispatch('Word.Application', pythoncom.CoInitialize())

change:

word = win32com.client.Dispatch('Word.Application')

to

import pythoncom
word = win32com.client.Dispatch('Word.Application', pythoncom.CoInitialize())
你爱我像她 2025-01-17 23:30:46
from docx2pdf import convert

inputFile = "document.docx"
outputFile = "document2.pdf"
file = open(outputFile, "w")
file.close()

convert(inputFile, outputFile)

您应该首先创建输出文件

from docx2pdf import convert

inputFile = "document.docx"
outputFile = "document2.pdf"
file = open(outputFile, "w")
file.close()

convert(inputFile, outputFile)

You should create the output file first

短暂陪伴 2025-01-17 23:30:46

我的观察之一是,当在 Microsoft Word 中打开 Word 文档,然后我们尝试对同一个 Word 文件执行 convert() 时,就会发生此问题。

如果是这种情况,如果文件已打开,请先将其关闭,然后重试。

One of the observations I had is that this issue happens when the Word document is opened in Microsoft Word and then we try to execute convert() for the same Word file.

If that is the case, if the file is opened, please close it first and try again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文