2013-04-17 55 views
1

我試圖使用Python與win32組件自動化Microsoft Office Word 2010文檔。在那邊我需要閱讀並獲取WORD文檔的標題信息。我看了一下MSDN庫(並認爲getFieldNames可以幫助我),甚至看看@Editing MS Word header with win32com(使用這個我只能編輯標題信息),但沒有一個適用於我。Python:Win32:獲取Word文檔中的標題信息

我的代碼片段是:

..//

#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這樣的屬性。

對此的任何建議是最受歡迎的。

+0

對這個有什麼想法? – Varun

回答

2

明白了。它應該是這樣的:

... ///

#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 

... ///

+0

當我嘗試你的代碼時,我得到「AttributeError:wdHeaderFooterPrimary」。這是我的腳本:.... import win32com.client .... word = win32com.client.Dispatch(「Word.Application」)... word.Documents.Open(「C:\ Users \ ME \ Desktop \ Test \ wordDOC.doc「).... header_string = word.ActiveDocument.Sections(1).Headers(win32com.client.constants.wdHeaderFooterPrimary).Range.Text – Norfeldt

+1

我缺少單詞= win32com.client.gencache .EnsureDispatch(「Word.Application」) – Norfeldt

+0

你還可以在頭文件中讀取表格嗎? – Norfeldt