DEV Community

Free Python Code
Free Python Code

Posted on

Converting Text to PDF Using Python.

Hi πŸ™‚πŸ–

In this post, I will share with you How To Convert Text to PDF Using Python.

step 1

install fpdf

pip install fpdf

step 2

You need to create a TXT file for testing

Lorem Ipsum is simply dummy text of the printing and typesetting industry.Lorem Ipsum has been the 
industry's standard dummy text ever since the 1500s, 
when an unknown printer took a galley of type and 
scrambled it to make a type specimen book. It has 
survived not only five centuries, but also the leap 
into electronic typesetting, remaining essentially 
unchanged. It was popularised in the 1960s with the 
release of Letraset sheets containing Lorem Ipsum passages, 
and more recently with desktop publishing software 
like Aldus PageMaker including versions of Lorem Ipsum.
Enter fullscreen mode Exit fullscreen mode

step 3

Add PDF file setting

from fpdf import FPDF

pdf = FPDF()

pdf.add_page()

pdf.set_font("Arial", size = 10) 
Enter fullscreen mode Exit fullscreen mode

step 4

Read all text from the TXT file

for word in open('my_txt_file.txt', 'r'):
    pdf.cell(200, 20, txt = word, ln = 1, align = 'L')

pdf.output('my_pdf_file.pdf')
Enter fullscreen mode Exit fullscreen mode

result

Image description

Now we're done πŸ€—

Don't forget to like and follow πŸ™‚

Support me on PayPal πŸ€—
https://www.paypal.com/paypalme/amr396

Top comments (0)