1

I've added a Field 'Page' to my code using code:

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 = "Right-click to update field."
    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, name):
    """ add a number range field to a run
    """
    _add_field(run, r'SEQ %s \* ARABIC' % name)

Now as section is on page-3 of the document its getting number 3 and further pages get the page number as 4,5,...

How to reset this starting page number for that section as 1 from 3 using python-docx?

Here is the image of doing it from MSWord: Solution from MSWord

P.Natu
  • 131
  • 1
  • 3
  • 12
  • I'm not familiar with the programming language, but I think I can help you find the answer, as your code appears to mirror the Word Open XML fairly closely. The page number information is stored in the section break *following* the section content. The XML for a section break with renumbering set to 1 (look for `w:pgNumType `): `` – Cindy Meister Jul 24 '19 at 11:45
  • So it would be a matter of appending/inserting the w:pgNumType element. Note that the *order* of elements is often important in the Word Open XML Schema. – Cindy Meister Jul 24 '19 at 11:46

1 Answers1

0
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from docx.text.paragraph import Paragraph

def create_page_number_type(start_num: int=1):  # type: ignore
    num_type = OxmlElement('w:pgNumType')
    num_type.set(qn('w:start'), str(start_num))
    return num_type

def make_page_number(p: Paragraph) -> None:
    run = p.add_run()
    fldChar1 = OxmlElement("w:fldChar")
    fldChar1.set(qn("w:fldCharType"), "begin")

    instrText = OxmlElement("w:instrText")
    instrText.set(qn("xml:space"), "preserve")
    instrText.text = "PAGE"

    fldChar2 = OxmlElement("w:fldChar")
    fldChar2.set(qn("w:fldCharType"), "end")

    r_element = run._r  # noqa: protected-access
    r_element.append(fldChar1)
    r_element.append(instrText)
    r_element.append(fldChar2)
    p.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER


content_section: Section = document.sections[-1]
pg_num_type = create_page_number_type(start_num=1)
content_section._sectPr.append(pg_num_type) # noqa: protected-access

content_section_footer = content_section.footer
content_section_footer.is_linked_to_previous = False
make_page_number(content_section_footer.paragraphs[0])
whtsky
  • 351
  • 1
  • 4