【问题标题】:python + wx & uno to fill libreoffice using ubuntu 14.04python + wx & uno 使用 ubuntu 14.04 填充 libreoffice
【发布时间】:2016-03-31 14:58:05
【问题描述】:

我使用 wx python gui 收集用户数据,然后使用 uno 将这些数据填充到 ubuntu 10.xx 下的 openoffice 文档中

user + my-script (+empty document) --> 预填充文档

升级到 ubuntu 14.04 后,uno 不再适用于 python 2.7,现在我们在 ubuntu 中有 libreoffice 而不是 openoffice。当我尝试运行我的 python2.7 代码时,它说:

ImportError: No module named uno

我怎样才能让它恢复工作?

我尝试了什么: 安装https://pypi.python.org/pypi/unotools v0.3.3 sudo apt-get install libreoffice-script-provider-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 一起运行? 我还能尝试什么?

【问题讨论】:

    标签: python-2.7 python-3.x wxpython libreoffice uno


    【解决方案1】:

    在 LibreOffice 中创建一个 python 宏,该宏将完成将数据插入 LibreOffice 的工作,然后在您的 python 2.7 代码中调用该宏。
    由于宏是从 LibreOffice 运行的,它将使用 python3。 以下是如何从命令行调用 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 Document\ndirectly 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 
    

    您当然需要调整代码以接受数据作为参数、从文件中读取数据或其他任何方式。

    【讨论】:

      【解决方案2】:

      理想情况下,我会说使用 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

      【讨论】:

      • 我尝试按照以下说明安装 pip3:[link]askubuntu.com/questions/412178/… 之后我尝试了您的 pip3 命令并收到此错误:/usr/bin/python3 -u build.py build
      • 最终出现此错误:命令“/usr/bin/python3 -u build.py build”失败,退出代码为 1。--------------- ----------------------- 清理... 命令 /usr/bin/python3 -c "import setuptools, tokenize;__file__='/tmp/pip_build_root /wxPython-Phoenix/setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\ n'), file, 'exec'))" install --record /tmp/pip-apne5cii-record/install-record.txt --single-version-externally-managed --compile failed /tmp/pip_build_root/wxPython-Phoenix 中的错误代码为 1 在 /home/moddi/.pip/pip.log 中存储故障的调试日志
      • import wx ... 仍然失败,我不明白,为什么 AOO 更好???我的问题是,我没有导入 uno 和 wx 的 python 发行版。我不介意将代码升级到更新的 python ......只想要一个库的组合来完成这项工作。
      • 我希望 AOO 将使用 /usr/bin/python,而不是 /usr/bin/python3。它在某些系统上以这种方式工作。当你在终端中输入python 会发生什么——它会给你一个python 2 版本吗?如果是这样,那么您可以为 python 2 安装 wx 吗?如果两个问题的答案都是肯定的,那么它应该可以工作。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-19
      • 1970-01-01
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多