-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (36 loc) · 1.49 KB
/
Copy pathmain.py
File metadata and controls
49 lines (36 loc) · 1.49 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
from docx import Document
import pandas as pd
import os
from docxcompose.composer import Composer
from lxml import etree
for NIGHT in ['Monday', 'Tuesday', 'Wednesday']:
TEMPLATE_FILE = 'Table Tent Template.docx'
NAMES_FILE = f'{NIGHT} Table Tents.csv'
OUTPUT_DIR = 'Output'
if not os.path.isfile(NAMES_FILE):
print(f"Could not find file \"{NAMES_FILE}\" in current directory; skipping {NIGHT}...")
continue
names = pd.read_csv(NAMES_FILE)
composer = None
print(f"Generating {NIGHT} night")
for index, row in names.iterrows():
name = row.iloc[0]
table = row.iloc[1]
firstName, lastName = name.split(' ', 1)
# if (' ' in lastName):
# print(f"firstName: {firstName}, lastName: {lastName}, table: {table}")
doc = Document(TEMPLATE_FILE)
xml_str = etree.tostring(doc.element, encoding='unicode')
new_xml_str = xml_str.replace('%FIRNAME%', firstName).replace('%LASTNAME%', lastName).replace('%TABLE_NUM%', table)
new_xml = etree.fromstring(new_xml_str.encode('utf-8'))
doc.element.clear()
for child in new_xml:
doc.element.append(child)
# doc.save(f"{OUTPUT_DIR}/id_{index}.docx")
if composer == None:
composer = Composer(doc)
else:
composer.append(doc)
print(f"{NIGHT}: Finished {index+1}/{len(names)}")
os.makedirs(OUTPUT_DIR, exist_ok=True)
composer.save(f"{OUTPUT_DIR}/{NIGHT} Combined.docx")