Hello, readers! In this article, we will learn to use the fpdf module in Python to convert text data and files into PDF format, in Python.
So, let us get started!
Using the fpdf module to convert data/files to PDF
Python serves us with numerous libraries to perform real-time conversions and to solve real-life problems through automation coming into the picture with python libraries.
When it comes to data transfer or conversion of data into certain formatted files, Python does offer us with some functions to perform those actions.
With Python, we can automate the entire process of conversion and storage of data into a PDF form. This is possible with the fpdf module in Python.
This case helps when we want to automate the process of data transfer to pdf directly without any manual intervention. You can even send the files to a particular storage location that may be local to the system or even on some public, private cloud storage.
Today, in this article, we will be performing the below two tasks with the fpdf module in Python:
- Storing script generated text data into a PDF format.
- Conversion of text files into PDF format.
So, let us begin!
1. Storing data into PDF format
Initially, in this section, we have tried to store the script generated text data into the PDF form at a particular location.
To achieve the same, we have to import FPDF class from fpdf module. Further, we need to create a page in the pdf to imprint data on the same. This can be achieved using add_page() method. The set_font() function is used to set attributes to the text i.e. the font of the text, size of alphabets, etc.
Having created a page, we now use cell() function to set the width and height of the pagination and also set the data to be pasted to the newly created pdf file.
At last, we make use of output() function to print the data into the created PDF at the mentioned location.
from fpdf import FPDF pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size = 25) # create a cell pdf.cell(200, 10, txt = "JournalDev", ln = 1, align = 'C') pdf.cell(200, 10, txt = "Welcome to the world of technologies!", ln = 2, align = 'C') pdf.output("data.pdf")
2. Conversion of text file into PDF file in Python
Having stored the script generated text file to PDF form, now let us convert the locally available text file to PDF form using the fpdf module.
For the same, we need to use the file.open() function to open the text file in “read” mode. After which, we traverse through the data in a for loop and then use the cell() function to store that traversed data into the PDF form.
At last, we make use of output() function to create the designed PDF at the mentioned location with the stated name.
from fpdf import FPDF pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size = 25) # create a cell file = open("data.txt", "r") # insert the texts in pdf for g in file: pdf.cell(200, 10, txt = g, ln = 1, align = 'C') pdf.output("PDF.pdf")
By this, we have come to the end of this topic. Feel free to comment below, in case you come across any questions.
For more such posts related to Python programming, Stay tuned with us!
Till then, Happy Learning!! 🙂