... that was a tough one ...
There were two issues with your code. (i) PixelWidth/PixelHeight is way too large for the GraphicExporter, and (ii) you have to do some forced type conversion when passing the FilterData. See the comments in the code
Code: Select all
import uno
import sys
import time
import os
from subprocess import Popen
from com.sun.star.beans import PropertyValue #type:ignore
def ExportJPEG(xExporter, slide, sURL, quality):
PixelHeight = slide.Height
PixelWidth = slide.Width
print(PixelHeight)
print(PixelWidth)
filterData = (
# PixelWidth / PixelHeight is 29700 / 21000 on my computer. This seems to be too high for the exporter.
PropertyValue("PixelWidth",0,PixelWidth/10,0),
PropertyValue("PixelHeight",0,PixelHeight/10,0),
PropertyValue("Quality",0,quality,0),
PropertyValue("ColorMode",0,1,0)
)
writeJPEG = (PropertyValue("MediaType", 0, "image/jpeg",0),
PropertyValue("URL", 0, sURL,0),
PropertyValue("Overwrite", 0, True, 0),
# PropertyValue("FilterData",0, filterData,0) # this does not work
PropertyValue("FilterData",0,uno.Any("[]com.sun.star.beans.PropertyValue", filterData),0)
)
xExporter.setSourceDocument(slide)
xExporter.filter(writeJPEG)
lo_proc = Popen('"C:\Program Files (x86)\OpenOffice 4\program\soffice.exe" -accept=socket,host=localhost,port=2002;urp;', shell=True)
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext)
ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
fileProps = (PropertyValue( "MacroExecutionMode", 0, 4, 0), PropertyValue( "Hidden", 0, False, 0 ))
file = "C:\Users\Martin\Desktop\ExportJpg.odg"
fileURL = uno.systemPathToFileUrl(file)
document = desktop.loadComponentFromURL(fileURL, "_default",0,fileProps)
slides = document.getDrawPages()
xExporter = smgr.createInstance("com.sun.star.drawing.GraphicExportFilter")
sURL1 = uno.systemPathToFileUrl("C:\Users\Martin\Desktop")+"/Slide1.JPG"
sURL2 = uno.systemPathToFileUrl("C:\Users\Martin\Desktop")+"/Slide2.JPG"
ExportJPEG(xExporter, slides.getByIndex(0), sURL1, 100)
ExportJPEG(xExporter, slides.getByIndex(0), sURL2, 10)
lo_proc.wait()