-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_cover_creator.py
136 lines (107 loc) · 4.05 KB
/
pdf_cover_creator.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from fpdf import FPDF
import os
import csv
os.chdir('C:\\Users\\chapman4\\Desktop')
def create_filenames():
# Creating list of filenames in folder specified when setting directory above
filenames = [name.split(".")[0] for name in os.listdir(".") if name.endswith(".txt")]
return filenames
def create_metadata():
metadata = []
with open('zuck-metadata.csv', encoding="UTF-8") as csvfile:
fieldnames = ['record_id', 'participants', 'record_type', 'record_format',
'date', 'source', 'title', 'url', 'description']
reader = csv.DictReader(csvfile, fieldnames)
for row in reader:
record = {}
for name in fieldnames:
record[name] = row[name]
metadata.append(record)
return metadata
def create_pdf(row):
# Create instance of FPDF class
pdf = FPDF(format='Letter', unit='in')
# Set margin sizes
pdf.l_margin = 1
pdf.r_margin = 1
pdf.t_margin = .25
pdf.b_margin = .25
# Create first page
pdf.add_page()
# Effective page width and height (for centering purposes)
epw = pdf.w - 2*pdf.l_margin
#eph = pdf.h = 2*pdf.t_margin
# Font declaration
pdf.set_font('Helvetica', '', 14)
# Text height
th = pdf.font_size
# Initial line break needed for unknown reasons.
pdf.ln(2*th)
# Images
# Use absolute positioning. Specify image dimension or it will revert to 72dpi.
pdf.image('zuckbox.png', x=.39, y=.535, h=9.93, type='PNG')
pdf.image('zucklogo.png', x=1, y=1, h=1.133, type='PNG')
# Zuck Files URL
pdf.set_xy(4.86, 1.43) # Absolute positioning
pdf.cell(0, th, 'www.zuckerbergfiles.org') # Content cell (width, height, content)
pdf.ln(1.2) # Line break
# Title
pdf.set_font('Helvetica','B', 20)
pdf.multi_cell(epw, th*1.5, row['title'], align='C')
pdf.ln(.10)
# ID
pdf.set_font('Helvetica', '', 12)
pdf.cell(epw, th, row['record_id'], align='C')
pdf.ln(.72)
# Date
pdf.set_xy(1.25, 3.6)
pdf.set_font('Helvetica', 'B')
pdf.cell(1.1, th, 'Date: ', align='R')
pdf.set_font('Helvetica', style = '')
pdf.multi_cell(4.93, th, row['date'], align='L')
pdf.ln(.25)
# Description
pdf.set_x(1.25) # Absolute positioning
pdf.set_font('Helvetica', 'B') # Bold font for label
pdf.cell(1.1, th, 'Description: ', align='R') # Metadata field label
pdf.set_font('Helvetica', style = '') # Unbold font for contents
pdf.multi_cell(4.93, th, row['description'], align='L') # Metadata value contents
pdf.ln(.25)
# Participants
pdf.set_x(1.25)
pdf.set_font('Helvetica', 'B')
pdf.cell(1.1, th, 'Participants: ', align='R')
pdf.set_font('Helvetica', style = '')
pdf.multi_cell(4.93, th, row['participants'], align='L')
pdf.ln(.25)
# Source
pdf.set_x(1.25) # Reset L-R position
pdf.set_font('Helvetica', 'B')
pdf.cell(1.1, th, 'Source: ', align='R')
pdf.set_font('Helvetica', style = '')
pdf.multi_cell(4.93, th, row['source'], align='L')
pdf.ln(.25)
# Type
pdf.set_x(1.25)
pdf.set_font('Helvetica', 'B')
pdf.cell(1.1, th, 'Type: ', align='R')
pdf.set_font('Helvetica', style = '')
pdf.multi_cell(4.93, th, row['record_type'], align='L')
pdf.ln(.25)
# URL
pdf.set_x(1.25)
pdf.set_font('Helvetica', 'B')
pdf.cell(1.1, th, 'URL: ', align='R')
pdf.set_font('Helvetica', style = '')
pdf.multi_cell(4.93, th, row['url'], align='L')
# Write file
pdf.output(row['record_id'] + '_cover' + '.pdf', 'F')
def main():
filenames = create_filenames()
for row in create_metadata():
if row['record_id'] in filenames:
try:
create_pdf(row)
except UnicodeEncodeError:
print('UnicodeEncodeError: ' + row['record_id'])
main()