I need the current directory of the file, without the file name. I made a few attempts but failed.
# -*- coding: cp1252 -*-
from __future__ import unicode_literals
import uno
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
ODOC = XSCRIPTCONTEXT.getDocument()
def create_instance(name, with_context=False):
if with_context:
instance = SM.createInstanceWithContext(name, CTX)
else:
instance = SM.createInstance(name)
return instance
def apsoConsole(event=None):
"""
https://extensions.libreoffice.org/extensions/apso-alternative-script-organizer-for-python
"""
create_instance("apso.python.script.organizer.impl")
from apso_utils import console
#console()
console(BACKGROUND=0x0, FOREGROUND=0xD3D7CF)
return
import os
def mytest(*args):
apsoConsole()
var4 = ''
var1 = os.path.realpath(__file__)
var2 = os.path.abspath(os.getcwd())
var3 = ODOC.getURL()
#var4 = ConvertFromURL(var3)
print(var1, '\n')
print(var2, '\n')
print(var3, '\n')
print(var4, '\n')
Result:
/home/MY_USER/vnd.sun.star.tdoc:/13/Scripts/python/Module.py
/home/MY_USER
file:///home/MY_USER/%C3%81rea%20de%20Trabalho/p3/testep3.ods
Complement
I will use information from an ods file to insert into an odt file using the py3o.template module (instaled via Zaz-pip - elmau/zaz-pip: Extension for install and admin Python Pip in LibreOffice. - zaz-pip - Git para Cuates)
The encoding I use is cp1252.
The macro will take the ods file directory and manipulate an odt template file that is in the same directory. And, especially on Linux, I need to provide the full path to the file.
On Linux my directory is written like this:
/home/MY_USER/Área de Trabalho/p3/Instituição
But when using “.getURL ()” the text is encoded as if it were a url and is displayed like this, replacing the non-utf-8 characters:
file:///home/MY_USER/%C3%81rea%20de%20Trabalho/p3/Institui%C3%A7%C3%A3o
But if I use this string like this:
/home/MY_USER/%C3%81rea%20de%20Trabalho/p3/Institui%C3%A7%C3%A3o
The operating system cannot find the directory. It only finds the directory if it is written that way
/home/MY_USER/Área de Trabalho/p3/Instituição
I am currently using the following code to do this, but I was wondering if there is anything more simple and direct.
# -*- coding: cp1252 -*-
from __future__ import unicode_literals
import uno
import os
import platform
import urllib.parse
CTX = uno.getComponentContext()
SM = CTX.getServiceManager()
ODOC = XSCRIPTCONTEXT.getDocument()
OS_PATAFORM = platform.system()
def mytest2(*args):
full_path = os.path.dirname(ODOC.getURL())
full_path_mod = full_path.replace('file:///', '')
separator = '/'
if OS_PATAFORM == 'Windows' :
dir_path = f"""{full_path_mod}{separator}"""
else:
dir_path = f"""{separator}{full_path_mod}{separator}"""
dir_path = urllib.parse.unquote(dir_path)