场景:
业务需要,要使用JAVA根据ppt模板自动生成MP4视频。
调研:
查了一下比较符合的PPT 操作方式,POI,ASPOSE,VBA
1. POI 支持PPT文件的读,修改,写。HSLF,XLSF支持PNG,以及基于Graphics2D 的输入文件格式,但没见到有视频输出的。 详见:XLSF
2. ASPOSE 支持的输出文件相对多一些 详见:outputFormat,但是,很遗憾,不支持MP4类视频文件。
3. VBSCRIPT 是通用的 但是不能直接调用,需要 JAVA调用VBSCRIPT导出文件
实操作:
没办法,只能VBSCRIPT了。
调用过程是这样:JAVA->CSCRIPT->执行vbscript脚本->输出MP4
JAVA:
- public void testNewPPT() throws IOException, InterruptedException {
- //注意“E:/Temp/vbs.vbs”应该是正确的存储的VBS代码的位置
- Process process = Runtime.getRuntime().exec("cmd /c CScript E:/example2.vbs");
- //等待VBS执行完毕
- process.waitFor();
- }
VBSCRIPT(参考Neohope's Blog)
- Option Explicit
- 'PPT2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.pptx","PATH_TO_INFILE\NEOHOPE.COM.OUT.pdf","PDF"
- 'PPT2ANY "PATH_TO_INFILE\NEOHOPE.COM.IN.pptx","PATH_TO_INFILE\NEOHOPE.COM.OUT.png","PNG"
- PPT2ANY "e:\example2.pptx","e:\777","MP4"
- Sub PPT2ANY( inFile, outFile, outFormat)
- Dim objFSO, objPPT, objPresentation, pptFormat
- Const ppSaveAsAddIn =8
- Const ppSaveAsBMP =19
- Const ppSaveAsDefault =11
- Const ppSaveAsEMF =23
- Const ppSaveAsExternalConverter =64000
- Const ppSaveAsGIF =16
- Const ppSaveAsJPG =17
- Const ppSaveAsMetaFile =15
- Const ppSaveAsMP4 =39
- Const ppSaveAsOpenDocumentPresentation =35
- Const ppSaveAsOpenXMLAddin =30
- Const ppSaveAsOpenXMLPicturePresentation =36
- Const ppSaveAsOpenXMLPresentation =24
- Const ppSaveAsOpenXMLPresentationMacroEnabled =25
- Const ppSaveAsOpenXMLShow =28
- Const ppSaveAsOpenXMLShowMacroEnabled =29
- Const ppSaveAsOpenXMLTemplate =26
- Const ppSaveAsOpenXMLTemplateMacroEnabled =27
- Const ppSaveAsOpenXMLTheme =31
- Const ppSaveAsPDF =32
- Const ppSaveAsPNG =18
- Const ppSaveAsPresentation =1
- Const ppSaveAsRTF =6
- Const ppSaveAsShow =7
- Const ppSaveAsStrictOpenXMLPresentation =38
- Const ppSaveAsTemplate =5
- Const ppSaveAsTIF =21
- Const ppSaveAsWMV =37
- Const ppSaveAsXMLPresentation =34
- Const ppSaveAsXPS =33
- ' Create a File System object
- Set objFSO = CreateObject( "Scripting.FileSystemObject" )
- ' Create a PowerPoint object
- Set objPPT = CreateObject( "PowerPoint.Application" )
- With objPPT
- ' True: make PowerPoint visible; False: invisible
- .Visible = True
- ' Check if the PowerPoint document exists
- If not( objFSO.FileExists( inFile ) ) Then
- WScript.Echo "FILE OPEN ERROR: The file does not exist" & vbCrLf
- ' Close PowerPoint
- .Quit
- Exit Sub
- End If
- ' Open the PowerPoint document
- .Presentations.Open inFile
- ' Make the opened file the active document
- Set objPresentation = .ActivePresentation
- If StrComp(Ucase( outFormat ),"PDF") = 0 then
- pptFormat = ppSaveAsPDF
- ElseIf StrComp(Ucase( outFormat ),"XPS") = 0 then
- pptFormat = ppSaveAsXPS
- ElseIf StrComp(Ucase( outFormat ),"BMP") = 0 then
- pptFormat= ppSaveAsBMP
- ElseIf StrComp(Ucase( outFormat ),"PNG") = 0 then
- pptFormat= ppSaveAsPNG
- ElseIf StrComp(Ucase( outFormat ),"JPG") = 0 then
- pptFormat= ppSaveAsJPG
- ElseIf StrComp(Ucase( outFormat ),"GIF") = 0 then
- pptFormat= ppSaveAsGIF
- ElseIf StrComp(Ucase( outFormat ),"XML") = 0 then
- pptFormat= ppSaveAsOpenXMLPresentation
- ElseIf StrComp(Ucase( outFormat ),"RTF") = 0 then
- pptFormat= ppSaveAsRTF
- ElseIf StrComp(Ucase( outFormat ),"MP4") = 0 then
- pptFormat= ppSaveAsMP4
- Else
- WScript.Echo "FILE FORTMART ERROR: Unknown file format" & vbCrLf
- ' Close PowerPoint
- .Quit
- Exit Sub
- End If
- ' Save in PDF/XPS format
- objPresentation.SaveAs outFile, pptFormat
- ' save as MP4 need long time, do not close too early
- If StrComp(Ucase( outFormat ),"MP4") = 0 then
- wscript.sleep 1000*15
- End If
- ' Close the active document
- objPresentation.Close
- ' Close PowerPoint
- .Quit
- End With
- End Sub
详见:文件键值对对应关系
注意:
1 因为要用到vbscript,部署环境需要windows。
2 saveas 格式为 ppSaveAsMP4或其它较大输出文件时,要注意根据文件大小,设定sleep时间,正在进行导出时执行了close 是获取不到输出文件的。
3 导出时PPT会打开。
4 PPT最好是英文名,否则需要处理乱码问题。