-2

How do I go about converting a word file to a pdf in python? I use replit, so I realise that rules out all the options that require linux to install Office

Many thanks,

ideally, this is what i expect

input(DOCUMENT.docx)
document.docx.convert(pdf)
output(DOCUMENT.pdf)

macropod
  • 12,757
  • 2
  • 9
  • 21
002593
  • 3
  • 2
  • You have this answer too. [.doc to pdf using python](https://stackoverflow.com/questions/6011115/doc-to-pdf-using-python) – JLMelandri8 Jan 23 '23 at 10:09
  • If I understand @002593 correctly, he is on Linux, so **no Windows components** can be used (win32com, etc). But you could install `LibreOffice` and use its PDF export facility. You would have to check out if / how this can be used as a batch command (CLI). Then use Python `subprocess` module to do the invocation. Check here: https://www.libreofficehelp.com/batch-convert-writer-documents-pdf-libreoffice/ – Jorj McKie Jan 23 '23 at 10:16
  • 1
    Found it: `lowriter --headless --convert-to pdf yourword.docx`. Results in `yourword.pdf`. `lowriter` is part of the LibreOffice installation. – Jorj McKie Jan 23 '23 at 10:22
  • OpenOffice variant: https://stackoverflow.com/questions/4818342/convert-word-doc-to-pdf-python – tevemadar Jan 23 '23 at 13:52
  • Yes, the common thing in them is that they'll unlikely work in an online environment, let it be repl.it or some other one. – tevemadar Jan 23 '23 at 13:58

1 Answers1

0

Just confirmed that the following works in Linux:

import subprocess
import os

wordfile = "yourword.docx"
command = ["lowriter", "--headless", "--convert-to", "pdf", wordfile]
subprocess.call(command)
if os.path.exists("yourword.pdf"):
    print("Done")
Jorj McKie
  • 2,062
  • 1
  • 13
  • 17