Python giving AttributeError: loadComponentFromUrl

Java, C++, C#, Delphi... - Using the UNO bridges
Post Reply
emil_blad
Posts: 1
Joined: Thu Jan 06, 2022 4:25 pm

Python giving AttributeError: loadComponentFromUrl

Post by emil_blad »

Hello,

Have been looking for a solution to this problem for a while now and have not come up with a solution.

Trying to open a calc document with OO-python and getting
AttributeError: loadComponentFromUrl
error

Running on Windows 10 installation.
First trying with OpenOffice 4.1.11 and included python.
Then tying with LibreOffice 7.2.4.1 (x64) version.

Both gives the same error.

Have tried with the "URE_BOOTSTRAP" and changing the spaces to %20 but no success.

Code: Select all

C:\Program Files\LibreOffice\program>.\python
Python 3.8.10 (default, Dec  3 2021, 01:18:43) [MSC v.1928 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> import os
>>> import uno
>>> import unohelper
>>> localContext = uno.getComponentContext()
>>> resolver = localContext.ServiceManager.createInstanceWithContext(
... "com.sun.star.bridge.UnoUrlResolver", localContext )
>>> ctx = resolver.resolve( "uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext" )
>>> smgr = ctx.ServiceManager
>>> desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)
>>> model = desktop.getCurrentComponent()
>>> sURL = "e:\Test.ods"
>>> systemURL = uno.systemPathToFileUrl(sURL)
>>> oDocument = desktop.loadComponentFromUrl(systemURL,"_blank",0, tuple())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: loadComponentFromUrl
>>> print(os.environ["URE_BOOTSTRAP"])
file:///C:/Program%20Files/LibreOffice/program/fundamental.ini
Update:
So, made a VM with Ubunto and LibreOffice 6.4.7.2 and get the same AttributeError: loadComponentFromUrl as with Windows.
OpenOffice 4.1.11, LibreOffice 7.2.4.1(x64)
Windows 10
Bernard Mouille
Posts: 10
Joined: Fri Dec 31, 2021 9:42 pm

Re: [Python] giving AttributeError: loadComponentFromUrl

Post by Bernard Mouille »

Hello,

It is not exacly your answer.
It is an external Pyton script.
Sorry for my write, I am beginner in Python from 2022-01-08.
The comments arent in french ( fr-FR ) lang.

Regards,
Bernard.

Code: Select all


# -*- coding: utf-8 -*-

#. // CalcOpen.
#. // Exemple de d'ouverture d'un fichier ouvert avec Libre Office calc.
#. // Je suis débutant depuis le 2022-01-08, veuillez excuser mon écriture.

import os
#. // pip install pywin32
import win32com.client
#. // pip install PyMsgBox
import pymsgbox

#. // Programme maître.
def CalcOpen():
   oServiceManager = win32com.client.Dispatch( "com.sun.star.ServiceManager" )
   oServiceManager._FlagAsMethod( "Bridge_GetStruct" )

   #. // Sélection d'un fichier Calc.
   cFileOds = ca_GetFile( oServiceManager )
   if len( cFileOds ) == 0:
      pymsgbox.alert( "Pas de fichier sélectionné, abandon", "Erreur" )
      return -1
   # End if

   #. // Crée les paramètres d'ouverture.
   args1 = lo_MakeProperty( oServiceManager, "Hidden"            , False )
   args2 = lo_MakeProperty( oServiceManager, "ReadOnly"          , False )
   args3 = lo_MakeProperty( oServiceManager, "MacroExecutionMode", 0     )

   #. // Ouverture de Libre Office sélectionné avec désactivation des macros.
   oDesktop = oServiceManager.createInstance( "com.sun.star.frame.Desktop"  )
   oDoc     = oDesktop.loadComponentFromURL( cFileOds, "_blank", 0, [ args1, args2, args3 ] )

   oServiceManager = None
   return 0

#. // Sélectionne un nom de fichier à ouvrir avec Writer au format URL.
def ca_GetFile( oServiceManager ):
   cDir      = ""
   cDirWork  = os.getcwd()
   oFso      = win32com.client.Dispatch( "Scripting.FileSystemObject" )
   if not oFso.FolderExists( cDirWork + "\Parameters" ):
          oFso.CreateFolder( cDirWork + "\Parameters" )
   else:
      EndIf = None
   if oFso.FileExists( cDirWork + "\Parameters\LastDir.txt" ):
      if oFso.Getfile( cDirWork + "\Parameters\LastDir.txt" ).Size > 0:
         oFile = oFso.OpenTextFile( cDirWork + "\Parameters\LastDir.txt" )
         cDir  = oFile.Readline()
         oFile.Close()
         if not oFso.FolderExists( cDir ):
            cDir = ""
         else:
            EndIf = None
      else:
         EndIf = None
   else:
      EndIf = None
   if len( cDir ) == 0 :
      cDir = cDirWork
   else:
      EndIf = None
   oFilePicker = oServiceManager.CreateInstance( "com.sun.star.ui.dialogs.FilePicker" )
   args = [ None ]
   # FILEOPEN_SIMPLE = 0
   oFilePicker.initialize( args )
   oFilePicker.setDisplayDirectory( cDir )
   oFilePicker.appendFilter( "Tous les fichiers (*.* )", "*.*" )
   oFilePicker.appendFilter( "Calc (*.ods )"      , "*.ods" )
   oFilePicker.appendFilter( "Excel (*.xlsx, *.xls )"   , "*.xlsx;*.xls" )
   cUrl = ""
   if oFilePicker.Execute() == 1:
      cUrl = oFilePicker.Files[ 0 ]
      cDir = by_UrlToFile( cUrl )
      oFile = oFso.CreateTextFile( cDirWork + "\Parameters\LastDir.txt", True )
      oFile.Writeline( cDir[ :cDir.rfind( "\\" ) ] )
      oFile.Close()
   else:
      EndIf = None
   oFso = None
   return cUrl

#. // Cree une propriete.
def lo_MakeProperty( oServiceManager, cName, xValue ):
   oProperty       = oServiceManager.Bridge_GetStruct( "com.sun.star.beans.PropertyValue" )
   oProperty.Name  = cName
   oProperty.Value = xValue
   return oProperty

#. // Détruire un fichier.
def by_ferase( cFile ):
   if os.path.isfile( cFile ):
      os.remove( cFile )
   # End if
   return 0

#. // Convertit un nom de fichier URL en nom Windows.
def by_UrlToFile( cUrl ):
   cFile = cUrl
   cFile = cFile.replace( "file:///", "" )
   cFile = cFile.replace( "/", "\\" )
   cFile = cFile.replace( "%20", " " )
   cFile = cFile.replace( "|", ":" )
   return cFile

CalcOpen()

libre Office 7.2.4.1(x64) Windows 8
Post Reply