使用win32com编辑MS Word标头 [英] Editing MS Word header with win32com
本文介绍了使用win32com编辑MS Word标头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用win32com
编辑具有现有标题的MS Word文档的标题.
我尝试使用此方法来编辑页面标题:
I'm trying to edit header of a MS Word document that has existing header using win32com
.
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()
但是它没有任何作用(标题完全没有改变)!!
通过win32com编辑MS Word标头的正确方法是什么?
But it has no effect (header didn't changed at all)!!
What is the correct way to edit MS Word header via win32com ?
推荐答案
在此行中使用括号而不是方括号,以及基于1的索引. COM中的所有内容都是函数调用或属性.
Use parentheses instead of square brackets in this line, along with 1-based indexing. Everything in COM is a function call or property.
word.ActiveDocument.Sections(1).Headers(win32.constants.wdHeaderFooterPrimary).Range.Text='test text'
这篇关于使用win32com编辑MS Word标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文