简体   繁体   English

使用win32com编辑MS Word标头

[英]Editing MS Word header with win32com

I'm trying to edit header of a MS Word document that has existing header using win32com . 我正在尝试使用win32com编辑具有现有标题的MS Word文档的标题。
I tried this to edit page header: 我试图这样做编辑页面标题:

import win32com.client as win32

word = win32.gencache.EnsureDispatch('Word.Application')
doc=word.Documents.Open("C:\\a.docx")
word.Visible = True
word.ActiveDocument.Sections[0].Headers[win32.constants.wdHeaderFooterPrimary].Range.Text='test text'
word.ActiveDocument.Save()
doc.Close(False)
word.Application.Quit()

But it has no effect (header didn't changed at all)!! 但这没有效果(标题完全没有改变)!!
What is the correct way to edit MS Word header via win32com ? 通过win32com编辑MS Word标头的正确方法是什么?

Use parentheses instead of square brackets in this line, along with 1-based indexing. 在此行中使用括号而不是方括号,以及从1开始的索引。 Everything in COM is a function call or property. COM中的所有内容都是函数调用或属性。

word.ActiveDocument.Sections(1).Headers(win32.constants.wdHeaderFooterPrimary).Range.Text='test text'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM