This Python script utilizes the fpdf
library to generate a PDF document based on data from a CSV file. The generated PDF contains multiple pages, each representing a different topic along with a specified number of pages.
- Python 3.x
- Install the
fpdf
library using:pip install fpdf
-
Ensure you have a CSV file named
topics.csv
with the following columns:Topic
: The title of the topic.Pages
: The number of pages for each topic.
-
Run the script using:
python main.py
-
The script will read the CSV file, generate a PDF (
pdf-template.pdf
), and populate it with topics and pages.
-
Import Libraries
- The script uses the
fpdf
library for PDF generation andpandas
for reading the CSV file.
- The script uses the
-
Initialize PDF Object
- An
FPDF
object is created with specified orientation, unit, and format.
- An
-
Read CSV Data
- The script reads data from the
topics.csv
file into a pandas DataFrame (df
).
- The script reads data from the
-
Iterate Over DataFrame
- For each row in the DataFrame, a new page is added to the PDF.
- Topic title is added to the page along with a horizontal line.
- Footer is set with the same topic title.
-
Generate Multiple Pages for Each Topic
- If a topic has more than one page, additional pages are generated with the same topic title in the footer.
-
Output PDF
- The generated PDF is saved as
pdf-template.pdf
.
- The generated PDF is saved as
- Ensure that the CSV file is correctly formatted with the required columns.
- Check the output PDF (
pdf-template.pdf
) for the finalized document.