-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_req_formatter.py
More file actions
90 lines (70 loc) · 3.05 KB
/
main_req_formatter.py
File metadata and controls
90 lines (70 loc) · 3.05 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
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 re
from docx import Document
from docx.oxml import OxmlElement, ns
from docx.shared import Pt
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.enum.text import WD_LINE_SPACING
from docx.shared import RGBColor
from docx.shared import Cm
class MainRequirementsFormatter:
@staticmethod
def number_pages(doc):
for section in doc.sections:
section.footer.is_linked_to_previous = True
new_paragraph = doc.sections[0].footer.add_paragraph()
new_run = new_paragraph.add_run()
new_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
doc.sections[0].different_first_page_header_footer = True
fld_char1 = OxmlElement('w:fldChar')
fld_char1.set(ns.qn('w:fldCharType'), 'begin')
instr_text = OxmlElement('w:instrText')
instr_text.set(ns.qn('xml:space'), 'preserve')
instr_text.text = "PAGE"
fld_char2 = OxmlElement('w:fldChar')
fld_char2.set(ns.qn('w:fldCharType'), 'end')
new_run._r.append(fld_char1)
new_run._r.append(instr_text)
new_run._r.append(fld_char2)
@staticmethod
def change_font(font_name: str, paragraph):
for run in paragraph.runs:
run.font.name = font_name
@staticmethod
def change_font_size(font_size: int, paragraph):
for run in paragraph.runs:
run.font.size = Pt(font_size)
@staticmethod
def change_font_color(color: RGBColor, paragraph):
for run in paragraph.runs:
run.font.color.rgb = color
@staticmethod
def change_line_spacing(spacing: WD_LINE_SPACING, paragraph):
paragraph.paragraph_format.line_spacing_rule = spacing
@staticmethod
def change_margins(left_cm, right_cm, top_cm, bottom_cm, doc: Document):
for section in doc.sections:
section.left_margin = Cm(left_cm)
section.right_margin = Cm(right_cm)
section.top_margin = Cm(top_cm)
section.bottom_margin = Cm(bottom_cm)
@staticmethod
def change_left_paragraph_indentation(indentation_cm, paragraph):
paragraph.paragraph_format.left_indent = Cm(indentation_cm)
@staticmethod
def change_title_page_year(doc: Document, year: str):
regex = re.compile(r"20[0-9][0-9]")
for p in doc.paragraphs:
for r in p.runs:
if regex.search(r.text):
r.text = f'\n{year}'
return
@staticmethod
def format_document(doc: Document):
for paragraph in doc.paragraphs:
MainRequirementsFormatter.change_font('Times New Roman', paragraph)
MainRequirementsFormatter.change_font_size(14, paragraph)
MainRequirementsFormatter.change_font_color(RGBColor(0, 0, 0), paragraph)
MainRequirementsFormatter.change_line_spacing(WD_LINE_SPACING.ONE_POINT_FIVE, paragraph)
MainRequirementsFormatter.change_left_paragraph_indentation(1.25, paragraph)
MainRequirementsFormatter.number_pages(doc)
MainRequirementsFormatter.change_margins(3, 1.5, 2, 2, doc)