Hello guys! we can do anything with python. Yes, I can assure you that. we can build web and mobile apps, we can build machine learning and deep learning models. If you are beginner, this is going to be the most favourable article. In this tutorial, we are going to learn how to generate PDF using python.
Do know that python has many supporting libraries and worlds largest community? yes, there are so many python lovers ahead to make it pretty easy for even beginners.
Sometimes we need to convert our text files into PDFs. So we immediately open our browser and type ” text to pdf converters “. It’s easy right. But what happens if you don’t have an internet connection.
Don’t worry guys! I am here to help you. you can create your own text file to pdf converter applications using python.
Create text file to PDF converter using python
I am assuming that you definitely have python in your system. so let’s install our beautiful library, which helps us to build a pdf converter.
pip install fpdf
“FPDF” is a python package that is used to convert text files to pdf files. you can check more here FPDF.
let’s create a new python file and named as main.py or anything you want. And write the below lines in your file.
from fpdf import FPDF pdf = FPDF() pdf.add_page() pdf.set_font('Arial',size=18) pdf.cell(200,100,txt='Hello, you are in www.devpyjp.com ', align='C') pdf.output("txt_pdf.pdf")
I will explain what we have written in the above code block.
we import FPDF and create an object for it. Now we create a page. we set font_size, font_family. And now we are instructing the cell to structure our file. “200” represents the left padding of our text. “100” represents the top padding of our text.
align represents the position of content in the cell, “C” -center, “R”-right, “L”-left.
Note: Make changes according to your patterns.
pdf.output() method generate the output file. we need to give the output file name.
Now run this python script and see in your current directory, it has a new pdf file which was given above name. In my case it txt_pdf.pdf.
Ok, Jp! I am creating a pdf file that’s fine. But how can I generate pdf file from text file.?
Well, I am here to help you guys, let me explain how to generate pdf file using python. let’s modify a little in the above python script. so you stored your text in a text file, so we need to get that text into our scripts.
Fortunately, we file system in python, so we can easily create, read and modify text files using python. let’s use those skills here to build text to pdf converter using python.
from fpdf import FPDF f = open('exp.txt','r') #exp.txt is a text file here. content = '' for i in f: content =i #print(d) pdf = FPDF() pdf.add_page() pdf.set_font('Arial',size=18) pdf.cell(100,10,txt=content,align='C') pdf.output("txt_pdf.pdf")
Here, open() helps us to grab our text file from our current directory in our system. ‘r’ represents the mode of the file, read.
I created one string empty variable “content” which helps us to store our text from the text file. I used here a for loop to get that text from textfile and store it into “content”.
In pdf.cell() we reassign the txt parameter to “content” because we were storing the data in a variable content.
Now execute the script and check your local directory and open your pdf file. This is the output file.
Congratulations ! you did it man. If you love this article, please appreciate us and check our other articles here.
Here is a new article for you, How to generate QR code in python – Quick start guide.
Please subscribe to our newsletter to get notified whenever we post a new article. Thank you.