在 python-docx 中设置 pgNumType 属性 无效
Setting pgNumType property in python-docx is without effect
我正在尝试使用 python-docx
在 word 文档中设置页码。我找到了一个属性 pgNumType
(pageNumberType),我正在使用此代码进行设置:
document = Document()
section = document.add_section(WD_SECTION.CONTINUOUS)
sections = document.sections
sectPr = sections[0]._sectPr
pgNumType = OxmlElement('w:pgNumType')
pgNumType.set(qn('w:fmt'), 'decimal')
pgNumType.set(qn('w:start'), '1')
sectPr.append(pgNumType)
此代码不执行任何操作,输出文档中没有页码。我对行号的 lnNumType
属性做了同样的事情,它工作正常。那么 pgNumType
属性是什么呢?程序执行无误,所以该属性存在。但是有谁知道为什么没有效果吗?
虽然您的页面样式设置很好,但它不会自动在文档中的任何位置插入页码字段。通常,页码出现在 header 或页脚中,但不幸的是,python-docx 目前不支持 header、页脚或字段。前两个似乎是正在进行的工作:https://github.com/python-openxml/python-docx/issues/104。
链接的问题提到了一些解决方法。我发现最可靠的方法是创建一个空白文档,其中的 header 和页脚完全按照您在 MS Word 中想要的方式设置。然后,您可以加载并附加到该文档,而不是 docx.Document
returns.
的默认模板
此技术暗示是在 official documentation:
中编辑 header 的建议方法
A lot of how a document looks is determined by the parts that are left when you delete all the content. Things like styles and page headers and footers are contained separately from the main content, allowing you to place a good deal of customization in your starting document that then appears in the document you produce.
@T.Poe
我正在处理同样的问题。
new_section = document.add_section() # Added new section for assigning different footer on each page.
sectPr = new_section._sectPr
pgNumType = OxmlElement('w:pgNumType')
pgNumType.set(qn('w:fmt'), 'decimal')
pgNumType.set(qn('w:start'), '1')
sectPr.append(pgNumType)
new_footer = new_section.footer # Get footer-area of the recent section in document
new_footer.is_linked_to_previous = False
footer_para = new_footer.add_paragraph()
run_footer = footer_para.add_run("Your footer here")
_add_number_range(run_footer)
font = run_footer.font
font.name = 'Arial'
font.size = Pt(8)
footer_para.paragraph_format.page_break_before = True
这对我有用:)
我不知道什么不适合你。我刚刚创建了一个新部分
我用来定义字段的部分函数代码如下:
def _add_field(run, field):
""" add a field to a run
"""
fldChar1 = OxmlElement('w:fldChar') # creates a new element
fldChar1.set(qn('w:fldCharType'), 'begin') # sets attribute on element
instrText = OxmlElement('w:instrText')
instrText.set(qn('xml:space'), 'preserve') # sets attribute on element
instrText.text = field
fldChar2 = OxmlElement('w:fldChar')
fldChar2.set(qn('w:fldCharType'), 'separate')
t = OxmlElement('w:t')
t.text = "Seq"
fldChar2.append(t)
fldChar4 = OxmlElement('w:fldChar')
fldChar4.set(qn('w:fldCharType'), 'end')
r_element = run._r
r_element.append(fldChar1)
r_element.append(instrText)
r_element.append(fldChar2)
r_element.append(fldChar4)
def _add_number_range(run):
""" add a number range field to a run
"""
_add_field(run, r'Page')
我正在尝试使用 python-docx
在 word 文档中设置页码。我找到了一个属性 pgNumType
(pageNumberType),我正在使用此代码进行设置:
document = Document()
section = document.add_section(WD_SECTION.CONTINUOUS)
sections = document.sections
sectPr = sections[0]._sectPr
pgNumType = OxmlElement('w:pgNumType')
pgNumType.set(qn('w:fmt'), 'decimal')
pgNumType.set(qn('w:start'), '1')
sectPr.append(pgNumType)
此代码不执行任何操作,输出文档中没有页码。我对行号的 lnNumType
属性做了同样的事情,它工作正常。那么 pgNumType
属性是什么呢?程序执行无误,所以该属性存在。但是有谁知道为什么没有效果吗?
虽然您的页面样式设置很好,但它不会自动在文档中的任何位置插入页码字段。通常,页码出现在 header 或页脚中,但不幸的是,python-docx 目前不支持 header、页脚或字段。前两个似乎是正在进行的工作:https://github.com/python-openxml/python-docx/issues/104。
链接的问题提到了一些解决方法。我发现最可靠的方法是创建一个空白文档,其中的 header 和页脚完全按照您在 MS Word 中想要的方式设置。然后,您可以加载并附加到该文档,而不是 docx.Document
returns.
此技术暗示是在 official documentation:
中编辑 header 的建议方法A lot of how a document looks is determined by the parts that are left when you delete all the content. Things like styles and page headers and footers are contained separately from the main content, allowing you to place a good deal of customization in your starting document that then appears in the document you produce.
@T.Poe 我正在处理同样的问题。
new_section = document.add_section() # Added new section for assigning different footer on each page.
sectPr = new_section._sectPr
pgNumType = OxmlElement('w:pgNumType')
pgNumType.set(qn('w:fmt'), 'decimal')
pgNumType.set(qn('w:start'), '1')
sectPr.append(pgNumType)
new_footer = new_section.footer # Get footer-area of the recent section in document
new_footer.is_linked_to_previous = False
footer_para = new_footer.add_paragraph()
run_footer = footer_para.add_run("Your footer here")
_add_number_range(run_footer)
font = run_footer.font
font.name = 'Arial'
font.size = Pt(8)
footer_para.paragraph_format.page_break_before = True
这对我有用:) 我不知道什么不适合你。我刚刚创建了一个新部分
我用来定义字段的部分函数代码如下:
def _add_field(run, field):
""" add a field to a run
"""
fldChar1 = OxmlElement('w:fldChar') # creates a new element
fldChar1.set(qn('w:fldCharType'), 'begin') # sets attribute on element
instrText = OxmlElement('w:instrText')
instrText.set(qn('xml:space'), 'preserve') # sets attribute on element
instrText.text = field
fldChar2 = OxmlElement('w:fldChar')
fldChar2.set(qn('w:fldCharType'), 'separate')
t = OxmlElement('w:t')
t.text = "Seq"
fldChar2.append(t)
fldChar4 = OxmlElement('w:fldChar')
fldChar4.set(qn('w:fldCharType'), 'end')
r_element = run._r
r_element.append(fldChar1)
r_element.append(instrText)
r_element.append(fldChar2)
r_element.append(fldChar4)
def _add_number_range(run):
""" add a number range field to a run
"""
_add_field(run, r'Page')