我正嘗試在使用Python的新Microsoft Word文檔中創建多個表格。我可以創建第一個表。但我想我有COM Range對象配置錯誤。它並沒有指向結尾。第一個表格放在「Hello I am a text!」之前,第二個表格放在第一個表格的第一個單元格內。我認爲從wordapp返回一個範圍將返回全部範圍,然後使用wdCollapseStart Enum(我認爲是1)摺疊它(我無法在Python win32com中找到常量)。因此,將一個表格添加到範圍的末尾會將其添加到文檔的末尾,但這不會發生。使用python win32com無法在MS Word 2007中創建兩個單獨的表格
任何想法?
感謝蒂姆
import win32com.client
wordapp = win32com.client.Dispatch("Word.Application")
wordapp.Visible = 1
worddoc = wordapp.Documents.Add()
worddoc.PageSetup.Orientation = 1
worddoc.PageSetup.BookFoldPrinting = 1
worddoc.Content.Font.Size = 11
worddoc.Content.Paragraphs.TabStops.Add (100)
worddoc.Content.Text = "Hello, I am a text!"
location = worddoc.Range()
location.Collapse(1)
location.Paragraphs.Add()
location.Collapse(1)
table = location.Tables.Add (location, 3, 4)
table.ApplyStyleHeadingRows = 1
table.AutoFormat(16)
table.Cell(1,1).Range.InsertAfter("Teacher")
location1 = worddoc.Range()
location1.Paragraphs.Add()
location1.Collapse(1)
table = location1.Tables.Add (location1, 3, 4)
table.ApplyStyleHeadingRows = 1
table.AutoFormat(16)
table.Cell(1,1).Range.InsertAfter("Teacher1")
worddoc.Content.MoveEnd
worddoc.Close() # Close the Word Document (a save-Dialog pops up)
wordapp.Quit() # Close the Word Application