Editing MS Word header with win32com

谁说我不能喝 提交于 2019-12-13 15:52:08

问题


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()

But it has no effect (header didn't changed at all)!!
What is the correct way to edit MS Word header via win32com ?


回答1:


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'


来源:https://stackoverflow.com/questions/14148531/editing-ms-word-header-with-win32com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!