Convert text file to pdf using python | PDF generator in Python

Step by Step Procedure to create PDF Generator

Hello friends how are you, Today in this post "Convert text file to pdf using python" i am going to teach you how you can use Python to convert Text file into PDF file. If you are a computer science students or teacher and want to learn something interesting in programming then just go through this post. This can also be a simple and interesting project for you.

PDF is the most used digital media file which is used to create , present and exchange documents reliably, independent of software, hardware, or operating system. So in most of the cases we have to convert a Text file into PDF file so if you don't know how to convert Text file to pdf in python then just read this complete post and at last you can create your own Text File to PDF converter.

Now  i am going to explain it step by step so just go through this post to understand this completely.

If you want to understand this through video then watch this video i have explained it step by step live

Step 1: Install Python : Click here to watch a video on how to install python or Open any browser and type Download Python and click the first link you will get official website of python here you wi ll get a Download button and after clicking on this button you will get exe of latest python version just install it into your system. 

Step 2: Install Pycharm | Create Project | Install LibraryClick here to watch a single video on How to install Pycharm | Create Project | Install Library  or To install Pycharm IDE Open any browser and type Download Pycharm and click the first link you will get official website of Pycharm  here you will get a black download button and after clicking on this button you will get an exe of Pycharm , just install it into your system. If you are facing problem to install then watch the above video i have explained step by step.

Step 3: Create Project : Open Pycharm  click on File which is in top left of screen and select New Project. Then you will get a screen. Here first you need to select directory of your project and then type a name for your project like "TextToPDF" after directory name, and at last click the create button to create this project. 

Step 4: Create Python file: To create a python file for coding in your project just right click on project name "TextToPDF" and select new and click on Python File , you will get popup screen in which you have to type a name like "PdfConverter" for python file and press enter, it will create a python file with name PdfConverter.py inside the project TextToPDF. 

Step 5: Install library (fpdf) : Before doing code for this we have to first, install the library which is used to convert Text file into PDF file. To install this library just click on Terminal which will be in Left bottom of screen , you will get a console screen and in this console screen you have to type pip install fpdf and then press enter. After some seconds you will get a message like library is installed successfully. Still facing problem then watch the above video, i have explained everything.


Step 6: Code Explanation: To explain the code i have divided it into four part and the first part is 

1.Library import: Here i have imported the needed library to make this project

#import library
from fpdf import FPDF
from tkinter import*
from tkinter import filedialog

Here as you can see that i have used two library here fpdf and tkinter in which  fpdf library is  using to convert TextFile into PDF file and tkinter library is using to GUI window and Filedialog.

2.Convert Text to PDF: Here in this code i have created a function that will take a Text file and reads its content and it will place the content of Textfile into a PDF file and at last it will generate a PDF file.

def convert():
 #creating variable of PDF
 pdf = FPDF()
 #Adding pages
 pdf.add_page()
 #setting fon family and Textsize
 pdf.set_font("Arial", size=15)
 #open file in read mode
 f = open(fname, "r")
 #reading file content
 for x in f:
    #placing file content into pdf
    pdf.cell(200, 10, txt=x, ln=1, align='L')
 #generating a complete pdf file with name Krazy.pdf
 pdf.output("Krazy.pdf")
 #showing success message
 msgForGenerate.set("Krazy.pdf is generated successfully")

Here in the above code you can see that the function name is convert() and i have already explained each line of code. At last a pdf file will be generated with name Krazy.pdf and it will be stored inside that same folder where you have created your python file. 

3.Upload Text File: Here I have also used a function that will use a function askopenfilename which is the module of filedialog .

def UploadAction():
    #uploading file
    filename = filedialog.askopenfilename(filetypes =[('Text Files', '*.txt')])
    #showing message for uploaded file
    msgForUpload.set("Uploaded:"+filename)
    #declaring global variable
    global fname
    #passing uploaded filename into fname
    fname=filename

Here i have already explained each line of code with comment.


4.Create GUI Display: Here to create GUI display i have used tkinter library, There are two labels are used one for displaying message for uploading and another for displaying the success message about the pdf generation. I have also used two buttons here one for Uploading and another for conversion.

