-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
32 lines (26 loc) · 895 Bytes
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from fpdf import FPDF
import glob
from pathlib import Path
# Create a list of text filepaths
filepaths = glob.glob("files/*.txt")
# Create one PDF file
pdf = FPDF(orientation="P", unit="mm", format="A4")
# Go through each text file
for filepath in filepaths:
# Add a page to the PDF document for each text file
pdf.add_page()
# Get the filename without the extension
# and convert it to title case (e.g. Cat)
filename = Path(filepath).stem
name = filename.title()
# Add the name to the PDF
pdf.set_font(family="Times", size=16, style="B")
pdf.cell(w=50, h=8, txt=name, ln=1)
# Get the content of each text file
with open(filepath, "r") as file:
content = file.read()
# Add the text file content to the PDf
pdf.set_font(family="Times", size=12)
pdf.multi_cell(w=0, h=6, txt=content)
# Produce the PDF
pdf.output("output.pdf")