如何将LibreOffice Impress导出为图像。

huangapple go评论87阅读模式
英文:

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()

请注意,代码中的&quot;字符应该是"字符。

英文:

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(&quot;Height: &quot; + str(PixelHeight))
#print(&quot;Width: &quot; + str(PixelWidth))
filterData = (
PropertyValue(&quot;PixelWidth&quot;,0,PixelWidth,0), 
PropertyValue(&quot;PixelHeight&quot;,0,PixelHeight,0), 
PropertyValue(&quot;Quality&quot;,0,quality,0)
)
writeJPEG = (PropertyValue(&quot;MediaType&quot;, 0, &quot;image/jpeg&quot;,0),
PropertyValue(&quot;URL&quot;, 0, sURL,0),
PropertyValue(&quot;Overwrite&quot;, 0, True, 0),
PropertyValue(&quot;FilterData&quot;,0,uno.Any(&quot;[]com.sun.star.beans.PropertyValue&quot;, filterData),0)
)
xExporter.setSourceDocument(slide)
xExporter.filter(writeJPEG)
if len(sys.argv) &lt; 3:
print(&quot;Usage: python xport.py &lt;presentation_file&gt; &lt;output path&gt;&quot;)
sys.exit(1)
file = sys.argv[0]
outputFile = sys.argv[0]
lo_proc = Popen(&#39;&quot;D:\Program Files\LibreOffice\program\soffice.exe&quot; -headless -accept=socket,host=localhost,port=2002;urp;&#39;, shell=True)
localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext(&quot;com.sun.star.bridge.UnoUrlResolver&quot;, localContext)
ctx = resolver.resolve( &quot;uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext&quot; )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext( &quot;com.sun.star.frame.Desktop&quot;,ctx)
try:
fileProps = (PropertyValue( &quot;MacroExecutionMode&quot;, 0, 4, 0), PropertyValue( &quot;Hidden&quot;, 0, False, 0 ))
fileURL = uno.systemPathToFileUrl(file)
doc = desktop.loadComponentFromURL(fileURL, &quot;_default&quot;,0,fileProps)
except Exception as e:
print(&quot;Error loading presentation file:&quot;, e)
sys.exit(1)
slides = doc.getDrawPages()
num_slides = slides.getCount()
xExporter = smgr.createInstance(&quot;com.sun.star.drawing.GraphicExportFilter&quot;)
for i in range(num_slides):
sURL = uno.systemPathToFileUrl(outputFile + &quot;/Slide&quot; + str(i+1) + &quot;.jpg&quot;)
ExportJPEG(xExporter, slides.getByIndex(i), sURL, 100)
doc.close(True)
desktop.terminate()
lo_proc.wait()

huangapple
  • 本文由 发表于 2023年3月3日 19:14:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75626383.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定