-
Notifications
You must be signed in to change notification settings - Fork 0
/
replace_docstyles.py
305 lines (258 loc) · 9.57 KB
/
replace_docstyles.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
from os import listdir, mkdir, path, system, getcwd, chdir, remove
import shutil
from docx import Document
from docx.shared import Pt
import argparse
def summarize_args(kws):
print("----------- Parameters ---------")
print("In directory: {}".format(kws['in']))
print("Out directory: {}".format(kws['out']))
print("Options:")
# print("\tAdd metadata table: {}".format("yes" if kws['table'] else "no"))
# print("\tDo annotations: {}".format("yes" if kws['annotations'] else "no"))
print("\tDo Milestones: {}".format("yes" if kws['milestones'] else "no"))
def stardardize_in_files(infld):
'''
Standardize the files in the in folder converting them to docx
:param infld:
:return:
'''
bakfld = path.join(infld, 'bak')
if not path.exists(bakfld):
mkdir(bakfld)
cwd = getcwd()
for f in listdir(infld):
if f.endswith('.txt') or f.endswith('.rtf'):
newf = f.replace('.txt', '-temp.docx').replace('.rtf', '-temp.docx')
chdir(infld)
system('textutil -convert docx -output "{}" "{}"'.format(newf, f))
chdir(cwd)
shutil.move(path.join(infld, f), path.join(bakfld, f))
elif f.endswith('.docx') and not f.endswith('-temp.docx'):
newf = f.replace('.docx', '-temp.docx')
shutil.move(path.join(infld, f), path.join(infld, newf))
def clean_out_folder(outd):
for f in listdir(outd):
if f.endswith('-temp.docx'):
remove(path.join(outd, f))
def read_in_text(dir, fnm):
'''
Reads in plain text to put in Word doc. Original idea, maybe deprecated
:param dir: String
:param fnm: String
:return: List
'''
try:
with open("{}/{}".format(dir, fnm), 'r') as infl:
alltxt = infl.readlines()
except FileNotFoundError as fnfe:
print(fnfe)
alltxt = False
return alltxt
def read_in_doc(fnm):
'''
Read in a Word docx with file name
:param fnm: String
:return: docx.document.Document
'''
doc = Document(fnm)
return doc
def update_doc(infnm, outfnm, mark_miles=True):
"""
Copies unstyled word document paragraphs to a "template" document which is a Word docx with the correct styles
:param infnm:
:param outfnm:
:param include_table:
:param do_annots:
:param mark_miles:
:return: docx.document.Document
"""
indoc = read_in_doc(infnm)
indoc = fix_milestones(indoc)
outdoc = get_template_doc(outfnm, True)
pstyl = get_style(outdoc)
found_body = False
for ind, p in enumerate(indoc.paragraphs):
if not found_body:
if "0 Body" in p.style.name:
found_body = True
continue
outpara = outdoc.add_paragraph(p.text, pstyl)
if mark_miles:
pgst = get_style(outdoc, 'Page Number Print Edition')
lnst = get_style(outdoc, 'Line Number Print')
mark_milestones(outpara, pgst, lnst)
set_tib_font(outdoc)
outdoc.save(outfnm)
print("Saved to {}".format(outfnm))
return outdoc
def fix_milestones(adoc):
'''
If line milestones do not have page number in them
:param adoc:
:return:
'''
last_page = False
for p in adoc.paragraphs:
for rn in p.runs:
rsty = rn.style.name
if "Page Number Print" in rsty:
last_page = rn.text.strip('[]')
if "Line Number Print" in rsty:
rtxt = rn.text.strip('[]')
if '.' not in rtxt:
rn.text = '[{}.{}]'.format(last_page, rtxt)
return adoc
def do_annotations(p, annotstyl):
'''
Finds text betwee « and » and applies the Annotations style
:param p:
:param annotstyl:
:return: Null (manipulation done by reference)
'''
print("\tApplying styles to annotations!")
# Other option for annot_open and _close: < >
annot_open = '«'
annot_close = '»'
newruns = []
for rn in p.runs:
rtxt = rn.text
pts = rtxt.split(annot_open)
if len(pts) > 1:
newruns.append((pts[0], ''))
for subrn in pts[1:]:
rnpts = subrn.split(annot_close)
if len(rnpts) == 1:
print("No closing annotation marker")
newruns.append((subrn, annotstyl))
else:
newruns.append((rnpts[0], annotstyl))
runtxt = rnpts[1]
if len(rnpts) > 2:
print("more than two closing annotation markers!")
runtxt = ''.join(rnpts[1:])
newruns.append((runtxt, ''))
else:
newruns.append((rn.text, rn.style))
p.clear()
for rdata in newruns:
p.add_run(rdata[0], rdata[1])
def mark_milestones(p, pgstyl, lnstyl):
print("\tApplying styles to milestones!")
# Iterate through runs in paragraph
newruns = []
for rn in p.runs:
currsty = rn.style # get run's current style
rntxt = rn.text # get run text
pts = rntxt.split('[') # split on milestone delimiter [
if len(pts) > 1: # if there are more than one parts, it has a bracket/milestone in it
for msrn in pts: # iterate through the parts from splitting on [
if ']' in msrn: # if the part has a close bracket ], then ...
rnpts = msrn.split(']') # split it on the ]
newms = '[' + rnpts[0] + ']' # rebuild the milestone string
sty = lnstyl if '.' in newms else pgstyl # choose the style based on whether there is a "."
newruns.append((newms, sty)) # add the milestone with the style
if len(rnpts) > 1: # if there's another part
newruns.append((rnpts[1], currsty)) # add it back with the run's original style
else:
newruns.append((msrn, currsty)) # Otherwise no milestone in this part, add back with style
else:
newruns.append((rn.text, rn.style))
p.clear()
for rdata in newruns:
p.add_run(rdata[0], rdata[1])
def get_template_doc(fnm, with_table=True):
'''
Get the template document that has all the styles in it
:param fnm:
:param with_table:
:return: docx.document.Document
'''
folder = 'resources'
tplnm = 'tibtext-styled-tpl.docx'
# print("Template: {}".format(tplnm))
tplpath = path.join(folder, tplnm)
doc = Document(tplpath)
parastyl = get_style(doc)
if not with_table:
for tbl in doc.tables:
tbl._element.getparent().remove(tbl._element)
for p in doc.paragraphs:
p._element.getparent().remove(p._element)
doc.save(fnm)
return doc
def get_style(doc, stynm="Paragraph"):
'''
Find a style in the doc by name, defaults to paragraph
:param doc:
:param stynm: The name of the style to find, defaults to paragraph
:return: docx.styles.styles.Styles
'''
for st in doc.styles:
if st.name == stynm:
return st
def get_title_p(tmpldoc):
'''
Returns the paragraph with the text "TITLE HERE" from template
:param tmpldoc: docx.Document
:return: docx.text.paragraph.Paragraph
'''
for p in tmpldoc.paragraphs:
ptxt = p.text
ptxt = ptxt.strip()
if ptxt == 'TITLE HERE':
return p
print("Title paragraph not found!")
return False
def set_tib_font(doc):
'''
Set the font in the document to Jomolhari
:param doc:
:return: Null (manipulation by reference)
'''
for p in doc.paragraphs:
for r in p.runs:
r.font.complex_script = True
r.font.name = "Jomolhari"
r.font.size = Pt(16)
def convert_files(ind, outd, milestones):
ct = 0
for fnm in [f for f in listdir(ind) if f.endswith('.docx')]:
print("Processing {}".format(fnm))
inf = path.join(ind, fnm)
outf = path.join(outd, fnm.replace('-temp', ''))
update_doc(inf, outf, milestones)
ct += 1
print("{} documents done!".format(ct))
parser = argparse.ArgumentParser(description='Copy old Word docs to template with new styles and table')
parser.add_argument('-i', '--in', default='workspace/in',
help='Where are the documents to convert?')
parser.add_argument('-o', '--out', default='workspace/out',
help='Where to save the converted documents')
# parser.add_argument('-t', '--table', action='store_true',
# help='Add metadata tables to documents')
# parser.add_argument('-a', '--annotations', action='store_true',
# help='Convert << and >> into annotation style')
parser.add_argument('-m', '--milestones', action='store_true',
help='Add styles to page and line milestones')
args = parser.parse_args()
if __name__ == "__main__":
'''
The main routine that runs the conversion.
'''
kwargs = vars(args)
if not kwargs['out']:
kwargs['out'] = kwargs['in']
indir = kwargs['in']
if not path.isdir(indir):
print("The In directory setting, {}, is not a directory".format(indir))
exit(0)
outdir = kwargs['out']
if not path.isdir(outdir):
print("The Out directory setting, {}, is not a directory".format(outdir))
exit(0)
summarize_args(kwargs)
convert_files(indir, outdir, kwargs['milestones'])
# print("Removing temp files ...")
# clean_out_folder(outdir)
print("Done")