Open-Office 2.0 bêta-2 français pour Windows (en fait, la 1.9.125 ) est
sortie.
Vous la trouverez là :
http://oootranslation.services.openo...install_fr.zip
Je cite ce lien, car j'ai réussi à piloter Open-Office, depuis Python, avec
PyWin32.
Vous trouverez ci dessous le script expérimental (code "brut" et méchant ;
mais c'est la faisabilité qui importe ici).
Ce script fonctionne avec le fichier Open-office C:\\ootest.odt qui
contient :
---------------------------------------------------------------------
AAAAAAAAAAAA
BBBBBBBBBBB
CCCCCCCCCCC
111%TXT1%111
22222222222222
%CADRE1%
333333333333333
444444444444444 4
Aaz aze ABC djhevgd fude uftyf
ofh efzehiufre hiufre
zefoz hfzr hruifh
ABC ABC gyuguyg ufg eruzyfgerABCABC
efeorzehfzrehiu freh
ABC
---------------------------------------------------------------------
Recréez ce fichier, puis lancez le script ci-dessous.
Ensuite, ben... lisez le code-source, et débrouillez-vous...
---
Michel Claveau
PS : je poste ce message en HTML, puis en plain/text, pour faciliter les
choses.
---------------------------------------------------------------------
# -*- coding: cp1252 -*-
import win32com.client
import time
def insertIntoCell( strCellName, strText, objTable):
objCellText = objTable.getCel lByName( strCellName)
objCellCursor = objCellText.cre ateTextCursor()
objCellCursor.s etPropertyValue ("CharColor",16 777215)
objCellText.ins ertString(objCe llCursor, strText, False)
objServiceManag er = win32com.client .Dispatch("com. sun.star.Servic eManager")
objDesktop = objServiceManag er.CreateInstan ce("com.sun.sta r.frame.Desktop ")
args = []
#si nouveau document, ligne suivante
#objDocument = objDesktop.load ComponentFromUR L("private:fact ory/swriter",
"_blank", 0, args)
"""
//swriter pour le traitement de texte.
//scalc pour le tableur
//sdraw pour l'éditeur de dessin
//simpress pour l'éditeur de présentation (équivalent de PowerPoint)
//smath pour l'éditeur de formule mathématique
//swriter/Global Document document maitre
//swriter/web Pour l'éditeur HTML
"""
#si document existant
objDocument = objDesktop.load ComponentFromUR L("file:///C:/ootest.odt",
"_blank", 0, args)
objText = objDocument.Get Text()
objCursor = objText.createT extCursor()
objText.insertS tring(objCursor , "Vous avez les salutations de PONX.\nCeci
est la deuxième ligne.\n", 0)
objText.insertS tring(objCursor , "Troisième ligne.\n", 0)
objText.insertS tring(objCursor , "4è ligne éèë?@ç.\n\n\n", 0)
# Tableau
#Crée un tableau de 4 colonnes x 4 lignes
objTable= objDocument.cre ateInstance( "com.sun.star.t ext.TextTable")
objTable.IsWidt hRelative = True
objTable.Relati veWidth = 80 # largeur de 80 %
objTable.BackCo lor = 255*256*256+255 *256+204 # fond en jaune clair
objTable.HoriOr ient = 2 # 0 LEFT 1 RIGHT 2 CENTER
objTable.initia lize(8, 4) # lignes / colonnes
#Insère la table
objText.insertT extContent(objC ursor, objTable, 0)
#1ère ligne
objRows = objTable.getRow s()
objRow0 = objRows.getByIn dex(0)
#Couleur de fond
objTable.setPro pertyValue("Bac kTransparent", 0)
objTable.setPro pertyValue("Bac kColor", 255*256*256+255 *256+204)
#Autre couleur de fond, pour la première ligne
objRow0.setProp ertyValue("Back Transparent", 0)
objRow0.setProp ertyValue("Back Color", 6710932)
objRow0.setProp ertyValue("IsAu toHeight", False)
objRow0.setProp ertyValue("Heig ht", 3000) #30 mm
#Remplissage 1ère ligne
insertIntoCell( "A1","FirstColu mn",objTable)
insertIntoCell( "B1","SecondCol umn",objTable)
insertIntoCell( "C1","ThirdColu mn",objTable)
insertIntoCell( "D1","SUM",objT able)
#Remplissage suite
objTable.getCel lByName("A2").s etValue(22.5)
objTable.getCel lByName("B2").s etValue(5615.3)
objTable.getCel lByName("C2").s etValue(-2315.7)
objTable.getCel lByName("D2").s etFormula("sum( <A2>|<B2>|<C2>) ")
objTable.getCel lByName("A3").s etValue(21.5)
objTable.getCel lByName("B3").s etValue(615.3)
objTable.getCel lByName("C3").s etValue(-315.7)
objTable.getCel lByName("D3").s etFormula("sum( <A3:C3>)")
objTable.getCel lByName("A4").s etValue(121.5)
objTable.getCel lByName("B4").s etValue(-615.3)
objTable.getCel lByName("C4").s etValue(415.7)
objTable.getCel lByName("D4").s etFormula("sum ")
#on sélectionne la colonne C
sCell = objTable.create CursorByCellNam e("C1")
sCell.goDown(4, True)
sCell.BackColor = 255*256*256+200 *256+200 #rouge clair
""" ça ne marche pas (spécif OO-2.0 ?)
cols = objTable.getCol umns()
col = cols.getByIndex (2)
"""
#Couleur des caractères et ombre
objCursor.setPr opertyValue("Ch arColor", 255)
objCursor.setPr opertyValue("Ch arShadowed", True)
#Saut de paragraphe
#The second argument is a
com::sun::star: :text::ControlC haracter::PARAG RAPH_BREAK constant
objText.insertC ontrolCharacter (objCursor, 0 , False)
#Insertion texte
objText.insertS tring(objCursor , " Texte coloré - bleu avec ombre\n", False)
#Saut de paragraphe (caractère spécial PARAGRAPH_BREAK = 0).
objText.insertC ontrolCharacter (objCursor, 0, False)
#on repasse en noir, sans ombre
objCursor.setPr opertyValue("Ch arColor", 0)
objCursor.setPr opertyValue("Ch arShadowed", False)
# Attention : ces 4 lignes fonctionnent en invisible.
objText.insertS tring(objCursor , "RRRRRRRRRR R", False)
objCursor.goLef t(3, False)
objCursor.goLef t(5, True)
objCursor.Strin g="+++"
objCursor.gotoE ndOfParagraph(F alse)
#Saut de paragraphe
objText.insertC ontrolCharacter (objCursor, 0, False)
#Création d'un cadre (TextFrame)
objTextFrame= objDocument.cre ateInstance("co m.sun.star.text .TextFrame")
objTextFrame.Wi dth = 10400 # 104 mm largeur
#objTextFrame.H eight =2000 # 20 mm de haut # ne sert à rien, le cadre
d'agrandit tout seul
#Affectation type de cadre (AnchorType.AS_ CHARACTER = 1)
objTextFrame.se tPropertyValue( "AnchorType ", 1)
#insertion du cadre
objText.insertT extContent(objC ursor, objTextFrame, False)
#Lecture du texte du cadre
objFrameText= objTextFrame.ge tText()
#Positionnement curseur interne
objFrameTextCur sor= objFrameText.cr eateTextCursor( )
#Insertion texte
objFrameText.in sertString(objF rameTextCursor, "Encore du
texte\naaaaaaaa aaaa\nbbbbbbb\n ccccc\n", False)
objFrameText.in sertString(objF rameTextCursor, "\net une autre ligne.",
False)
objFrameText.in sertString(objF rameTextCursor, "AAAAAAAA1\ n", False)
objFrameText.in sertString(objF rameTextCursor, "AAAAAAAA2\ n", False)
objFrameText.in sertString(objF rameTextCursor, "AAAAAAAA3\ n", False)
objFrameText.in sertString(objF rameTextCursor, "AAAAAAAA4\ n", False)
objFrameText.in sertString(objF rameTextCursor, "AAAAAAAA5" , False)
#Création d'un cadre (TextFrame)
objTextFrame= objDocument.cre ateInstance("co m.sun.star.text .TextFrame")
objTextFrame.Wi dth = 8000 # 80 mm largeur
objTextFrame.se tPropertyValue( "AnchorType ", 1)
# on va mettre la cadre à la place d'autre chose (remplacement)
och=objDocument .createSearchDe scriptor()
och.SearchStrin g="%CADRE1%"
och.SearchWords = False #mots entiers seulement ?
position=objDoc ument.findFirst (och)
position.String =""
# on remplit le cadre
objText.insertT extContent(posi tion, objTextFrame, False)
objFrameText= objTextFrame.ge tText()
objFrameTextCur sor= objFrameText.cr eateTextCursor( )
objFrameText.in sertString(objF rameTextCursor, "1111111\n" , False)
objFrameText.in sertString(objF rameTextCursor, "2222222222 \n", False)
objFrameText.in sertString(objF rameTextCursor, "333333333" , False)
#couleur texte et sans ombre
objCursor.setPr opertyValue("Ch arColor", 255*256*256+0*2 56+0)
objCursor.setPr opertyValue("Ch arShadowed", False)
#Insertion texte
objText.insertS tring(objCursor , "\n\n\nThat 's all for now !!", False)
#recherche et remplacement
orempl=objDocum ent.createRepla ceDescriptor()
orempl.SearchSt ring="ABC"
orempl.ReplaceS tring="XXXYYYZZ Z"
orempl.SearchWo rds = False #mots entiers seulement ?
orempl.SearchCa seSensitive = True #sensible à la casse ?
nb = objDocument.rep laceAll(orempl)
print "Nb remplacements :",nb
#recherche, effacement et insertion
och=objDocument .createSearchDe scriptor()
och.SearchStrin g="%TXT1%"
och.SearchWords = False #mots entiers seulement ?
position=objDoc ument.findFirst (och)
position.String ="" #remplacement
position.setPro pertyValue("Cha rColor", 255*256*256) #rouge Rouge*256*256 +
Vert*256 + Bleu
#position.goRig ht(1, False)
objText.insertS tring(position, "Texte de remplacement", 0)
objText.insertS tring(position, "Texte ajouté", 0)
#impression, sauvergarde, sortie
objDocument.Pri nt(args)
objDocument.sto reAsURL("file:///C:/ootest2.odt", args)
objDesktop.Term inate()