py30报表
1. https://www.odoo.com/apps/modules/12.0/report_py3o/
2. 根据 readme 文件安装依赖模块
pip3 install py3o.template
pip3 install py3o.formats (若报错,升级 setuptools:pip3 install --upgrade setuptools)
3. 安装 libreoffice(需要6.0以上版本)
apt-get update && apt-get -y --no-install-recommends install libreoffice
4. 配置系统参数 py3o.conversion_command
libreoffice执行文件全路径, windows:
win: C:\Program Files\LibreOffice\program\soffice.exe
Linux: /usr/bin/libreoffice
5. 打印模板 ODT 设置
a) 控制语句:for,if,with
b) 变量定义:Ctrl + F2,用户字段,名称填字段,值填显示的名称
c) 输入字段:Ctrl + F2,选功能页签中的输入字段(Tips: 新版本中的字段对话框可以连续操作)
参照:py3o://function="format_multiline_value(objects.name)"
审阅字段:输入字段标识
6. PDF在线预览
@route()
def report_routes(self, reportname, docids=None, converter=None, **data):
if converter != 'py3o':
return super(ReportController, self).report_routes(
reportname=reportname, docids=docids, converter=converter,
**data)
context = dict(request.env.context)
if docids:
docids = [int(i) for i in docids.split(',')]
if data.get('options'):
data.update(json.loads(data.pop('options')))
if data.get('context'):
# Ignore 'lang' here, because the context in data is the
# one from the webclient *but* if the user explicitely wants to
# change the lang, this mechanism overwrites it.
data['context'] = json.loads(data['context'])
if data['context'].get('lang'):
del data['context']['lang']
context.update(data['context'])
ir_action = request.env['ir.actions.report']
action_py3o_report = ir_action.get_from_report_name(
reportname, "py3o").with_context(context)
if not action_py3o_report:
raise exceptions.HTTPException(
description='Py3o action report not found for report_name '
'%s' % reportname)
res, filetype = action_py3o_report.render(docids, data)
filename = action_py3o_report.gen_report_download_filename(
docids, data)
if not filename.endswith(filetype):
filename = "{}.{}".format(filename, filetype)
content_type = mimetypes.guess_type("x." + filetype)[0]
http_headers = [('Content-Type', content_type),
('Content-Length', len(res))
# ('Content-Disposition', content_disposition(filename))
]
return request.make_response(res, headers=http_headers)
7. 修改 function 方法
可以修改实现方法,例如 o_format_date 方法的实现
8. 增加二维码
增加一个抽象类: py3o.barcode.mixin, 或者,扩展下base_model
@api.multi
def get_barcode_content(self, attr_name, type, width, height):
"""
获取字符串二维码,可以做成抽象类
:param attr_name:
:param type:
:param width:
:param height:
:return:
"""
if not hasattr(self, attr_name):
raise UserError('属性字段 %s 不存在!' % attr_name)
content = getattr(self, attr_name, 'N/A')
barcode = self.env['ir.actions.report'].barcode(type, content, width=width, height=height,
humanreadable=True)
return base64.b64encode(barcode).decode('ascii')
在模板中增加交互式框架,并指定Name属性:
py3o.image(obj.get_barcode_content('name', 'QR', 80), 'png', width='80px', height='80px', keep_ratio=False, isb64=True)
注意:条码、二维码尺寸的匹配,keep_ratio及isb64参数的匹配
框架可以设置自动调整尺寸以及最小尺寸
================================================================
帮助:http://py3otemplate.readthedocs.io/en/latest/templating.html
参考模板:https://github.com/akretion/odoo-py3o-report-templates
Debian 系统安装最新的 LibreOffice:
https://www.linuxidc.com/Linux/2018-02/150913.htm