How to Convert a .docx File to a PDF Using Python?

Estimated read time 2 min read

To convert a .docx file to a PDF using Python, you can use the python-docx library to read the .docx file and the pyfpdf library to generate the PDF. Here’s an example of how you can do it:

First, make sure you have the python-docx and fpdf libraries installed. You can install them using pip:

pip install python-docx
pip install fpdf

Then, you can use the following code to convert a .docx file to a PDF:

from docx import Document
from fpdf import FPDF

def convert_docx_to_pdf(docx_path, pdf_path):
    # Read the .docx file
    doc = Document(docx_path)

    # Create a new PDF object
    pdf = FPDF()

    # Iterate over each paragraph in the .docx file
    for paragraph in doc.paragraphs:
        # Add the text of the paragraph to the PDF
        pdf.add_page()
        pdf.set_font("Arial", size=12)
        pdf.cell(0, 10, txt=paragraph.text, ln=True)

    # Save the PDF to the specified path
    pdf.output(pdf_path)

# Specify the paths of the input .docx file and the output PDF file
docx_file = "input.docx"
pdf_file = "output.pdf"

# Call the conversion function
convert_docx_to_pdf(docx_file, pdf_file)

In this code, we use the Document class from the python-docx library to read the .docx file. We then create a new FPDF object from the pyfpdf library, iterate over each paragraph in the .docx file, and add the text of each paragraph to the PDF using the add_page and cell methods. Finally, we save the PDF to the specified output path using the output method.

Make sure to replace “input.docx” with the path to your .docx file, and “output.pdf” with the desired path for the generated PDF file.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply