-1

I am workin on a report generator, and I used pip install python-docx and import docx. Now I have made a new docx file, edited it but I want to save it in pdf instead of docx file. And the program script will be converted into EXE file. Please help.

(pip install python-docx)

from docx import Document

doc=Document()

doc.add_heading('Report', 0)

# Now to save file, I know to save in docx,
# But, I want to save in pdf
# I can not finish the program and then manually convert
# As this script will run as an 
# **EXE**    

doc.save('report.docx')

I tried saving like --> doc.save('report.pdf') But, it did not work.

Aryan Jain
  • 13
  • 1
  • 3
  • 2
    If the end result is going to be a PDF, then why go through the DOCX phase at all? There are several excellent PDF libraries for Python that will allow to create a PDF directly, with full and complete control. – Tim Roberts Jul 28 '21 at 05:49
  • 1
    Does this answer your question? [.doc to pdf using python](https://stackoverflow.com/questions/6011115/doc-to-pdf-using-python) – AlexisG Jan 17 '22 at 11:35

2 Answers2

1

You can use the python package docx2pdf*:

pip install docx2pdf

Then call the convert function: convert("report.docx", "report.pdf") after saving doc.save('report.docx'). Creating the docx file before converting it is mandatory.

  • unless you work on a Linux Machine as it requires Microsoft Word to be installed.
Community
  • 1
  • 1
0

Try using the msoffice2pdf library using Microsoft Office or LibreOffice installed in the environment.

https://pypi.org/project/msoffice2pdf/