0

I am trying to change the page orientation to landscape using Quarto when generating docx (based on this SO). Although it changes the orientation, the footer is misplaced.

In the document which is generated by quarto (see screenshot), there is a footer in page 5 but the same footer is missed in all other pages. Also, one can notice there are arbitrary number of sections. How can I make that footer reflect in all pages (including dynamic page numbers)?

see screenshot

Below is the code I tried,

from docx import Document
document = Document(r"C:\dev\poetry-demo\poetry_demo\test.docx")
n_sections = len(document.sections)
document.sections[0].footer = document.sections[n_sections].footer
# Attribute error

Note: Quarto works based on template and I can extract the footer.xml from it. Hence, I am okay if one can apply that ooxml for all sections.

Selva
  • 951
  • 7
  • 23

1 Answers1

0

I couldn't assign the section 3 footer on other section because I was getting malformed word content while opening.

However, below code worked.

footer_xml = """
<w:ftr xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships">
<w:p>
<w:r>
<w:t>This is a custom footer.</w:t>
</w:r>
</w:p>
</w:ftr>
"""

document = Document('test.docx')
sections = document.sections
for section in sections:
    section.footer._definition._element = parse_xml(footer_xml) #sections[-1].footer._definition._element
document.save('test1.docx')
Selva
  • 951
  • 7
  • 23