Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How export libreoffice Impress to image

In Libreoffice Impress I can select a slide, File, Export, and select png, gif, jpg from the pull-down menu. I thought openxml sdk would let you automate any function within Libreoffice but apparently not.

I also tried running soffice.exe at the command line and converting to html but that didn't generate image files. I used:

soffice.exe --headless --convert-to html --convert-images-to jpg  dg_mobile.ppt

What can I use in Visual Studio 2022 and C# to automate this export function?

like image 927
Velocedge Avatar asked Sep 14 '25 18:09

Velocedge


1 Answers

I ended up converting to pdf and then to jpg but it was very poor quality. I ended up using a Python script and quality is excellent! I just call the following script from inside C# program with:

python xport.py inputFile outputPath

xport.py:

import uno
import code
import socket
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 // 14
    PixelWidth = slide.Width // 14
    #print("Height: " + str(PixelHeight))
    #print("Width: " + str(PixelWidth))
    filterData = (
        PropertyValue("PixelWidth",0,PixelWidth,0), 
        PropertyValue("PixelHeight",0,PixelHeight,0), 
        PropertyValue("Quality",0,quality,0)
    )
    writeJPEG = (PropertyValue("MediaType", 0, "image/jpeg",0),
        PropertyValue("URL", 0, sURL,0),
        PropertyValue("Overwrite", 0, True, 0),
        PropertyValue("FilterData",0,uno.Any("[]com.sun.star.beans.PropertyValue", filterData),0)
    )
    xExporter.setSourceDocument(slide)
    xExporter.filter(writeJPEG)


if len(sys.argv) < 3:
    print("Usage: python xport.py <presentation_file> <output path>")
    sys.exit(1)

file = sys.argv[0]
outputFile = sys.argv[0]

lo_proc = Popen('"D:\Program Files\LibreOffice\program\soffice.exe" -headless -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)

try:
    fileProps = (PropertyValue( "MacroExecutionMode", 0, 4, 0), PropertyValue( "Hidden", 0, False, 0 ))
    fileURL = uno.systemPathToFileUrl(file)
    doc = desktop.loadComponentFromURL(fileURL, "_default",0,fileProps)
except Exception as e:
    print("Error loading presentation file:", e)
    sys.exit(1)

slides = doc.getDrawPages()
num_slides = slides.getCount()

xExporter = smgr.createInstance("com.sun.star.drawing.GraphicExportFilter")

for i in range(num_slides):
    sURL = uno.systemPathToFileUrl(outputFile + "/Slide" + str(i+1) + ".jpg")
    ExportJPEG(xExporter, slides.getByIndex(i), sURL, 100)

doc.close(True)
desktop.terminate()

lo_proc.wait()
like image 182
Velocedge Avatar answered Sep 17 '25 08:09

Velocedge