0
我試圖以編程方式編寫一個Word文件,並努力在選擇對象上交替樣式。這裏是代碼的相關位:win32com在選擇對象中的樣式之間移動
objWord = win32com.client.Dispatch("Word.Application")
objSel = objWord.Selection
writestuff = "\r\nSome Heading"
objSel.Style = objWord.ActiveDocument.Styles("Heading 3")
objSel.TypeText(writestuff)
#This works so far, we have a heading and some text, now we want to write data below the heading
objSel.Style = objWord.ActiveDocument.Styles("Normal") #Setting style back to 'Normal' for next section
writestuff = "\r\nSome data about the heading we just wrote")
objSel.TypeText(writestuff)
#At this point the heading and new text both go to the 'Normal' style.
看來,我的「選擇」然而,影響所有的文件,當我做我的初步objSel.Style分配「標題3」之前的線都沒有影響。當我切換回正常時,其他一切都是。