-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv2pdf.py
90 lines (62 loc) · 3.28 KB
/
csv2pdf.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
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import csv
from fpdf import FPDF
def convert(source:str, destination:str, orientation="P", delimiter=",",
font=None, headerfont=None, align="C", size=8, headersize=10) -> None:
"""
# CONVERTS A CSV FILE TO PDF FILE ( .csv ➜ .pdf )
:param `source`: `***str*** : ***Required*** : The file path of the CSV FILE to be converted.
:param `destination`: ***str*** : ***Required*** :The file path of the PDF FILE to be generated.
:param `orientation`: ***str*** : *Optional* : The orientation in which the PDF will be created. ***Default ➜ "P"***
***Possible Values** ➜ **'P' ➣ Potrait** --- **'L' ➣ Landscape***
:param `delimiter`: ***str*** : *Optional* : The delimiter to be used for reading the CSV FILE. ***Default ➜ ","***
:param `font`: ***str*** : *Optional* : Path of the font to be used for the CSV data. ***Default ➜ None***
:param `headerfont`: ***str*** : *Optional* : Path of the font to be used for the CSV headers. ***Default ➜ None***
:param `align`: ***str*** : *Optional* : Alignment for the cells in PDF. ***Default ➜ "C"***
***Possible Values** ➜ **'J' ➣ Justify** --- **'C' ➣ Center** --- **'L ➣ Left** --- **'R' ➣ Right***
:param `size`: ***int*** : *Optional* : Specify the font size for the CSV data. ***Default size ➜ 8***
:param `headersize`: ***int*** : *Optional* : Specify the font size for the CSV header. ***Default size ➜ 10***
"""
if orientation not in ["P", "L"]:
raise Exception("Orientation Error: Invalid orientation parameter!\
\nExpected values: 'P' ➣ Potrait | 'L ➣ Landscape")
if align not in ["J", "C", "L", "R"]:
raise Exception("Alignment Error: Invalid alignment parameter!\
\nExpected values: 'J' ➣ Justify | 'C' ➣ Center | 'L ➣ Left | 'R' ➣ Right")
if not (isinstance(size, int) and isinstance(headersize, int)):
raise Exception("Type Error: Font Size should be of int data type")
PDF = FPDF(orientation)
PDF.add_page()
with open(source) as CSV:
data = [row for row in csv.reader(CSV, delimiter =delimiter )]
header = data[0]
rows = data[1:]
max = len(header)
for row in rows:
if len(row) > max:
max = len(row)
header.extend(list(" "*(max - len(header))))
for row in rows:
row.extend(list(" "*(max - len(row))))
if headerfont == None:
PDF.set_font("Courier", "B", size=size)
else:
PDF.add_font("header-font", "", font, uni=True)
PDF.set_font("header-font", size=size)
line_height = PDF.font_size * 2.5
col_width = PDF.epw / max
for cell in header:
PDF.multi_cell(col_width, line_height, cell, align=align, border=1,
ln=3, max_line_height=PDF.font_size)
PDF.ln(line_height)
if font == None:
PDF.set_font("Courier", size=size)
else:
PDF.add_font("normal-font", "", font, uni=True)
PDF.set_font("normal-font", size=size)
line_height = PDF.font_size * 2.5
for cells in rows:
for cell_value in cells:
PDF.multi_cell(col_width, line_height, cell_value, align=align, border=1,
ln=3, max_line_height=PDF.font_size)
PDF.ln(line_height)
PDF.output(destination)