Create PDF File From .txt File Using Python fpdf

In this tutorial, we will introduce the way to use python fpdf library to convert a .txt file to a pdf file.

1. Install python fpdf library

pip install fpdf

2. Import library

from fpdf import FPDF as fp
import os

3. Read .txt file content

with open("myfile.txt","r+") as fp:
    content = fp.read()
paragraph=content.split("\n")

4. Convert text to pdf

txtPdf=fp()
txtPdf.add_page()
txtPdf.set_font("Roboto",size=14)
ct=1
for para in paragraph:
  txtPdf.cell(200,10,txt=paragraph,ln=ct,align="C")
  ct+=1

5. Output pdf

txtPdf.output()