2
This question refers to the use of macros in python in Libreoffice.
I’ve been doing some studies on the subject and found that one of the ways to add a Python-written macro to Libreoffice would be to put it in the directory: ~/.config/libreoffice/4/user/Scripts/python
.
So far so good, but I’d like to take a step forward. When developing larger programs, it is often necessary to divide it into several modules. Consequently we would have, in the case of mod_A.py and mod_B.py modules in the same project:
import mod_B # Estando em mod_A
However, when I try to run the mod_A macro through [Tools > Organize Macros > Python] > mod_A > uma_das_functions I get a big error message informed that mod_B was not found.
com.sun.star.uno.Runtimeexceptionerror During invoking Function main in module file://usr/lib/libreoffice/share/Scripts/python/Balanco.py (name 'getMovimentGeral' is not defined /usr/lib/libreoffice/share/Scripts/python/Balanco.py:41 in Function main() [movement = getMovimentGeral(model, Sheets)] /usr/lib/libreoffice/program/pythonscript.py:869 in Function invoke() [Ret = self.func( *args )] )
complementing. I inform you that Balanco.py
and Movimento.py
are in the same folder as above.
In summary: I would like to know if anyone has knowledge about how to import a project module into a python macro developed for Libreoffice / Openoffice.
Actually, by my mistake, I did not realize that the error message I transcribed above was not an import error. However, I assure you that in subsequent tests I received messages stating that Movement was not found. Anyway, I realized by the link you sent me, that I could add the path to the macro folder to sys.path. So I inserted the following command at the beginning of the program:
sys.path.append('/home/jorge/.config/libreoffice/4/user/Scripts/python/')
which is the location of macros in Libreoffice / Openoffice. Then I inserted the import of the module desired.
Perhaps you’d better enter the code from the main module.
`Arquivo: Balanco.py
@author: Jorge Luiz
Aquivo principal da aplicação (teste) para totalização do balanço geral de uma planliha
voltada para controle de despesas de um condoínio.
Há uma aba para cada mês do ano, sendo que o balanço final do mês é transferido para o mês seguinte.
@note: A coluna dos valores é "C"
@attention: foi encontrada uma dificuldade na execução de import de módulos locais, no caso Movimento.py
o que foi resolvido adicionando-se o caminho do local do script a sys.path
Este é o arquivo pricipal da aplicação / macro para rodar numa planilha específica do Calc.
Depende do módulo Movimento
'''
import sys
from datetime import date
lSheets = ('JAN', 'FEV', 'MAR', 'ABR', 'MAI', 'JUN', 'JUL', 'AGO', 'SET',
'OUT', 'NOV', 'DEZ')
__debug = False #Mudar __debug para False quando for distribuir.
def init():
'''
Obter model no contexto de uma instância do LibreOffice já executando.
@todo: Tendo em vista que esta é uma macro para um arquivo específico:
1: Tentar inserir a macro no próprio arquivo OU
2. Fazer a verificação do nome do arquivo.
'''
desktop = XSCRIPTCONTEXT.getDesktop()
model = desktop.getCurrentComponent()
#Graças ao comando abaixo é possível reconhecer os imports (import Movimento)
#Obs: não aceita caminho: ~/
sys.path.append('/home/jorge/.config/libreoffice/4/user/Scripts/python/')
return model
def main():
# access the active sheet
if not __debug:
model = init()
else:
#Quando em modo de depuração.
import initOO
model = initOO.initOO()
import Movimento #Esse import deve ficar após as inicializções.
if not model:
print('Falha ao obter model')
sys.exit(1)
anoSimpl = str(date.today().year)[-2:]
nomesSheets = []
for sheet in lSheets:
nomesSheets.append(sheet + ' ' + anoSimpl)
print('Lendo Planilhas...')
lMov = Movimento.getMovimentoGeral(model, nomesSheets)
print('Escevendo movimento financeiro')
sheetResumo = model.Sheets.getByName('RESUMO')
Movimento.escreveMovimento(sheetResumo, lMov)
print('Planilha%12s' % 'VALOR')
valorTotal = 0.0
for mov in lMov:
print(mov['sheet'])
valorMensal = 0.0
for valor in mov['movimento']:
valorMensal += valor
valorTotal += valor
print ('%21.2f' % valor)
print('Saldo no mês:%8.2f\n' % valorMensal)
print('Saldo final %9.2f' % valorTotal)
receita,despesa = Movimento.getReceitasDespesas(lMov)
print('RESUMO ---------------------------------')
print('Receita:%13.2f\ndespesa:%13.2f\nsaldo: %13.2f' %(receita, despesa, receita + despesa))
if __name__ == '__main__': #Essa parte é só para debug.
main()
print ('FIM')
g_exportedScripts = main,
`
This way I was able to make the macro run in Libreoffice inside the desired file. So I think the problem is solved.
I really made a mistake when copying the message, but I assure you that I had previously received an error message in import. I think the difference in this issue is that it is Python code to be executed as macro in a Libreoffice / Openoffice file. Normally there would be no error in import, but by some peculiarity in the macros environment arises this disconcerting error.
– Jorge Luiz