【问题标题】:how to add header and footer in doc with pywin32?如何使用pywin32在文档中添加页眉和页脚?
【发布时间】:2023-03-29 06:23:02
【问题描述】:

我知道如何从 word doc 中获取页眉和页脚,但我想做的是操纵这些页眉和页脚(例如添加更多文本或从页眉或页脚中删除现有文本)。我的问题是如何使用 python 程序和 pywin32 操作页眉和页脚?请列出一些方法,以便我可以操作页眉和页脚。 提前谢谢你。

import win32com.client as win32
word = win32.Dispatch("Word.Application")
word.Visible = 0
word.Documents.Open("a.docx")
doc = word.ActiveDocument
footer = doc.sections[0].footers[0]    #get footer
header = doc.sections[0].headers[0]    #get header
print(str(header)+" "+str(footer))    #printing both

输出: 页眉页脚

【问题讨论】:

    标签: python header footer pywin32 doc


    【解决方案1】:

    这对我有用。 请记住,根据您的需要,格式可能需要调整。

    doc = word.ActiveDocument       
    sections = doc.Sections
    for section in sections:
    
        headersCollection = section.Headers
    
        for header in headersCollection:
            header.Range.Text = header.Range.Text + "hello world"
            print(header.Range.Text)
    

    【讨论】:

    • 页脚呢?那会以同样的方式工作吗?请编辑您的答案以包含该内容
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-09
    • 2010-10-08
    • 1970-01-01
    • 2020-05-04
    • 2013-09-05
    • 2011-11-27
    • 1970-01-01
    相关资源
    最近更新 更多