我正在尝试使用带有Win32组件的Python自动执行Microsoft Office Word 2010文档。在那边,我需要阅读并获取WORD文档的标头信息。我看了MSDN库(并认为getFieldNames可以帮到我),甚至看了@ Editing MS Word header with win32com(使用它我只能编辑标头信息),但是对我来说都不起作用。
我的代码段是:
..//
#tell word to open the document
word.Documents.Open (IP_Directory_Dest + "\\" + name)
#open it internally
doc = word.Documents(1)
### get the header of file - using this gives me Attribute Error
## header = doc.getFieldNames()
## print ('Header = ', header)
..//
此外,当我使用getFieldNames()时,它给出错误“ AttributeError:”对象没有属性“ getFieldNames””,这使我相信对象lib中没有像getFieldNames这样的属性。
欢迎对此提出任何建议。
最佳答案
得到它了。应该是这样的:
... ///
#tell word to open the document
word.Documents.Open (IP_Directory_Dest + "\\" + copied_IR_file)
# store the header information
header_string = word.ActiveDocument.Sections(1).Headers(win32com.client.constants.wdHeaderFooterPrimary).Range.Text
... ///
关于python - Python:Win32:在Word文档中获取标题信息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16055150/