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 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| coding: utf-8
from __future__ import unicode_literals
import uno
from com.sun.star.beans import PropertyValue
def dict_to_property(values, uno_any=False):
ps = tuple([PropertyValue(Name=n, Value=v) for n, v in values.items()])
if uno_any:
ps = uno.Any('[]com.sun.star.beans.PropertyValue', ps)
return ps
def copieImagesXl2Word():
doc = XSCRIPTCONTEXT.getDocument()
draw_page = doc.getCurrentController().getActiveSheet().getDrawPage()
for image in draw_page:
print(image.Name)
print(draw_page.Count)
ctx = uno.getComponentContext()
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
# open a writer document
newDoc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0,())
oText = newDoc.getText()
oCurseur = oText.createTextCursor()
#--- création tableau ---
oTable = newDoc.createInstance( "com.sun.star.text.TextTable" )
#Définit les dimensions du tableau:
oTable.initialize( 4, 2 )
oText.insertTextContent(oCurseur, oTable, False )
oTable.getCellByPosition(0,0).setString("Image")
oTable.getCellByPosition(1,0).setString("Nom Image")
x=1
for image in draw_page:
print(image.Name)
oGraph = newDoc.createInstance("com.sun.star.text.GraphicObject")
cellule1 = oTable.getCellByPosition(1,x)
cellule1.setString(image.Name)
cellule2 = oTable.getCellByPosition(0,x)
texte = cellule2.getText()
oGraph.Graphic = image.Graphic
curseur = texte.createTextCursor()
texte.insertTextContent( curseur ,oGraph , False )
x +=1
path_docx = uno.systemPathToFileUrl('D:/temp/MonFichierDocx.docx')
args = {
'FilterName': 'MS Word 2007 XML',
}
args = dict_to_property(args)
newDoc.storeToURL(path_docx, args) |
Partager