#creating GUI for Converter
root =Tk()
#setting window height and width
root.geometry("600x200")
#setting window background color
root["bg"]="#20325B"
#setting title of window
root.title("Krazy:Text File to PDF Converter")
#creating variable for messages
global msgForUpload
global msgForGenerate
msgForUpload=StringVar()
msgForGenerate=StringVar()
#label for uploading message
Label(root,text="Hello",textvariable=msgForUpload,font="Arial 12",bg="#20325B",fg="white").place(x=80,y=30)
#button for upload file
Button(root, text='Open', command=UploadAction,font="Arial 12 bold",width="10",bg="orange").place(x=80,y=70)
#button for convert
Button(root,text="Convert",font="Arial 12 bold",width="10",command=convert,bg="orange").place(x=350,y=70)
#label for generated/success message
Label(root,text="Hello",textvariable=msgForGenerate,font="Arial 12",bg="#20325B",fg="white").place(x=80,y=120)
root.mainloop()

Here i have already explained each line of code with comments.

Step 7: Complete Python code:  Here is the complete code for this project/program you just need to type this code into your python file or you can copy this code for your personal use.

#import library
from fpdf import FPDF
from tkinter import*
from tkinter import filedialog
#converting textfile into pdf
def convert():
 #creating variable of PDF
 pdf = FPDF()
 #Adding pages
 pdf.add_page()
 #setting fon family and Textsize
 pdf.set_font("Arial", size=15)
 #open file in read mode
 f = open(fname, "r")
 #reading file content
 for x in f:
    #placing file content into pdf
    pdf.cell(200, 10, txt=x, ln=1, align='L')
 #generating a complete pdf file with name Krazy.pdf
 pdf.output("Krazy.pdf")
 #showing success message
 msgForGenerate.set("Krazy.pdf is generated successfully")
#uploading Textfile
def UploadAction():
    #uploading file
    filename = filedialog.askopenfilename(filetypes =[('Text Files', '*.txt')])
    #showing message for uploaded file
    msgForUpload.set("Uploaded:"+filename)
    #declaring global variable
    global fname
    #passing uploaded filename into fname
    fname=filename
#creating GUI for Converter
root =Tk()
#setting window height and width
root.geometry("600x200")
#setting window background color
root["bg"]="#20325B"
#setting title of window
root.title("Krazy:Text File to PDF Converter")
#creating variable for messages
global msgForUpload
global msgForGenerate
msgForUpload=StringVar()
msgForGenerate=StringVar()
#label for uploading message
Label(root,text="Hello",textvariable=msgForUpload,font="Arial 12",bg="#20325B",fg="white").place(x=80,y=30)
#button for upload file
Button(root, text='Open', command=UploadAction,font="Arial 12 bold",width="10",bg="orange").place(x=80,y=70)
#button for convert
Button(root,text="Convert",font="Arial 12 bold",width="10",command=convert,bg="orange").place(x=350,y=70)
#label for generated/success message
Label(root,text="Hello",textvariable=msgForGenerate,font="Arial 12",bg="#20325B",fg="white").place(x=80,y=120)
root.mainloop()

Here just copy this code and paste it into PdfConverter.py file. 


Step 8: Run Code: To run this code just right click on the coding area and click on Run PdfConverter and you will get a message output screen like below.

Convert text file to pdf using python | PDF generator in Python

Here you have to Upload your text file so click on Open button to Select any Text file and when you will select any file then this screen will look like below

Convert text file to pdf using python | PDF generator in Python

Now here the last step after selecting a text file is to just click on Convert button and you will get a message like "Krazy.pdf file is generated successfully."

Convert text file to pdf using python | PDF generator in Python

Now open your project folder and you will get a new file Krazy.pdf just open it and the content of this pdf file will be same as your uploaded Text file.

I hope now you can "Convert text file to pdf using python". If you have any doubt regarding this post  or you want something more in this post then let me know by comment below i will work on it definitely.
 

Request:-If you found this post helpful then let me know by your comment and share it with your friend. 
 
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all. 

If you have not subscribed my website then please subscribe my website. Try to learn something new and teach something new to other. 

If you like my post then share it with your friends. Thanks😊Happy Coding.

Post a Comment

2 Comments

  1. Replies
    1. Thank you so much keep visiting you will get something new and interesting on daily basis 😊

      Delete