Générer un fichier doc à partir d'un modèle XML
Bonjour,
Je suis débutant en python et j'ai pour premier projet de maîtriser la lib pywin32 et minidom.
j'ai pour objectif de créer un script me permettant de parser un fichier XML et à partir du DOM récupéré, ensuite générer un tableau dans un document word avec les donnés parsées.
J'ai commencé un bout de script pour ouvrir un fichier word et écrire dedans.
Je coince sur la partie XML car avec j'ai une erreur "invalide syntaxe" sur print xmlTag
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
from xml.dom.minidom import parse, parseString
import win32com.client as win32
#open the xml file for reading:
file = open('C:\chemin\tableau.xml','r')
#convert to string:
data = file.read()
#close file because we dont need it anymore:
file.close()
#parse the xml you got from the file
dom = parseString(data)
#retrieve the first xml tag (<tag>data</tag>) that the parser finds with name tagName:
xmlTag = dom.getElementsByTagName('client')[0].toxml()
#strip off the tag (<tag>data</tag> ---> data):
xmlData=xmlTag.replace('<client>','').replace('</client>','')
#print out the xml tag and data in this format: <tag>data</tag>
print xmlTag
#just print the data
print xmlData
def word():
word = win32.gencache.EnsureDispatch('Word.Application')
doc = word.Documents.Add()
word.Visible = True
sleep(1)
rng = doc.Range(0,0)
rng.InsertAfter('Hacking Word with Python\r\n\r\n')
rng.InsertAfter("\r\nPython rules!\r\n")
#doc.Close(False)
#word.Application.Quit()
if __name__ == '__main__':
word() |