1
import os
import sys
from docx2pdf import convert

convert("D:\Dev2Ease\PyC\Output Word","D:\Dev2Ease\PyC\Output Pdf")
print ("All the files have been converted to pdf")

for the above-mentioned code, I get an error while the execution is in progress...however the conversion from docx to pdf is happening. just that I keep getting below error. can someone help me here?

Traceback (most recent call last):
  File "D:/Dev2Ease/PyC/word2pdf.py", line 5, in <module>
    convert("D:\Dev2Ease\PyC\Output Word","D:\Dev2Ease\PyC\Output Pdf")
  File "C:\Users\Shailesh\AppData\Roaming\Python\Python310\site-packages\docx2pdf\__init__.py", line 106, in convert
    return windows(paths, keep_active)
  File "C:\Users\Shailesh\AppData\Roaming\Python\Python310\site-packages\docx2pdf\__init__.py", line 25, in windows
    doc = word.Documents.Open(str(docx_filepath))
  File "<COMObject <unknown>>", line 5, in Open
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Word', 'The file appears to be corrupted.', 'wdmain11.chm', 25272, -2146822496), None)
genpfault
  • 51,148
  • 11
  • 85
  • 139
shall
  • 11
  • 1

1 Answers1

0

It seems to be a trend when trying to write to inexistent files: pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)

To have more consistent path variables, I would recommend using the os package. You can be more sure about your paths that way. You would then set your path as:

import os
from docx2pdf import convert

path1 = os.path.join("D:/", "Dev2Ease", "PyC", "Output", "Word")
path2 = os.path.join("D:/", "Dev2Ease", "PyC", "Output", "Pdf")
convert(path1, path2)
  • @shall and did this help with the conversion operation from docx to pdf? – Steinn Hauser Magnússon Jun 20 '22 at 15:24
  • https://stackoverflow.com/a/72689265/19375960 – shall Jun 20 '22 at 15:27
  • still getting same error after making the changes as suggested by you. – shall Jun 20 '22 at 15:29
  • And can you open the file with no issues on your machine? Is it maybe that you don't have the `.docx` and `.pdf` extensions in your filenames? – Steinn Hauser Magnússon Jun 20 '22 at 15:49
  • yeah.. i am to open the files... no issues....Also, i have the word file saved as .docx and the converted files to pdf has .pdf extension. But i still get the error. – shall Jun 20 '22 at 16:12
  • still waiting.. if someone could provide a solution to this – shall Jun 22 '22 at 10:23
  • or is there a way i can make to run in silent mode with in python,... is there any switch in python which i can use... without any text displayed on screen (thereby suppressing the error) – shall Jun 22 '22 at 13:38
  • @shall if this is a bottleneck in your project and you require a solution, could you post a dropbox link where one could download the docx file and try to run the code themselves? Makes it easier to pinpoint where the actual error is – Steinn Hauser Magnússon Jun 22 '22 at 14:36