Open In App

Convert Python File to PDF

Last Updated : 02 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a versatile programming language widely used for scripting, automation, and web development. Occasionally, you might find the need to convert your Python code into a more accessible format, such as a PDF. In this article, we will explore some methods to achieve this conversion using different libraries: ReportLab, FPDF, and Matplotlib.

How To Convert A Python File To A Pdf?

Below, are the method To Convert A Python File To A Pdf in Python.

Convert A Python File To A Pdf Using ReportLab

ReportLab is a powerful Python library for creating PDF documents. First, install the ReportLab to convert a Python file to a PDF using the below command

pip install reportlab

In this example, below code uses the ReportLab library to convert a Python file (‘example.py‘) to a PDF (‘output_reportlab.pdf’). It creates a canvas for the PDF, reads the content of the Python file, and writes it to the PDF at coordinates (100, 800). Finally, the PDF is saved, resulting in a PDF document containing the Python code.

Python3




from reportlab.pdfgen import canvas
 
def convert_to_pdf_reportlab(input_file, output_pdf):
    pdf_canvas = canvas.Canvas(output_pdf)
 
    with open(input_file, 'r') as python_file:
        content = python_file.read()
        pdf_canvas.drawString(100, 800, content)
 
    pdf_canvas.save()
 
# Example usage:
convert_to_pdf_reportlab('example.py', 'output_reportlab.pdf')
print("PDF is Saved Successfully")


Output :

PDF is Saved Successfully

Convert A Python File To A Pdf Using FPDF

FPDF is a lightweight PDF generation library for Python. Here’s how you can use FPDF to convert a Python file first install it using below command:

pip install fpdf

In this example, below code utilizes the FPDF library to convert a Python file (‘example.py’) into a PDF (‘output_fpdf.pdf’). It creates a PDF document, adds a page, and sets the font. The content of the Python file is then read and inserted into the PDF with line breaks using `multi_cell`. Finally, the resulting PDF is saved, containing the formatted Python code.

Python3




from fpdf import FPDF
 
def convert_to_pdf_fpdf(input_file, output_pdf):
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size=12)
 
    with open(input_file, 'r') as python_file:
        content = python_file.read()
        pdf.multi_cell(0, 10, content)
 
    pdf.output(output_pdf)
 
# Example usage:
convert_to_pdf_fpdf('example.py', 'output_fpdf.pdf')
print("PDF is Saved Successfully")


Output :

PDF is Saved Successfully

Convert A Python File To A Pdf Using Matplotlib

Matplotlib is primarily a data visualization library, but it can also be used to convert text into a PDF. first install it.

pip install matplotlib

In this example, below code utilizes the Matplotlib library to convert a Python file (‘example.py’) into a PDF (‘output_matplotlib.pdf’). It reads the content of the Python file, creates a Matplotlib figure with a text element, and saves it as a PDF with no axes. The resulting PDF contains the Python code with the specified formatting.

Python3




import matplotlib.pyplot as plt
 
def convert_to_pdf_matplotlib(input_file, output_pdf):
    with open(input_file, 'r') as python_file:
        content = python_file.read()
        fig, ax = plt.subplots()
        ax.text(0.1, 0.5, content, wrap=True, fontsize=12)
        ax.axis('off')
 
        plt.savefig(output_pdf, format='pdf')
 
# Example usage:
convert_to_pdf_matplotlib('example.py', 'output_matplotlib.pdf')
print("PDF is Saved Successfully")


Output :

PDF is Saved Successfully

Video Demonstration



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads