-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
  • 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
  • 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

3 Answers3

1

I fould some thing here: https://medium.com/analytics-vidhya/3-methods-to-convert-docx-files-into-pdf-files-using-python-b03bd6a56f45 I pesonally think the easiest way to do it is the docx2pdf module.

0

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

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

0

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.

gab
  • 645
  • 7
  • 30