-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (53 loc) · 2.34 KB
/
Copy pathmain.py
File metadata and controls
67 lines (53 loc) · 2.34 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import pandas as pd
import glob
from fpdf import FPDF
from pathlib import Path
filepaths = glob.glob("invoices/*.xlsx")
for filepath in filepaths:
df = pd.read_excel(filepath, sheet_name="Sheet 1")
pdf = FPDF(orientation="P", unit="mm", format="A4")
pdf.add_page()
# get file name as a string
filename = Path(filepath).stem
# split the name in the "-" as a list, and get the first item
invoice_nr, date = filename.split("-")
pdf.set_font(family="Times", size=16, style="B")
pdf.cell(w=50, h=8, txt=f"Invoice nr.{invoice_nr}", ln=1)
pdf.set_font(family="Times", size=16, style="B")
pdf.cell(w=50, h=8, txt=f"Date: {date}", ln=1)
# add a header
columns_invoices = df.columns
columns_invoices = [item.replace("_", " ").title() for item in columns_invoices]
pdf.set_font(family="Times", size=10, style="B")
pdf.set_text_color(80, 80, 80)
pdf.cell(w=30, h=8, txt=columns_invoices[0], border=1)
pdf.cell(w=60, h=8, txt=columns_invoices[1], border=1)
pdf.cell(w=30, h=8, txt=columns_invoices[2], border=1)
pdf.cell(w=30, h=8, txt=columns_invoices[3], border=1)
pdf.cell(w=30, h=8, txt=columns_invoices[4], border=1, ln=1)
# add rows to the table
for index, row in df.iterrows():
pdf.set_font(family="Times", size=10)
pdf.set_text_color(80, 80, 80)
# pdf.cell expect a string
pdf.cell(w=30, h=8, txt=str(row["product_id"]), border=1)
pdf.cell(w=60, h=8, txt=str(row["product_name"]), border=1)
pdf.cell(w=30, h=8, txt=str(row["amount_purchased"]), border=1)
pdf.cell(w=30, h=8, txt=str(row["price_per_unit"]), border=1)
pdf.cell(w=30, h=8, txt=str(row["total_price"]), border=1, ln=1)
total_sum = df["total_price"].sum()
pdf.set_font(family="Times", size=10)
pdf.set_text_color(80, 80, 80)
pdf.cell(w=30, h=8, border=1)
pdf.cell(w=60, h=8, border=1)
pdf.cell(w=30, h=8, border=1)
pdf.cell(w=30, h=8, border=1)
pdf.cell(w=30, h=8, txt=str(total_sum), border=1, ln=1)
# Add total sum sentence
pdf.set_font(family="Times", size=10)
pdf.cell(w=30, h=8, txt=f"The total price is {total_sum}", ln=1)
# Add company name and logo
pdf.set_font(family="Times", size=14, style="B")
pdf.cell(w=18, h=8, txt=f"Python")
pdf.image("python.png", w=10)
pdf.output(f"PDFs/{filename}.pdf")