Convert Text and Text File to PDF using Python Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like 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 useIt allows page format and marginIt allows to manage page header and footerPython 2.5 to 3.7 supportUnicode (UTF-8) TrueType font subset embeddingBarcode I2of5 and code39, QR code coming soon …PNG, GIF and JPG support (including transparency and alpha channel)Templates with a visual designer & basic html2pdfExceptions 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: Import the class FPDF from module fpdfAdd a pageSet the fontInsert a cell and provide the textSave 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: 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 - 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: Comment More infoAdvertise with us Next Article Convert Text and Text File to PDF using Python M mkumarchaudhary06 Follow Improve Article Tags : Technical Scripter Python python-utility Practice Tags : python Similar Reads 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 P 2 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 pr 3 min read Convert Text File to CSV using Python Pandas Converting Text File to CSV using Python Pandas refers to the process of transforming a plain text file (often with data separated by spaces, tabs, or other delimiters) into a structured CSV (Comma Separated Values) file using the Python Pandas library.In this article we will walk you through multip 2 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 fi 3 min read Convert PDF to Image using Python Many tools are available on the internet for converting a PDF to an image. In this article, we are going to write code for converting pdf to image and make a handy application in python. Before writing the code we need to install the required module pdf2image and poppler.Modules Neededpdf2image 1.14 2 min read Like