Python-docx 能处理页眉页脚吗?

RT python 白板,想用python处理word文件的页眉页脚,python-docx 支持吗?

  for section in doc_obj.sections:
        header = section.header
        footer = section.footer
        if header is not None:
            print (header.text)
        if footer is not None:
            print(footer.text)

AttributeError: 'Section' object has no attribute 'header'

可以
http://python-docx.readthedocs.io/en/latest/dev/analysis/features/header.html?highlight=footer

可以的 1楼地址可行 望楼主采纳

Python 大神在哪里?自己顶一下

 doc.Sections[0].Headers[0].Range.Find.Execute(old_value, False, False, False, False, False, True, 1, False, new_value, 2)

自己搞定,暂时在win系统下使用

AttributeError: 'Section' object has no attribute 'header',我也遇到这问题了,需要改设置文件吗,还是什么的

有两个问题:
1. section貌似要用section[0],具体为什么我也在研究
2. _header里面貌似没有text这个对象,要加paragraphs[0],然后再调用text

我写的是:

file =  Document(path)
print(file.sections[0].header.paragraphs[0].text)

输出结果为:
第二章 相关技术介绍

输出确实为file文档里面某一页的页眉,但具体为什么是这一页还没明白,希望能帮到你。

实测下来应该是 import的包的问题
如果同时安装了 docx和 python-docx可能会引发上述问题
建议先
pip uninstall docx
pip uninstall python-docx
先把l两个word编辑的包都给删了

然后
pip install python-docx
重新安装 python-docx