2017-03-09 5 views
0

Je tente d'écrire par programme un fichier Word et j'ai du mal à alterner les styles sur un objet de sélection. Voici le bit correspondant du code:Win32com se déplaçant entre Styles dans un 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. 

Il semble que ma « sélection » affecte la totalité du document, mais, quand je fais ma première affectation de objSel.Style à «titre 3 lignes précédentes ne sont pas affecté. Quand je reviens à la normale tout le reste est.

Répondre

0

Il semble que ce que je devais faire (ou au moins l'une des choses qui va résoudre ce problème) est d'ajouter une méthode TypeParagraph()

code corrigé ressemble à ceci:

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) 

Cette présenterait les données avec le titre correct, puis le texte de type normal sur la ligne suivante.