|
|
Python代码。。。
from docx import Document
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
def add_simple_field(paragraph, field_code):
fld = OxmlElement('w:fldSimple')
fld.set(qn('w:instr'), field_code)
paragraph._p.append(fld)
doc = Document(
r'K:\excelhome\第02讲 研究物质性质的基本方法(教师版)-【帮课堂】'
r'2022-2023学年高一化学同步精品讲义(鲁科2019必修第一册 ).docx'
)
section = doc.sections[0]
footer = section.footer
# 清空页脚并插入字段
footer.paragraphs[0].clear()
p = footer.paragraphs[0]
p.add_run("第 ")
add_simple_field(p, 'PAGE')
p.add_run(" 页 共 ")
add_simple_field(p, 'NUMPAGES')
p.add_run(" 页")
p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
doc.save(r'K:\excelhome\修改后的文档.docx')
print('已经添加完成。。。') |
|