Python PDF Generator - Convert Text to PDF in Python

Published February 03, 2022

PDF files are among the most popular types of digital media files. In this post, we will learn how to convert a text file into a PDF in Python using the FPDF class. The Python language uses the FPDF class to generate PDF files. This class is easy to use, free, and doesn't require API keys.

 

The steps below will guide you in creating a Python application that will convert your selected text file into a PDF.

Step 1: Create a new Python project using the File menu in PyCharm or your favorite Python IDE.

Step 2:  Import all the required Libraries.  We will import the tkinter library and the fpdf library for this application. The tkinter library enables us to create a GUI window and the dialog box. To import these libraries, add the following code

from fpdf import FPDF

from tkinter import*

from tkinter import filedialog

Step 4:  First, we will create a function to select and upload a Text file that will be converted to a PDF. To do this, we will use the askopenfilename and the filedialog. The filetype allows us to specify that we will only select the TextFiles

def UploadAction():

filename = filedialog.askopenfilename(filetypes =[('Text Files', '*.txt')])

msgForUpload.set("Uploaded:"+filename)

global fname

fname=filename

After uploading our Text file, we need to convert it to PDF. We will create a function to convert our Text file to PDF in the next step.

Step 5: Create a function to convert the selected text to PDF. Basically, this function will take the text file, extract its content, put it in a PDF file, and finally generate the PDF file. Upon successful conversion of the PDF into PDF, the following message will be displayed: “Text converted to PDF successfully!!!”

 

def convert():

pdf = FPDF()

 pdf.add_page()

 pdf.set_font("Arial", size=12)

f = open(fname, "r")

 for x in f:

pdf.cell(200, 10, txt=x, ln=1, align='L')

pdf.output("Text_to_Python.pdf")

msgForGenerate.set("Text converted to PDF successfully!!!")

 

As a next step, a GUI user interface will be created, and two buttons will be added to upload and convert the Text file into the PDF.

Step 6: We will use the Tkinter library to create the GUI display. Create the GUI, and then add labels and buttons.

The first Label will display information, including the name and path of the selected file

 

Label(root,text="Hello",textvariable=msgForUpload,font="Arial 12",fg="green").place(x=80,y=30)

The second label will display the message whether the message has been successfully converted into PDF or not

Button(root, text='Select Txt file', command=UploadAction,font="Arial 12 bold",width="12", bg="gray").place(x=80,y=70)

 

The first Button will allow the user to browse and select the Text file to be converted into a PDF

Button(root,text="Convert to PDF",font="Arial 12 bold",width="12",command=convert,bg="gray").place(x=350,y=70)

 

The second button “Convert”, will convert the selected Text file into PDF

Label(root,text="Hello",textvariable=msgForGenerate,font="Arial 12",fg="green").place(x=80,y=120)

 

Step 7: We are done. You can now run your application

from fpdf import FPDF

from tkinter import*

from tkinter import filedialog

def convert():

 pdf = FPDF()

 pdf.add_page()

 pdf.set_font("Arial", size=12)

 f = open(fname, "r")

 for x in f:

    pdf.cell(200, 10, txt=x, ln=1, align='L')

 pdf.output("Text_to_Python.pdf")

 msgForGenerate.set("Text converted to PDF successfully!!!")

 

def UploadAction():

    filename = filedialog.askopenfilename(filetypes =[('Text Files', '*.txt')])

    msgForUpload.set("Uploaded:"+filename)

    global fname

    fname=filename

root =Tk()

root.geometry("600x200")

root.title("Convert Text to PDF")

global msgForUpload

global msgForGenerate

msgForUpload=StringVar()

msgForGenerate=StringVar()

Label(root,text="Hello",textvariable=msgForUpload,font="Arial 12",fg="green").place(x=80,y=30)

Button(root, text='Select Txt file', command=UploadAction,font="Arial 12 bold",width="12", bg="gray").place(x=80,y=70)

Button(root,text="Convert to PDF",font="Arial 12 bold",width="12",command=convert,bg="gray").place(x=350,y=70)

Label(root,text="Hello",textvariable=msgForGenerate,font="Arial 12",fg="green").place(x=80,y=120)

root.mainloop()

 

 

How do i convert text to pdf python
Python text to pdf conversion

 

python text conversion pdf

 

 

Python Text conversion to pdf

 

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

899 Views