Python macro: Module not found

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
El Catala
Posts: 22
Joined: Mon Dec 07, 2020 6:29 pm

Python macro: Module not found

Post by El Catala »

Good morning,
I'm having trouble with a python script. This script works perfectly in a python editor as well as in apso.
When I launch it from a LibreOffice base form, I get the following error message:
Erreur d' exécution Basic
Une exception c' est produite:
Type: com.sun.star.script.provider.ScriptFrameworkErrorException
Message: <class 'ImportError'>:No module named 'achatvente' (or
'achatvente.cht.achat' is unknown)
File "/opt/libreoffice7.5/program/pythonscript.py", line 1057, in getscript
mod = self.provCtx.getModuleByUrl(fileUri)
File "/opt/libreoffice7.5/program/pythonscript.py", line 494, in getModuleByUrl
exec(code, entry.module._dict_)
File "/home/elfouster/.config/libreoffice/4/user/Scripts/python/
ordrebourse.py", line 3, in <module>
from achatvente.cht import achat
File "/opt/libreoffice7.5/program/uno.py", line 425, in_uno_import raise uno_import_exc
File "/opt/libreoffice7.5/program/uno.py", line 346, in_uno_import return_builtin_import(name *optargs, **kwargs)
Theachatvente folder containing the achat and vente modules is indeed in the same folder as the ordrebourse.py script.
Here is the original script:
#!/usr/bin/env python3
from achatvente.cht import achat
from achatvente.vnt import vente

def Control_Action(ev):
ctrl = ev.Source
frm = ev.Source.Model.Parent
subform = frm.getByName("F_Ctn")

try :
ordre = input ("Voulez-vous effectuer un achat d' action ? ")
if ordre == 'o' or ordre == 'O':
achat()
else :
vente()

except EOFError as e :
print (e)
Here you have the achat script:
def achat():
mntinvestissement = input("combien voulez-vous investir ? ")
a = int(mntinvestissement)
vlractions = input("Cote d' une action ? ")
valeur = float(vlractions)
x = int(a/valeur)
print("Vous pouvez acheter un maximum de " ,x, "actions")
nbractions = input("Combien voulez-vous acheter d' actions ? ")
# Calcul du montant des frais
xactions = int(nbractions)
mntcht = valeur * xactions
b = float(mntcht)
if b <= 500 :
frais = 1.95
elif b > 500 and b < 2000 :
frais = 3.90
elif b >= 2000 :
frais = b * 0.020

frsgst = float(frais)
montant = (xactions*valeur) + frsgst
mntachat = float(montant)
prixrevient = mntachat / xactions
prixunit = float(prixrevient)
ventemini = prixunit * 1.05
print ("Cout de l' opération : ",round(mntachat, 3), " €")
print ("Prix de revient unitaire :",round(prixrevient, 3), " €")
print ("prix de vente minimum : ", round(ventemini, 3), " €")


if __name__ == "__main__":
achat()
and the vente script:
def vente():
nbractions = input("Combien voulez-vous vendre d' actions ? ")
vlractions = input("Cote d' une action ? ")
xactions = int(nbractions)
valeur = float(vlractions)
# Calcul du montant des frais
mntvnt = valeur * xactions
b = float(mntvnt)
if b <= 500 :
frais = 1.95
elif b > 500 and b < 2000 :
frais = 3.90
elif b >= 2000 :
frais = b * 0.020
frsgst = float(frais)
recette = (xactions*valeur) - frsgst
mntvente = float(recette)
prixrevient = mntvente / xactions
prixunit = float(prixrevient)
print ("Bilan de l' opération : ", round(recette, 3), ' €')
print ("Prix de revient unitaire :", round(prixunit, 3), ' €')

if __name__ == "__main__":
vente()
Do you have an idea to work around this problem?

Cordially
Last edited by MrProgrammer on Tue Aug 01, 2023 5:50 pm, edited 1 time in total.
Reason: Edited topic's subject
LibreOffice 7.0.4 Xubuntu 20.4
Post Reply