py3o中数字金额转大写

py3o模板中利用libroffice中的calc来处理odoo中的模板打印
${eval("__import__('num2words')").num2words(round(sum(lowes.product_uom_qty * lowes.price_unit for lowes in objects.order_line if lowes.client_order_ref == order_ref), 2), to='currency', lang='en')}
round(sum(lowes.product_uom_qty * lowes.price_unit for lowes in objects.order_line if lowes.client_order_ref == order_ref), 2)是计算公式,得到的是一个数字。把这个数字转换成英文大写,如:
数字输出
1234.56 one thousand, two hundred and thirty-four point five six (不带单位)
1234.56(用 currency) one thousand, two hundred and thirty-four dollars and fifty-six cents

✅ 注意事项:

  • 如果用 to='currency'语言一定要是 'en',不要用 'zh' 否则报错。

  • 如果不加 to='currency',默认是纯英文数字大写,没有“美元”单位。

你可以通过 传入币种 作为参数来控制显示不同的货币。

${eval("__import__('num2words')").num2words(round(sum(lowes.product_uom_qty * lowes.price_unit for lowes in objects.order_line if lowes.client_order_ref == order_ref), 2), to='currency', lang='en', currency=currency_type)}

假设你希望根据传入的币种类型来显示不同的货币格式。首先,你可以使用这样的模板代码:
${currency_type == 'CNY' and '人民币' + eval("__import__('num2words')").num2words(round(sum(lowes.product_uom_qty * lowes.price_unit for lowes in objects.order_line if lowes.client_order_ref == order_ref), 2), to='currency', lang='zh') or
 currency_type == 'USD' and 'USD ' + eval("__import__('num2words')").num2words(round(sum(lowes.product_uom_qty * lowes.price_unit for lowes in objects.order_line if lowes.client_order_ref == order_ref), 2), to='currency', lang='en') or
 currency_type == 'EUR' and 'EUR ' + eval("__import__('num2words')").num2words(round(sum(lowes.product_uom_qty * lowes.price_unit for lowes in objects.order_line if lowes.client_order_ref == order_ref), 2), to='currency', lang='en')}

 

✅ 解释:

  1. 动态币种判断

    • currency_type'CNY' 时,显示 人民币

    • currency_type'USD' 时,显示 美元

    • currency_type'EUR' 时,显示 欧元

  2. num2words 的语言和货币处理

    • 使用 num2words 模块将数字转换为指定语言的货币大写。

  3. 传递 currency_type

    • 你需要在 Odoo 的报告上下文中,确保把币种类型(如 'CNY''USD''EUR')传递到模板中。

posted @ 2025-07-03 09:21  *感悟人生*  阅读(41)  评论(0)    收藏  举报