-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordreplace.py
67 lines (56 loc) · 2.37 KB
/
wordreplace.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
""" Replace text in a Word document
"""
import os
import logging # noqa: E402
from docx import Document
from doc2docx import convert
from config import replace_substring
def replace_word(doc_path: str, new_path: str, data: dict) -> None:
""" Replace text in a Word document
"""
if doc_path.endswith('doc'):
docx_path = os.path.splitext(doc_path)[0] + '.docx'
print(f'Converting {doc_path} to {docx_path}')
convert(doc_path, docx_path)
logging.info(f'Converting {doc_path} to {docx_path}')
doc_path = docx_path
doc = Document(doc_path)
replaced = {value: 0 for value in data.values()}
verbose = False
for paragraph in doc.paragraphs:
for key, value in data.items():
txt_old = paragraph.text
paragraph.text, n = replace_substring(
paragraph.text, key, value)
replaced[value] += n
print(f'{txt_old} -> {paragraph.text}') if verbose else None
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
for paragraph in cell.paragraphs:
for key, value in data.items():
txt_old = paragraph.text
paragraph.text, n = replace_substring(
paragraph.text, key, value)
replaced[value] += n
print(
f'{txt_old} -> {paragraph.text}') if verbose else None
for section in doc.sections:
for paragraph in section.footer.paragraphs:
for key, value in data.items():
txt_old = paragraph.text
paragraph.text, n = replace_substring(
paragraph.text, key, value)
replaced[value] += n
print(f'{txt_old} -> {paragraph.text}') if verbose else None
for paragraph in section.header.paragraphs:
for key, value in data.items():
txt_old = paragraph.text
paragraph.text, n = replace_substring(
paragraph.text, key, value)
replaced[value] += n
print(f'{txt_old} -> {paragraph.text}') if verbose else None
logging.info(f'Replacements:\n {replaced}')
doc.save(new_path)
logging.info(f'New docx file saved to {new_path}')
print(f'New docx file saved to {new_path}')