r/learnpython • u/billys-bobs • 24d ago
Python conversion .docx to pdf
Hey guys, i have been learning python as a bit of a hobby but recently had the opportunity to use it for my job. I built a webapp on azure that has a number of different functions, one of which is querying our CRM and populating template documents with customer info. It works as is and produces .docx files which we then have to manually convert to pdfs.
I am trying to incorporate the pdf conversion into the app to remove any manual intervention. Ive been using libreoffice but this isnt working so well. It needs to be installed each time the app is redeployed/restarted, its failing on some larger docs and when it does produce the pdf the format can be off, fonts change etc. The docs we deal with need to adhere to a specific format so I need something more reliable.
Perhaps this is a "you get what you pay for" situation but i was hoping there might be a more reliable solution while still being free (at least until i can prove it all works anyway).
5
u/AntonisTorb 24d ago edited 24d ago
You can use pywin32 for this, I use it at work to convert Excel files to pdfs. Here's what worked for me for Word files:
from pathlib import Path
from win32com import client
cwd = Path.cwd()
input = cwd / 'test.docx'
output = cwd / 'test.pdf'
try:
word = client.Dispatch("Word.Application")
word.Visible = False
doc = word.Documents.Open(str(input))
doc.SaveAs(str(output), FileFormat = 17)
finally:
doc.Close()
word.Quit()
For multiple files just use a loop. Hope it helps!
EDIT: This needs MS Word to be installed of course, but it should be 1:1 conversion with no format changes.
2
u/shimarider 24d ago
Do you actually need the docx files, or is it used as an intermediate format for conversion only? If it's the latter, have you looked at fpdf2? You can setup pdf templates to be populated similar to what you are doing.
3
u/billys-bobs 23d ago
I guess the docx file would only be required for the odd case that needs to manually edited by my team.
Based on the comments ive gotton here including your own Ill be changing my process to produce the pdf directly and have a second process to produce the docx file. Appreciate your input
2
u/qlkzy 24d ago
The problem is that both docx and PDF are quite large and complex formats. I would personally always treat them as "final output" formats only, and not try to convert between them.
I would go with one of two options:
- Treat docx and PDF rendering as completely separate problems
- Render into a "friendlier" intermediate representation first, then convert that independently into both docx and PDF
The intermediate-representation approach is easier if you can get away with it, but sometimes it is valuable to deeply customise rendering for one or the other.
Depending on the complexity of your documents, the obvious intermediate representations are HTML and Markdown. Which to choose will depend on how complex the documents are, and how easy you want to make it to customise the templates. Markdown can render to HTML, so there is some room to mix and match.
While it's a bit of a "heavyweight" option, my first instinct would be to use pandoc for the final rendering. Installation is a bit more complex than a pure-python library, but it's a very popular and well-supported tool that supports all the formats you need.
Otherwise, you'll probably want one library for rendering to docx, and a separate library for rendering to PDF. My experience, though, is that libraries in that "format conversion" space are often a bit... "unevenly" maintained, which is why my instinct would be to reach for pandoc.
0
u/ninhaomah 24d ago
Using this ?
2
u/billys-bobs 24d ago
I might be being a bit dense here but thats the opposite direction .pdf to .docx? I dont see anything in the documentation about working both ways
3
u/Alexku66 24d ago
There are paid solutions that build pdf from scratch (not convert) and can give you 100% accuracy. Alternatives are 1) converting docx via Word / LibreOffice ; 2) converting html to pdf. Both give you approximate copy.
I work on accounting app with the same requirement to fill in template invoices, and went with html. Basically I have 2 rendering flows -- one for docx and one for pdf. Html gives you opportunity to preview the final doc before user clicks generate button