TheDarkLord69696969
commited on
Commit
•
f763e99
1
Parent(s):
12f6a20
Upload translate_fill.py
Browse files- translate_fill.py +116 -0
translate_fill.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from docx import Document
|
2 |
+
import os
|
3 |
+
import sys
|
4 |
+
|
5 |
+
import languages
|
6 |
+
def docx_replace(doc, data):
|
7 |
+
paragraphs = list(doc.paragraphs)
|
8 |
+
for t in doc.tables:
|
9 |
+
for row in t.rows:
|
10 |
+
for cell in row.cells:
|
11 |
+
for paragraph in cell.paragraphs:
|
12 |
+
paragraphs.append(paragraph)
|
13 |
+
|
14 |
+
for key, val in data.items():
|
15 |
+
for p in paragraphs:
|
16 |
+
#key_name = '${{{}}}'.format(key) # I'm using placeholders in the form ${PlaceholderName}
|
17 |
+
key_name = key
|
18 |
+
if key_name in p.text:
|
19 |
+
#print(f'old one {p.text}')
|
20 |
+
inline = p.runs
|
21 |
+
# Replace strings and retain the same style.
|
22 |
+
# The text to be replaced can be split over several runs so
|
23 |
+
# search through, identify which runs need to have text replaced
|
24 |
+
# then replace the text in those identified
|
25 |
+
started = False
|
26 |
+
key_index = 0
|
27 |
+
# found_runs is a list of (inline index, index of match, length of match)
|
28 |
+
found_runs = list()
|
29 |
+
found_all = False
|
30 |
+
replace_done = False
|
31 |
+
for i in range(len(inline)):
|
32 |
+
|
33 |
+
# case 1: found in single run so short circuit the replace
|
34 |
+
if key_name in inline[i].text and not started:
|
35 |
+
found_runs.append((i, inline[i].text.find(key_name), len(key_name)))
|
36 |
+
text = inline[i].text.replace(key_name, str(val))
|
37 |
+
inline[i].text = text
|
38 |
+
replace_done = True
|
39 |
+
found_all = True
|
40 |
+
break
|
41 |
+
|
42 |
+
if key_name[key_index] not in inline[i].text and not started:
|
43 |
+
# keep looking ...
|
44 |
+
continue
|
45 |
+
|
46 |
+
# case 2: search for partial text, find first run
|
47 |
+
if key_name[key_index] in inline[i].text and inline[i].text[-1] in key_name and not started:
|
48 |
+
# check sequence
|
49 |
+
start_index = inline[i].text.find(key_name[key_index])
|
50 |
+
check_length = len(inline[i].text)
|
51 |
+
for text_index in range(start_index, check_length):
|
52 |
+
if inline[i].text[text_index] != key_name[key_index]:
|
53 |
+
# no match so must be false positive
|
54 |
+
break
|
55 |
+
if key_index == 0:
|
56 |
+
started = True
|
57 |
+
chars_found = check_length - start_index
|
58 |
+
key_index += chars_found
|
59 |
+
found_runs.append((i, start_index, chars_found))
|
60 |
+
if key_index != len(key_name):
|
61 |
+
continue
|
62 |
+
else:
|
63 |
+
# found all chars in key_name
|
64 |
+
found_all = True
|
65 |
+
break
|
66 |
+
|
67 |
+
# case 2: search for partial text, find subsequent run
|
68 |
+
if key_name[key_index] in inline[i].text and started and not found_all:
|
69 |
+
# check sequence
|
70 |
+
chars_found = 0
|
71 |
+
check_length = len(inline[i].text)
|
72 |
+
for text_index in range(0, check_length):
|
73 |
+
if inline[i].text[text_index] == key_name[key_index]:
|
74 |
+
key_index += 1
|
75 |
+
chars_found += 1
|
76 |
+
else:
|
77 |
+
break
|
78 |
+
# no match so must be end
|
79 |
+
found_runs.append((i, 0, chars_found))
|
80 |
+
if key_index == len(key_name):
|
81 |
+
found_all = True
|
82 |
+
break
|
83 |
+
|
84 |
+
if found_all and not replace_done:
|
85 |
+
for i, item in enumerate(found_runs):
|
86 |
+
index, start, length = [t for t in item]
|
87 |
+
if i == 0:
|
88 |
+
text = inline[index].text.replace(inline[index].text[start:start + length], str(val))
|
89 |
+
inline[index].text = text
|
90 |
+
else:
|
91 |
+
text = inline[index].text.replace(inline[index].text[start:start + length], '')
|
92 |
+
inline[index].text = text
|
93 |
+
#print(p.text)
|
94 |
+
|
95 |
+
|
96 |
+
def translate_fill(document_name,output_file, src, trg):
|
97 |
+
template_document = Document(document_name)
|
98 |
+
|
99 |
+
variables = {}
|
100 |
+
for paragraph in template_document.paragraphs:
|
101 |
+
if(paragraph.text.strip() != ""):
|
102 |
+
variables[paragraph.text] = languages(paragraph.text, src, trg)
|
103 |
+
|
104 |
+
for t in template_document.tables:
|
105 |
+
for row in t.rows:
|
106 |
+
for cell in row.cells:
|
107 |
+
for paragraph in cell.paragraphs:
|
108 |
+
if(paragraph.text.strip() != ""):
|
109 |
+
variables[paragraph.text] = languages(paragraph.text, src, trg)
|
110 |
+
|
111 |
+
docx_replace(template_document, variables)
|
112 |
+
template_document.save(output_file)
|
113 |
+
return variables
|
114 |
+
|
115 |
+
sys.modules[__name__] = translate_fill
|
116 |
+
## args = translate_fill(document_name, output_name, src, trg)
|