英文:
How export libreoffice Impress to image
问题
在LibreOffice Impress中,我可以选择一张幻灯片,点击“文件”,然后选择从下拉菜单中导出为png、gif、jpg。我认为OpenXML SDK应该能够自动执行LibreOffice中的任何功能,但显然不行。
我还尝试在命令行上运行soffice.exe并将其转换为html,但没有生成图像文件。我使用了以下命令:
soffice.exe --headless --convert-to html --convert-images-to jpg dg_mobile.ppt
在Visual Studio 2022和C#中,我可以使用什么来自动化执行此导出功能?
英文:
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?
答案1
得分: 0
我最终将其转换为PDF,然后再转换为JPG,但质量非常差。最后,我使用了一个Python脚本,质量非常好!我只需要在C#程序内部调用以下脚本:
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
def ExportJPEG(xExporter, slide, sURL, quality):
PixelHeight = slide.Height // 14
PixelWidth = slide.Width // 14
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()
请注意,代码中的"
字符应该是"
字符。
英文:
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()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论