-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_first_pages.py
67 lines (58 loc) · 2.4 KB
/
fix_first_pages.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
"""
A script to add first page milestones if missing from word document chunks
"""
import os
import argparse
from docx import Document
import re
from docx.text.run import Run
parser = argparse.ArgumentParser(description='Add first page milestones to all documents in folder')
parser.add_argument('-f', '--folder', default='workspace/out',
help='Folder with documents')
args = parser.parse_args()
if __name__ == "__main__":
mspattern = re.compile('\[(\d+)\.?(\d*)\]') # matches either a page or line milestone
kwargs = vars(args)
folder = kwargs['folder']
chngct = 0
print(f"Updating documents in {folder}:")
docs = os.listdir(folder)
docs.sort()
for docnm in docs:
if '.docx' not in docnm:
continue
print(docnm)
docpath = os.path.join(folder, docnm)
doc = Document(docpath)
p = doc.paragraphs[0]
change = False
for r in p.runs:
mtchs = re.search(mspattern, r.text)
if mtchs:
pg = mtchs.group(1)
ln = mtchs.group(2)
print(pg, ln)
if ln == "" or int(ln) > 2:
print("problem")
elif int(ln) == 2:
# If first milestone is line 2, then insert page and line 1 Milestone at beginning of doc
new_run_element = p._element._new_r()
p.runs[0]._element.addprevious(new_run_element)
new_run = Run(new_run_element, p.runs[0]._parent)
new_run.text = r.text.replace('.2', '.1')
new_run.style = r.style
new_run.font.name = "Microsoft Himalaya (Body CS)"
new_run.font.size = r.style.font.size
# Insert Page Milestone
new_run_element = p._element._new_r()
p.runs[0]._element.addprevious(new_run_element)
new_run = Run(new_run_element, p.runs[0]._parent)
new_run.text = r.text.replace('.2', '')
new_run.style = "Page Number Print Edition"
new_run.font.name = "Microsoft Himalaya (Body CS)"
new_run.font.size = r.style.font.size
change = True
break
if change:
newdocpath = os.path.join(folder, f"{docnm}-fixed.docx")
doc.save(newdocpath)