2017-03-09 1 views
0

プログラムでWordファイルを書き込もうとしていますが、選択オブジェクトのスタイルを変更するのに苦労しています。ここでは、コードの該当ビットは次のとおりです。win32comがSelectoinのスタイル間を移動する

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見出し」に作るとき、私の「選択」は、しかし、文書のすべてに影響を及ぼしていることが表示されます影響を受けた。私が正常に戻ったときには他のすべてがあります。

答えて

0

このようになります私は何をするために必要な(または、この問題を解決することの少なくとも一方が)TypeParagraph()方法

修正されたコードを追加することであることが表示されます:

objWord = win32com.client.Dispatch("Word.Application") 
objSel = objWord.Selection 
writestuff = "\r\nSome Heading" 
objSel.Style = objWord.ActiveDocument.Styles("Heading 3") 
objSel.TypeText(writestuff) 
objSel.TypeParagraph ##THIS IS WHAT I ADDED, NO NEED TO READJUST STYLE## 
writestuff = "\r\nSome data about the heading we just wrote") 
objSel.TypeText(writestuff) 

この次の行に正しい見出しと通常のテキストのデータが表示されます。

関連する問題