feature: Document.headers and footers
See original GitHub issueIt would be incredibly useful to be able to get the page headers through Document.headers, or similar.
For now, I use a very ugly hack to get alll header texts:
def get_headers(file):
import xml.etree.ElementTree as ET
document = Document(file)
namespace = dict(w="http://schemas.openxmlformats.org/wordprocessingml/2006/main")
header_uri = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/header"
headers = []
for rel, val in document._document_part._rels.iteritems():
if val._reltype == header_uri:
xml = val._target._blob
root = ET.fromstring(xml)
text_element = root.find(".//w:t", namespace)
if text_element.text is not None:
headers.append(text_element.text)
return headers
Issue Analytics
- State:
- Created 9 years ago
- Reactions:4
- Comments:45 (14 by maintainers)
Top Results From Across the Web
Word 2016: Headers and Footers - GCF Global
Headers and footers generally contain additional information such as page numbers, dates, an author's name, and footnotes, which can help keep longer documents...
Read more >Insert a header or footer - Microsoft Support
Go to Insert > Header or Footer. · Choose from a list of standard headers or footers, go to the list of Header...
Read more >Headers and Footers in Microsoft Word
Headers and footers are useful for including material that you want to appear on every page of a document such as your name,...
Read more >Using Headers and Footers - Suzanne S. Barnhill
As mentioned above, even if you think your document doesn't yet have a header or footer, you have to use View | Header...
Read more >Sections / Headers and Footers in Microsoft Word
A header or footer is text or other information such as graphics that is stored at the top or bottom of the page...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@scanny
I have lots of docx, and I want to replace these docx header’s text Is there any way to do it?
Thanks
This feature set is under active development by @eupharis on this pull request: #291
I don’t believe there’s a workaround at the moment, it would need to be a pretty hairy one given the involvement of new parts, but would certainly be possible. You can look at the analysis documents on that branch to get an idea what’s involved.
And of course encouragement/+1s on that PR is always helpful in keeping spirits high as we move the development along 😃