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 Library: Click 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.
#import library from fpdf import FPDF from tkinter import* from tkinter import filedialog
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")
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()
#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 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
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."
2 Comments
nice...i like your work
ReplyDeleteThank you so much keep visiting you will get something new and interesting on daily basis 😊
Delete