Skip to main content
 首页 » 编程设计

python之使用 jython 和 open office 2.4 将文档转换为 pdf

2025年05月04日7me-sa

我使用 pyuno 完成了一个 python 脚本,它成功地将文档/xls/rtf 等转换为 pdf。 然后我需要更新一个 mssql 数据库,因为 open office 目前支持 python 2.3,它很古老,缺乏对体面数据库库的支持。所以我求助于使用 Jython,这样我就不会因为使用旧的 pyuno 在 OO python 环境中运行而感到负担。

这也意味着我的转换代码被破坏了,我现在需要使用 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

//used in 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
// also what is the equivalent of url = uno.systemPathToFileUrl(file) in Java so that I can make use of it to nicely format my path
properties = []
p = PropertyValue()
p.Name = "Hidden"
p.Value = True
properties.append(p)
properties = tuple(properties)
doc = xcomponentloader.loadComponentFromURL(file, "_blank",0, properties)

请您参考如下方法:

事情就是这样,根据这个人的说法,你需要一些油......它就像一个魅力

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

包括这个库 C:\OpenOffice_24\program\classes\unoil.jar