How to Convert Docx to PDF

.doc to pdf using python

A simple example using comtypes, converting a single file, input and output filenames given as commandline arguments:

import sys
import os
import comtypes.client

wdFormatPDF = 17

in_file = os.path.abspath(sys.argv[1])
out_file = os.path.abspath(sys.argv[2])

word = comtypes.client.CreateObject('Word.Application')
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
doc.Close()
word.Quit()

You could also use pywin32, which would be the same except for:

import win32com.client

and then:

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

Converting .docx to .pdf in Python (File locked for editing)

It works fine when I run your code no matter the path. Maybe you need to exit ms Word before you try and run it?

Looping over the .doc files to convert them to .pdf (Python)

note that the error mentions your file name, but in a system path

C:\\windows\\system32\\PrivateCourse_AR.doc

That's because you're not actually calling a Word subprocess but a more complex communication protocol with MSWord, and obviously here MSWord is running using another current directory. So passing relative file paths fails in that case (and fortunately MSWord has the courtesy of providing the absolute path of the non found file)

To fix that, just do:

word.Documents.Open(os.path.abspath(file))

to make the path absolute relatively to your script (which is in the correct directory)

It's probably the same issue/fix for the save part:

doc.SaveAs(os.path.abspath(output), FileFormat=wdFormatPDF)

Aside: always use raw prefix for windows filepaths, you may have surprises with paths like C:\temp (tab character instead of \t, write r"C:\temp")

How can I implement a word to PDF conversion in python without importing any libraries?

Code it from scratch. If you're not going to use an external library, that is by definition pretty much your only option.

You'll want to become an expert in the formal specifications for both PDF
and MS Word. Given the complexity and history of each of those, I expect a senior developer will want 6-12 months of experience with each to obtain the necessary understanding.

You should also have 6-12 months' experience with Python, since you'll likely need to be familiar with the language in order to define and use all the functions you'll need. But in just a few years of dedication, you should be able to write the necessary code.

MORE REALISTICALLY, import Python libraries for managing PDFs and MS Word. That should only take a week or two.



Related Topics



Leave a reply



Submit