【OpenERP】Report 生成

以模块oecnj_trainning为例,模块路径: ~/openerp/addons/oecn_training/ ,以下简写为 path/oecn/

Report生成方法:(手写) rml + reportlab生成.

实践步骤:

1. 在 path/oecn/oecn_report.xml ,
<?xml version="1.0" encoding="utf-8"?>
<openerp>
    <data>
        <report
                id="report_oecn_training_lesson" # 标签 id
                name="oecn.lesson" # 标签名称, 是下文中oecn_print.py中的 "'report.oecn.lesson"中除去前面report.的部分.
                model="oecn.training.lesson" # 标签匹配的model
                rml="oecn_training/report/oecn_report.xml" # 使用到的 rml 文件
                string="打印课程" # 客户端上 显示此报表 的对应菜单的文本
                header="False" # 是否在本报表中显示公司统一的表头和表尾
                auto="False" />
    </data>
</openerp>

2. 在 path/oecn/__openerp__.py, 注意update_xml的值
# -*- coding: utf-8 -*-
{
        "name" : "OECN Training", #模块名
    "version" : "1.0", #模块版本
    "description" : 'OECN Training Demo', #模块说明
    "author" : "Shine IT", #作者
    "website" : "http://www.openerp.cn", #网址
    "depends" : [], #依赖的模块
    "update_xml" : ["lesson_view.xml",
                                        "lesson_report.xml",
                                        "report/oecn_report.rml"
        ], #模块更新的时候会读入的文件
    "installable" : True, #可否安装
    "category":'Generic Modules/Others' #模块类型
}




3. 新增path/oecn/report/ 文件夹, 其下有 __init__.py, oecn_print.py, oecn_report.rml 文件,
 3.1) __init__.py:
  import oecn_print

 3.2) oecn_print.py:
import time
from openerp.report import report_sxw

# rml parser 类
class rpt_oecn_training_lesson(report_sxw.rml_parse):
    def __init__(self, cr, uid, name, context):
        super(rpt_oecn_training_lesson, self).__init__(cr, uid, name, context)
        self.localcontext.update({"time": time,})
        
report_sxw.report_sxw('report.oecn.lesson', # 对象的内部名字,可以任意取, 以"report."开头
                      'oecn.training.lesson', # 与报表想关联的对象(model),报表数据通过该对象访问.
                      'addons/oecn_training/report/oecn_report.rml', # 与报表关联的RML文件名及路径.
                      parser=rpt_oecn_training_lesson, # 解析rml文件的对象。
                      header=False) # 表示要不要在本报表中显示公司统一的表头和表尾。


 3.3) oecn_report.rml , <template> 标签下必须对页面进行设置,否则在OpenERP运行会报错。
<?xml version="1.0" encoding="utf-8" standalone="no" ?>

<document filename="report.pdf">
    <!-- 报表页面设置 -->
    <template>
        <pageTemplate id="main">
            <frame id="first" x1="72" y1="72" width="451" height="698"/>
        </pageTemplate>
    </template>
    
    <!-- 报表页面样式设置 -->
    <stylesheet>
        <paraStyle name="Standard" fontName="Helvetica" fontSize="14.0" leading="16.0" alignment="CENTER" />
    </stylesheet>
    
    <!-- 报表页面正文-->
    <story>
        <para>[ repeatIn(objects,'o') ]</para>
        <para style="Standard">介绍</para>
        <para style="Standard">[ o.name ]</para>
    </story>
</document>

note:
 将 repeatIn( ) 放在 <section> tag 下,内容迭代不分页
 将 repeatIn( ) 放在 <story> tag 下,内容迭代分页
测试发现,objects 参数 必须在 <story>下,不然会报错。

4. 升级该模块, 就可以打印report。 由于 OpenERP 对中文支持不够, 输出的pdf文档会显示为黑块。 安装第三方模块oecn_base_fonts(安装说明)即可解决中文打印的问题:

原文地址:https://www.cnblogs.com/chjbbs/p/3777763.html