我有 Python 连接 openoffice 后,日志里发现 python 连接 openoffice socket 很费时间

2015-08-03 18:39:55 +08:00
 liuxu
这么写一个helloworld就贼慢,以后在做pdf,doc等转矢量图那就更慢了,请问有啥办法么?
日志:
2015-08-03 18:30:56,804 [CALL,tid 1]: Instantiating pyuno bridge
2015-08-03 18:30:56,808 [CALL,tid 1]: try py->uno[0x7fd8e9b51c30].createInstanceWithContext((string)"com.sun.star.bridge.UnoUrlResolver", (com.sun.star.uno.XComponentContext)0x7fd8e9bdcd18)
2015-08-03 18:30:56,809 [CALL,tid 1]: success py->uno[0x7fd8e9b51c30].createInstanceWithContext()=(com.sun.star.uno.XInterface)0x7fd8e9baf288
2015-08-03 18:30:56,809 [CALL,tid 1]: try py->uno[0x7fd8e9b51e30].resolve((string)"uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
#此处等待30s以上
2015-08-03 18:31:56,892 [CALL,tid 1]: success py->uno[0x7fd8e9b51e30].resolve()=(com.sun.star.uno.XInterface)0x167e7d8
2015-08-03 18:31:56,907 [CALL,tid 1]: try py->uno[0x7fd8e9b51d30].createInstanceWithContext((string)"com.sun.star.frame.Desktop", (com.sun.star.uno.XInterface)0x167e7d8)
2015-08-03 18:31:56,908 [CALL,tid 1]: success py->uno[0x7fd8e9b51d30].createInstanceWithContext()=(com.sun.star.uno.XInterface)0x1687a88
2015-08-03 18:31:56,928 [CALL,tid 1]: try py->uno[0x7fd8e9b9a038].getCurrentComponent()
2015-08-03 18:31:56,929 [CALL,tid 1]: success py->uno[0x7fd8e9b9a038].getCurrentComponent()=(com.sun.star.lang.XComponent)0x1696828
2015-08-03 18:31:56,983 [CALL,tid 1]: try py->uno[0x7fd8e16f6060].createTextCursor()
2015-08-03 18:31:56,984 [CALL,tid 1]: success py->uno[0x7fd8e16f6060].createTextCursor()=(com.sun.star.text.XTextCursor)0x16d79d8
2015-08-03 18:31:57,000 [CALL,tid 1]: try py->uno[0x7fd8e16f6060].insertString((com.sun.star.text.XTextCursor)0x16d79d8, (string)"Hello World", (byte)0x0)
2015-08-03 18:31:57,002 [CALL,tid 1]: success py->uno[0x7fd8e16f6060].insertString()=void


python程序:

import socket
import uno

#get the uno component from the PyUNO runtime
localContext = uno.getComponentContext()

#create the UnoUrlResolver
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver",localContext)

#connect to the running office
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
smgr = ctx.ServiceManager

# get the contral desktop object
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop",ctx)

# access the current writer document
model = desktop.getCurrentComponent()

# access the document's text property
text = model.Text

# create a cursor
cursor = text.createTextCursor()

# Insert the text into the document
text.insertString(cursor,"Hello World",0)

# Do a nasty thing before exiting the python process. In case the
# last call is a oneway call (e.g. see idl-spec of insertString),
# it must be forced out of the remote-bridge caches before python
# exits the process. Otherwise, the oneway call may or may not reach
# the target object.
# I do this here by calling a cheap synchronous call (getPropertyValue).
ctx.ServiceManager
1583 次点击
所在节点    Python
0 条回复

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/210514

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX