[Solved] Call webservice with zeep with OpenOffice Python

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Isatis34
Posts: 15
Joined: Wed Jun 28, 2023 11:45 am

[Solved] Call webservice with zeep with OpenOffice Python

Post by Isatis34 »

Hello,

I have a issue when I call a webservice in Python throught zeep with OpenOffice running on Linux CentOS 7.9.2009.

OpenOffice 7.5.4.2 (64b) come with python 3.8.16, who is installed in /opt/libreoffice7.5/program/python :
/opt/libreoffice7.5/program/python -V
=> 3.8.16

I install an other python 3.8.16 in the standard way :
yum groupinstall "Development Tools" -y
yum install openssl-devel libffi-devel bzip2-devel -y
cd /tmp
wget https://www.python.org/ftp/python/3.8.1 ... 3.8.16.tgz
tar xf Python-*.tgz
cd /tmp/Python-3.8.16/
/tmp/Python-3.8.16/configure --enable-optimizations
make -j 8 altinstall
cd /usr/local/bin
/usr/local/bin/python3.8 -V
=> 3.8.16, so the other python is install and with the same version as /opt/libreoffice7.5/program/python

/usr/local/bin/python3.8 -m pip install --upgrade pip

/usr/local/bin/pip3.8 uninstall urllib3
/usr/local/bin/pip3.8 install urllib3==1.26.6
/usr/local/bin/pip3.8 install zeep
=> zeep-4.2.1 installed

I reinstall python for libreoffice in the same directory :
/tmp/Python-3.8.16/configure --enable-optimizations --prefix=/opt/libreoffice7.5/program

make -j 8 altinstall

/opt/libreoffice7.5/program/python /tmp/get-pip.py
/opt/libreoffice7.5/program/python -m pip install --upgrade pip
/opt/libreoffice7.5/program/bin/pip3.8 install --target=/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages urllib3==1.26.6
/opt/libreoffice7.5/program/bin/pip3.8 install --target=/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages zeep
=> zeep-4.2.1 installed

My code is :

import traceback
try:
import zeep
print("---------------------------------> Loads done !")
except:
print(traceback.format_exc())

As you can see, there is no call to a webservice, I get error at the line "import zeep" :
Traceback (most recent call last):
File "/tmp/testzeep02.py", line 3, in <module>
import zeep
File "/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages/zeep/__init__.py", line 1, in <module>
from zeep.client import AsyncClient, CachingClient, Client
File "/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages/zeep/client.py", line 6, in <module>
from zeep.transports import AsyncTransport, Transport
File "/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages/zeep/transports.py", line 11, in <module>
from zeep.utils import get_media_type, get_version
File "/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages/zeep/utils.py", line 5, in <module>
from lxml import etree
ImportError: cannot import name 'etree' from 'lxml' (/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages/lxml/__init__.py)

If I launch :
usr/local/bin/python3.8 "/tmp/testzeep02.py"
---------------------------------> Loads done !
so everything works fine.

If I launch /opt/libreoffice7.5/program/python "/tmp/testzeep02.py", I got this error.

I'm searching on this problem since 2 weeks and I don't find a solution.

What's wrong with my installation ?

Thanks for your help !
Last edited by robleyd on Sun Jul 23, 2023 9:12 am, edited 1 time in total.
Reason: Tagged [Solved]. Add green tick
LibreOffice 7.5.3.2 (x86 64), Windows 10
User avatar
Villeroy
Volunteer
Posts: 31279
Joined: Mon Oct 08, 2007 1:35 am
Location: Germany

Re: Call webservice with zeep with OpenOffice Python

Post by Villeroy »

/opt/libreoffice7.5/program/python might not be the Python runtime where you install any pip package.
And "OpenOffice Python" might be Python might be Python 2.7 which is completely outdated.

Recently on ask.libreoffice.org: https://ask.libreoffice.org/t/how-do-i- ... thon/47972
Please, edit this topic's initial post and add "[Solved]" to the subject line if your problem has been solved.
Ubuntu 18.04 with LibreOffice 6.0, latest OpenOffice and LibreOffice
Mountaineer
Posts: 318
Joined: Sun Sep 06, 2020 8:27 am

Re: Call webservice with zeep with OpenOffice Python

Post by Mountaineer »

I don't use CentOS, but some hints from what I know(?) from other versions.

Usually LibreOffice uses the python installed in the OS, when on linux, so I'm surprised there even is a python in /opt/libreoffice... Check if this is a symlink.

On Windows this is different, as a usual Windows has no python. As the python brought by LibreOffice has no pip, the extension in the Thread Villeroy linked may solve this, but would not not work in Linux.

My suspicion is something else: APSO hides Pythonpath, wich may be problem with imports you install.

To check: Run Idle from APSO and check the import-path and folders seen by python...
OpenOffice 3.1 on Windows Vista
Isatis34
Posts: 15
Joined: Wed Jun 28, 2023 11:45 am

Re: Call webservice with zeep with OpenOffice Python

Post by Isatis34 »

Hello,

Thank you for your quick answer !

To reply to Villeroy, I don't find the release note that indicate that Python 3.8.16 is installed with OpenOffice, but in https://wiki.documentfoundation.org/ReleaseNotes/7.0 is said that "Support for building against the obsolete CPython 2.7 has been removed, and scripts are now always executed on CPython 3 core commit c4fa6efa67775a6b333a4a5aa873b5cc24a4f7bc (Michael Stahl, CIB)"
So with OO 7.5.4.2 (64b), there is a python 3.8.16 in /opt/libreoffice7.5/program and folder named "python3.8" in /opt/libreoffice7.5/program/lib, both installed by OO installer. It's not a Symlink.

On Windows, everything works fine. My problem is to use my code in Linux.

I wrote this python script to check path :

Code: Select all

import traceback
import sys

USE_LIBREOFFICE = (__name__ != '__main__')

try:
	import uno
except ImportError:
	pass

def InsertText(text):
    desktop = XSCRIPTCONTEXT.getDesktop()
    model = desktop.getCurrentComponent()

    if not hasattr(model, "Text"):
        return

    xModel = XSCRIPTCONTEXT.getDocument()
    xSelectionSupplier = xModel.getCurrentController()

    xIndexAccess = xSelectionSupplier.getSelection()
    count = xIndexAccess.getCount()

    if count >= 1:  # ie we have a selection
        i = 0
    while i < count:
        xTextRange = xIndexAccess.getByIndex(i)
        theString = xTextRange.getString()

        if not len(theString):
            xText = xTextRange.getText()
            xWordCursor = xText.createTextCursorByRange(xTextRange)
            xWordCursor.setString(text)
			
        else:
            xTextRange.setString(text)

        i += 1

def GetSystemInfos():
	Buffer = "Python: " + sys.executable + " version " + sys.version
	Buffer += "\nPath: " + str(sys.path)
	
	if USE_LIBREOFFICE:
		InsertText(Buffer)
	else:
		print(Buffer)

if not USE_LIBREOFFICE:
	GetSystemInfos()

# Make InsertHello visible by the Macro Selector
g_exportedScripts = (GetSystemInfos, )
I run it a Linux prompt (no issue with zeep) :
/usr/local/bin/python3.8 "/home/laurent/.config/libreoffice/4/user/Scripts/python/testsystem.py"
=>
Python: /usr/local/bin/python3.8 version 3.8.16 (default, Jul 20 2023, 09:24:40)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]
Path: ['/home/laurent/.config/libreoffice/4/user/Scripts/python', '/usr/local/lib/python38.zip', '/usr/local/lib/python3.8', '/usr/local/lib/python3.8/lib-dynload', '/usr/local/lib/python3.8/site-packages']

I run it in OpenOffice (the script is bind to a menu) :
Python: version 3.8.16 (default, Jun 1 2023, 17:48:07)
[GCC 7.3.1 20180303 (Red Hat 7.3.1-5)]
Path: ['/opt/libreoffice7.5/program/python-core-3.8.16/lib', '/opt/libreoffice7.5/program/python-core-3.8.16/lib/lib-dynload', '/opt/libreoffice7.5/program/python-core-3.8.16/lib/lib-tk', '/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages', '/opt/libreoffice7.5/program', '/opt/libreoffice7.5/program/python-core-3.8.16/lib/python38.zip', '/opt/libreoffice7.5/program/python-core-3.8.16/lib/python3.8', '/opt/libreoffice7.5/program/python-core-3.8.16/lib/python3.8/lib-dynload', '/opt/libreoffice7.5/program/python-core-3.8.16/lib/python3.8/site-packages', '/opt/libreoffice7.5/share/extensions/dict-en/pythonpath']

lxml is in "/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages" and "/opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages" is in Python path.
LibreOffice 7.5.3.2 (x86 64), Windows 10
Isatis34
Posts: 15
Joined: Wed Jun 28, 2023 11:45 am

Re: Call webservice with zeep with OpenOffice Python

Post by Isatis34 »

zaz-pip doesn't seem to run on Linux.
2023-07-21_09-28-55.jpg
2023-07-21_09-28-55.jpg (101.22 KiB) Viewed 3542 times
LibreOffice 7.5.3.2 (x86 64), Windows 10
Mountaineer
Posts: 318
Joined: Sun Sep 06, 2020 8:27 am

Re: Call webservice with zeep with OpenOffice Python

Post by Mountaineer »

Isatis34 wrote: Fri Jul 21, 2023 8:43 am ...
To reply to Villeroy, I don't find the release note that indicate that Python 3.8.16 is installed with OpenOffice, ...
He was reacting on your
Isatis34 wrote: Thu Jul 20, 2023 9:42 am ...
I have a issue when I call a webservice in Python throught zeep with OpenOffice running on Linux CentOS 7.9.2009.
...
Actually the Version 7.5.4.2 (64b) you give, tells the above mention of OpenOffice is false, you are working wuth LibreOffice, as OpenOffice is still at version 4.x
OpenOffice 3.1 on Windows Vista
Isatis34
Posts: 15
Joined: Wed Jun 28, 2023 11:45 am

Re: Call webservice with zeep with OpenOffice Python

Post by Isatis34 »

Hello,

You are right, I mixed OpenOffice with LibreOffice, as I founded so many resources in this forum to build my project.

I'm wonder if I have to post my issue in a Python forum. Maybe OpenOffice nor LibreOffice are not the cause of my issue.
LibreOffice 7.5.3.2 (x86 64), Windows 10
Isatis34
Posts: 15
Joined: Wed Jun 28, 2023 11:45 am

Re: Call webservice with zeep with OpenOffice Python

Post by Isatis34 »

Hello,

I think I fix my issue with this : https://stackoverflow.com/questions/109 ... om-so-file

I created this file : sudo nano /opt/libreoffice7.5/program/python-core-3.8.16/lib/site-packages/lxml/etree.py

Code: Select all

def __bootstrap__():
   global __bootstrap__, __loader__, __file__
   import sys, pkg_resources, imp
   __file__ = pkg_resources.resource_filename(__name__,'etree.cpython-38-x86_64-linux-gnu.so')
   __loader__ = None; del __bootstrap__, __loader__
   imp.load_dynamic(__name__,__file__)
__bootstrap__()
and everything seems to work now.

Thanks to all !
LibreOffice 7.5.3.2 (x86 64), Windows 10
User avatar
karolus
Volunteer
Posts: 1160
Joined: Sat Jul 02, 2011 9:47 am

Re: [Solved] Call webservice with zeep with OpenOffice Python

Post by karolus »

Hello
Instead of this questionable "bootstrap-acrobatic" I would just install Libreoffice from CentOS sources:

Code: Select all

# yum install libreoffice -y
This way your libreoffice will probably use the python environment of your operating system, and all problems with "pip" are solved robustly.
AOO4, Libreoffice 6.1 on Rasbian OS (on ARM)
Libreoffice 7.4 on Debian 12 (Bookworm) (on RaspberryPI4)
Libreoffice 7.6 flatpak on Debian 12 (Bookworm) (on RaspberryPI4)
Post Reply