使用Jython和Open Office 2.4将文档转换为PDF

2 投票
2 回答
1099 浏览
提问于 2025-04-11 09:36

我写了一个用pyuno的python脚本,成功把文档、xls、rtf等格式转换成pdf。然后我需要更新一个mssql数据库,但因为Open Office只支持老旧的python 2.3,缺乏对好的数据库库的支持,所以我决定使用Jython。这样我就不用在Open Office的python环境中使用老旧的pyuno了。

这也意味着我的转换代码坏了,我需要用Java库来替代pyuno库。

import com.sun.star.beans.PropertyValue as PropertyValue
import com.sun.star.bridge.XUnoUrlResolver as XUnoUrlResolver
import com.sun.star.comp.helper.Bootstrap as Bootstrap
->> import com.sun.star.frame.XComponentLoader as XComponentLoader
->> import com.sun.star.frame.XStorable as XStorable
import com.sun.star.lang.XMultiComponentFactory as XMultiComponentFactory
import com.sun.star.uno.UnoRuntime as UnoRuntime
import com.sun.star.uno.XComponentContext as XComponentContext

带有'->>'的导入语句是因为编译器无法识别com.sun.star.frame中的'frame'部分。这些是我包含的库。

alt text http://www.freeimagehosting.net/uploads/eda5cda76d.jpg

如果能给我一些建议就太好了。

context = XComponentContext
xMultiCompFactory = XMultiComponentFactory
xcomponentloader = XComponentLoader

//在python中使用
ctx = None
smgr = None
doc = None
url = None

context = Bootstrap.bootstrap()
xMultiCompFactory = self.context.getServiceManager()
xcomponentloader = UnoRuntime.queryInterface(XComponentLoader.class, ....xMultiCompFactory.createInstanceWithContext("com.sun.star.frame.Desktop", context))

file = "file:\\" + file
// 在Java中,url = uno.systemPathToFileUrl(file)的等价物是什么?我想用它来格式化我的路径
properties = []
p = PropertyValue()
p.Name = "Hidden"
p.Value = True
properties.append(p)
properties = tuple(properties)
doc = xcomponentloader.loadComponentFromURL(file, "_blank",0, properties)

2 个回答

0

我觉得用Jython是个不错的主意。不过,为什么不可以用两个脚本呢?一个脚本用pyuno/2.3,另一个用pymssql/2.5(或者你用的其他数据库适配器)呢?

中间的格式可以是任何东西,比如pickle、json或者XML。

补充一下,我之前用过很多pyuno,我能理解你的苦恼。

1

所以这个家伙说,你需要一些油……而且效果非常好。

http://www.oooforum.org/forum/viewtopic.phtml?p=304263#304263

把这个库包含进来 C:\OpenOffice_24\program\classes\unoil.jar

撰写回答