The Wayback Machine - https://web.archive.org/web/20240717024330/https://www.geeksforgeeks.org/convert-text-and-text-file-to-pdf-using-python/
Open In App

Convert Text and Text File to PDF using Python

Last Updated : 25 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

PDFs are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. Converting a given text or a text file to PDF (Portable Document Format) is one of the basic requirements in various projects that we do in real life. So, if you don’t know how to convert a given text to PDF then this article is for you. In this article, you will come to know the way to convert text and text file to PDF in Python. FPDF is a Python class that allows generating PDF files with Python code. It is free to use and it does not require any API keys. FPDF stands for Free PDF. It means that any kind of modification can be done in PDF files. The main features of this class are:

  • Easy to use
  • It allows page format and margin
  • It allows to manage page header and footer
  • Python 2.5 to 3.7 support
  • Unicode (UTF-8) TrueType font subset embedding
  • Barcode I2of5 and code39, QR code coming soon …
  • PNG, GIF and JPG support (including transparency and alpha channel)
  • Templates with a visual designer & basic html2pdf
  • Exceptions support, other minor fixes, improvements and PEP8 code cleanups

To install the fpdf module type the below command in the terminal.

pip install fpdf

Approach:

  1. Import the class FPDF from module fpdf
  2. Add a page
  3. Set the font
  4. Insert a cell and provide the text
  5. Save the pdf with “.pdf” extension

Example: 

Python3




# Python program to create
# a pdf file
 
 
from fpdf import FPDF
 
 
# save FPDF() class into a
# variable pdf
pdf = FPDF()
 
# Add a page
pdf.add_page()
 
# set style and size of font
# that you want in the pdf
pdf.set_font("Arial", size = 15)
 
# create a cell
pdf.cell(200, 10, txt = "GeeksforGeeks",
         ln = 1, align = 'C')
 
# add another cell
pdf.cell(200, 10, txt = "A Computer Science portal for geeks.",
         ln = 2, align = 'C')
 
# save the pdf with name .pdf
pdf.output("GFG.pdf")  


Output: python fpdf

Converting text file to PDF

Now if we want to make the above program more advance what we can do is that from a given text file extract the data using file handling and then insert it into the pdf file. The approach is all same as above, one thing you have to do is extract the data from a text file using file handling. Note: Refer this article to know more about file handling in Python. Example: Let’s suppose the text file looks like this – python-fpdf-text-file 

Python3




# Python program to convert
# text file to pdf file
 
 
from fpdf import FPDF
  
# save FPDF() class into
# a variable pdf
pdf = FPDF()  
  
# Add a page
pdf.add_page()
  
# set style and size of font
# that you want in the pdf
pdf.set_font("Arial", size = 15)
 
# open the text file in read mode
f = open("myfile.txt", "r")
 
# insert the texts in pdf
for x in f:
    pdf.cell(200, 10, txt = x, ln = 1, align = 'C')
  
# save the pdf with name .pdf
pdf.output("mygfg.pdf")  


Output: python-fpdf



Similar Reads

How to convert PDF file to Excel file using Python?
In this article, we will see how to convert a PDF to Excel or CSV File Using Python. It can be done with various methods, here are we are going to use some methods. Method 1: Using pdftables_api Here will use the pdftables_api Module for converting the PDF file into any other format. It's a simple web-based API, so can be called from any programmin
2 min read
How to convert CSV File to PDF File using Python?
In this article, we will learn how to do Conversion of CSV to PDF file format. This simple task can be easily done using two Steps : Firstly, We convert our CSV file to HTML using the PandasIn the Second Step, we use PDFkit Python API to convert our HTML file to the PDF file format. Approach: 1. Converting CSV file to HTML using Pandas Framework. P
3 min read
How to convert a PDF file to TIFF file using Python?
This article will discover how to transform a PDF (Portable Document Format) file on your local drive into a TIFF (Tag Image File Format) file at the specified location. We'll employ Python's Aspose-Words package for this task. The aspose-words library will be used to convert a PDF file to a TIFF file. Aspose-Words: Aspose-Words for Python is a pot
3 min read
Convert PDF File Text to Audio Speech using Python
Let us see how to read a PDF that is converting a textual PDF file into audio. Packages Used: pyttsx3: It is a Python library for Text to Speech. It has many functions which will help the machine to communicate with us. It will help the machine to speak to usPyPDF2: It will help to the text from the PDF. A Pure-Python library built as a PDF toolkit
2 min read
Send PDF File through Email using pdf-mail module
pdf_mail module is that library of Python which helps you to send pdf documents through your Gmail account. Installing Library This module does not come built-in with Python. You need to install it externally. To install this module type the below command in the terminal. pip install pdf-mail Function of pdf_mail This module only comes with a singl
2 min read
Extract text from PDF File using Python
All of you must be familiar with what PDFs are. In fact, they are one of the most important and widely used digital media. PDF stands for Portable Document Format. It uses .pdf extension. It is used to present and exchange documents reliably, independent of software, hardware, or operating system. We will extract text from pdf files using two Pytho
3 min read
Convert Python File to PDF
Python is a versatile programming language widely used for scripting, automation, and web development. Occasionally, you might find the need to convert your Python code into a more accessible format, such as a PDF. In this article, we will explore some methods to achieve this conversion using different libraries: ReportLab, FPDF, and Matplotlib. Ho
3 min read
Save multiple matplotlib figures in single PDF file using Python
In this article, we will discuss how to save multiple matplotlib figures in a single PDF file using Python. We can use the PdfPages class's savefig() method to save multiple plots in a single pdf. Matplotlib plots can simply be saved as PDF files with the .pdf extension. This saves Matplotlib-generated figures in a single PDF file named Save multip
3 min read
Modifying PDF file using Python
The following article depicts how a PDF can be modified using python's pylovepdf module. The Portable Document Format(PDF) is a file format developed by Adobe in 1993 to present documents, including text formatting and images, in a manner independent of application software, hardware, and operating systems. pylovepdf module can be downloaded using
3 min read
Take and convert Screenshot to PDF using Python
In order to take and convert a screenshot to PDF, firstly the PyAutoGUI can be used which is an automation library in python which can control mouse, keyboard and can handle many GUI control tasks. Secondly, for the conversion PIL(Python Imaging Library) of python can be used which provides image processing facility and it supports many file format
3 min read
Practice Tags :
three90RightbarBannerImg