0

I am writing a python script and would like to convert a DOCX to PDF. Are there any ways of doing this? Here's my current code:

printer_path = 'C:\\Program Files\\Nitro\\Pro\\12\\NitroPDF.exe'
doc_path = 'Test.docx'

subprocess.call([printer_path,  doc_source_path])

Nitro PDF will open and begin converting the file but won't finish. Thank you for any input.

Edit 1: For the subprocess.call to work, I had to make both inputs absolute paths e.g. doc_path = 'C:\Documents\Test.docx'

Nick Ysidron
  • 36
  • 1
  • 10

2 Answers2

1

If you have Microsoft Word installed the following should work:

subprocess.call('docto -f "C:\Dir with Spaces\FilesToConvert\" -O "C:\DirToOutput" -T wdFormatPDF  -OX .pdf', shell=True)
jwalton
  • 5,286
  • 1
  • 18
  • 36
1

You can use the docx2pdf python package to convert docx with zero formatting issues on Windows or macOS. It requires Microsoft Word to be installed and uses the COM API on Windows and AppleScript (JXA) on macOS.

from docx2pdf import convert

convert("input.docx")
convert("input.docx", "output.pdf")
convert("my_docx_folder/")

Disclaimer: I wrote docx2pdf. https://github.com/AlJohri/docx2pdf

Al Johri
  • 1,729
  • 22
  • 23
  • docx2pdf is not implemented for linux as it requires Microsoft Word to be installed – PHZ.fi-Pharazon Apr 08 '21 at 07:30
  • @PHZ.fi-Pharazon that's right! Although I heard some reports that it seems to be compatible with "WPS Office" (https://github.com/AlJohri/docx2pdf/pull/10) but I haven't tried it myself. – Al Johri Apr 08 '21 at 12:46