|
|
|
@ -1,9 +1,12 @@
|
|
|
|
|
import logging
|
|
|
|
|
|
|
|
|
|
from docx.oxml.ns import qn
|
|
|
|
|
from docx.shared import Pt
|
|
|
|
|
|
|
|
|
|
# 获取日志记录器
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#excluded_list = ['{{year}}','{{month}}', '{{day}}']
|
|
|
|
|
# 将文档中的字符串变量替换成提取内容
|
|
|
|
|
def replace_text_in_paragraph(paragraph, old_text, new_text):
|
|
|
|
|
try:
|
|
|
|
@ -12,7 +15,11 @@ def replace_text_in_paragraph(paragraph, old_text, new_text):
|
|
|
|
|
for run in paragraph.runs:
|
|
|
|
|
if old_text in run.text:
|
|
|
|
|
run.text = run.text.replace(old_text, new_text)
|
|
|
|
|
|
|
|
|
|
if old_text.startswith('{{') :
|
|
|
|
|
run.font.name = "Times New Roman"
|
|
|
|
|
#run.font.name = "仿宋"
|
|
|
|
|
run._element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋')
|
|
|
|
|
run.font.size = Pt(16)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.exception(f"替换段落里的文本失败:{e}")
|
|
|
|
|
print(f"替换段落里的文本失败:{e}")
|
|
|
|
@ -25,7 +32,6 @@ def replace_text_in_docx(doc, replacements):
|
|
|
|
|
for paragraph in doc.paragraphs:
|
|
|
|
|
for old_text, new_text in replacements.items():
|
|
|
|
|
replace_text_in_paragraph(paragraph, old_text, new_text)
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.exception(f"替换段落中的文本失败:{e}")
|
|
|
|
|
print(f"替换段落中的文本失败:{e}")
|
|
|
|
|