1

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

scwol111
  • 98
  • 9
  • 1
    For the last version, maybe try `word = win32com.client.gencache.EnsureDispatch('Word.Application')` instead? It seems as if all the versions are getting confused with the type library. – DS_London Feb 28 '22 at 16:03

3 Answers3

1

change:

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

to

import pythoncom
word = win32com.client.Dispatch('Word.Application', pythoncom.CoInitialize())
Jeril
  • 7,858
  • 3
  • 52
  • 69
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 27 '22 at 11:37
0
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

wl9724
  • 9
  • 2
  • It still doesn't work. The error does not change. I tried creating a real pdf, but it doesn't work either. – scwol111 Feb 28 '22 at 14:10
-1

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.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135