-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwrite_word_document.py
51 lines (40 loc) · 1.61 KB
/
write_word_document.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
'''
Author: Brian Mukeswe
Contact: [email protected]
This script writes a customized MSWord report based on a provided template.
'''
from docx import Document
import pandas as pd
# set inputs
document = Document("IR_Template.docx")
normal_style = document.paragraphs[12].style
preamble = document.paragraphs[13].style
questions = document.paragraphs[16].style
question_style = document.paragraphs[17].style
def clean_ref(text):
tokens = text.split(",")
new_text = ''
for token in tokens:
temp = token.strip()
if "Schedule" in temp:
temp = temp + "-Transmission System Plan"
new_text = new_text + temp + ", "
return new_text
def write_line(line, irs, outdoc):
ref = "Ref:\t"+clean_ref(irs.Reference.iloc[line])
TBD_text = irs.Preamble.iloc[line]
comment = irs.Questions.iloc[line]
intro = "At the above noted reference, HONI stated the following:"
outdoc.add_paragraph("2-Staff-"+str(line+1), style=preamble)
outdoc.add_paragraph(ref, style=normal_style)
outdoc.add_paragraph("Preamble:", style=preamble)
outdoc.add_paragraph(intro, style=normal_style)
outdoc.add_paragraph(TBD_text, style=normal_style)
outdoc.add_paragraph("Questions:", style=preamble)
outdoc.add_paragraph(comment, style=question_style)
if __name__ == "__main__":
outdoc = Document("populated.docx")
irs = pd.read_excel("comments-referenced.xlsx", sheet_name="Sheet1")
for line in range(0, len(irs)):
write_line(line, irs, outdoc)
outdoc.save("populated1.docx")