我使用wx python gui收集用户数据,然后使用uno将这些数据填充到ubuntu 10.xx 下的openoffice文档中
用户+我的脚本(+空文档)-->预填充文档
升级到ubuntu 14.04后,uno不再适用于python 2.7,现在我们在ubuntu中使用了libreoffice而不是openoffice。当我尝试运行python2.7代码时,它会显示:
ImportError: No module named uno
我怎么能把它带回工作岗位?
我尝试过的:已安装https://pypi.python.org/pypi/unotoolsv0.3.3sudo apt-get安装libreoffice脚本提供程序python
将代码转换为python3并得到不可导入的,但wx在python3:-/中不可导入
ImportError: No module named 'wx'
谷歌搜索和阅读python3仅适用于wx phoenix
因此尝试安装:http://wxpython.org/Phoenix/snapshot-builds/但没能让它与python3 一起运行
有没有办法让uno桥在ubuntu 14.04下使用py2.7?或者如何让wx与py3一起运行?我还能试什么?
在LibreOffice中创建一个python宏,该宏将完成将数据插入LibreOffice的工作,然后在python 2.7代码中对宏进行envoke
由于宏是从LibreOffice运行的,它将使用python3。以下是如何从命令行envoke LibreOffice宏的示例:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
##
# a python script to run a libreoffice python macro externally
# NOTE: for this to run start libreoffice in the following manner
# soffice "--accept=socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;" --writer --norestore
# OR
# nohup soffice "--accept=socket,host=127.0.0.1,port=2002,tcpNoDelay=1;urp;" --writer --norestore &
#
import uno
from com.sun.star.connection import NoConnectException
from com.sun.star.uno import RuntimeException
from com.sun.star.uno import Exception
from com.sun.star.lang import IllegalArgumentException
def uno_directmacro(*args):
localContext = uno.getComponentContext()
localsmgr = localContext.ServiceManager
resolver = localsmgr.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
try:
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
except NoConnectException as e:
print ("LibreOffice is not running or not listening on the port given - ("+e.Message+")")
return
msp = ctx.getValueByName("/singletons/com.sun.star.script.provider.theMasterScriptProviderFactory")
sp = msp.createScriptProvider("")
scriptx = sp.getScript('vnd.sun.star.script:directmacro.py$directmacro?language=Python&location=user')
try:
scriptx.invoke((), (), ())
except IllegalArgumentException as e:
print ("The command given is invalid ( "+ e.Message+ ")")
return
except RuntimeException as e:
print("An unknown error occurred: " + e.Message)
return
except Exception as e:
print ("Script error ( "+ e.Message+ ")")
print(e)
return
return(None)
uno_directmacro()
这是LibreOffice中名为"directmacro.py"的相应宏代码,存储在LibreOffice宏的User
区域中(通常为$HOME/.config/LibreOffice/4/user/Scripts/python:
#!/usr/bin/python
from com.sun.star.awt.MessageBoxButtons import BUTTONS_OK, BUTTONS_OK_CANCEL, BUTTONS_YES_NO, BUTTONS_YES_NO_CANCEL, BUTTONS_RETRY_CANCEL, BUTTONS_ABORT_IGNORE_RETRY
from com.sun.star.awt.MessageBoxButtons import DEFAULT_BUTTON_OK, DEFAULT_BUTTON_CANCEL, DEFAULT_BUTTON_RETRY, DEFAULT_BUTTON_YES, DEFAULT_BUTTON_NO, DEFAULT_BUTTON_IGNORE
from com.sun.star.awt.MessageBoxType import MESSAGEBOX, INFOBOX, WARNINGBOX, ERRORBOX, QUERYBOX
def directmacro(*args):
import socket, time
class FontSlant():
from com.sun.star.awt.FontSlant import (NONE, ITALIC,)
#get the doc from the scripting context which is made available to all scripts
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
text = model.Text
tRange = text.End
cursor = desktop.getCurrentComponent().getCurrentController().getViewCursor()
doc = XSCRIPTCONTEXT.getDocument()
parentwindow = doc.CurrentController.Frame.ContainerWindow
# your cannot insert simple text and text into a table with the same method
# so we have to know if we are in a table or not.
# oTable and oCurCell will be null if we are not in a table
oTable = cursor.TextTable
oCurCell = cursor.Cell
insert_text = "This is text inserted into a LibreOffice Documentndirectly from a macro called externally"
Text_Italic = FontSlant.ITALIC
Text_None = FontSlant.NONE
cursor.CharPosture=Text_Italic
if oCurCell == None: # Are we inserting into a table or not?
text.insertString(cursor, insert_text, 0)
else:
cell = oTable.getCellByName(oCurCell.CellName)
cell.insertString(cursor, insert_text, False)
cursor.CharPosture=Text_None
return None
当然,您需要调整代码以接受数据作为参数,或者从文件中读取数据,或者其他什么。
理想情况下,我会说使用python 3,因为python 2已经过时了。切换需要相当多的新编码更改,但迟早会更好。所以我尝试了:
sudo pip3 install -U --pre
-f http://wxpython.org/Phoenix/snapshot-builds/
wxPython_Phoenix
然而,这给了我错误,我不想花接下来的几天时间来解决这些问题。可能预发布版本还没有准备好进入黄金时段。
因此,我建议现在切换到AOO。看见https://stackoverflow.com/a/27980255/5100564以获取说明。AOO并没有LO拥有的所有最新功能,但它是一款很好的、可靠的Office产品。
显然,使用以下脚本用python 2重建LibreOffice也是可能的:https://gist.github.com/hbrunn/6f4a007a6ff7f75c0f8b