Prejdi na hlavný obsah

HOW #4 – Generate PDFs automatically

Bored from repetitive creation of the same documents? Ease your life with Python and fpdf2 library and let them do automatic work.

Generate PDFs automatically

Generate PDFs automatically

Are you bored from repetitive creation of the same documents each day/week/month? With python and fpdf2 library you can create the script which will do it for you!

Today, I will demonstrate such a simple document generation, so you could try it by yourself.

First, I will use FPDF2 library: https://pypi.org/project/fpdf2/.

Install it by running command in shell:

Create new empty python file and import libraries:

I need some sample text data to be the document created of (notice, that two-dimensional array could be easily generated by one line in python):

Init the document:

This code is self explanatory:

Insert date and time and line break:

Insert some other text in loop:

I will create the table, which can be displayed on more pages. When the table reaches new page, script will generate also table header. or this, I will write function, which will create table header:

I will start the table by calling:

Generate the table (if there is page break, create also table header):

My document is now finished. Lets save it:

The documentation of FPDF2 library:

https://pyfpdf.github.io/fpdf2/index.html

That’s all for today, and I hope you will have a good one.