Python 如何在不使用向导的情况下从窗体视图打印excel文件

Python 如何在不使用向导的情况下从窗体视图打印excel文件,python,odoo,odoo-10,xls,xlwt,Python,Odoo,Odoo 10,Xls,Xlwt,我需要在不使用向导的情况下从表单视图打印excel文件。问题是,当按下生成\u bt时,相同的表单视图显示为一个框 代码 将target更改为current或从报税表中删除密钥,因为current是默认值。我尝试删除密钥并将target设置为current。但加载后,不会生成任何文件。当我点击那个按钮时,如何自动下载那个文件?也许会对你有所帮助。 # -*- coding: utf-8 -*- from odoo import models, fields, api from odoo.too

我需要在不使用向导的情况下从表单视图打印excel文件。问题是,当按下
生成\u bt
时,相同的表单视图显示为一个框

代码


target
更改为
current
或从报税表中删除密钥,因为
current
是默认值。

我尝试删除密钥并将
target
设置为
current
。但加载后,不会生成任何文件。当我点击那个按钮时,如何自动下载那个文件?也许会对你有所帮助。
# -*- coding: utf-8 -*-

from odoo import models, fields, api
from odoo.tools.translate import _
from odoo.exceptions import UserError, AccessError
from datetime import datetime, timedelta
import dateutil.parser
import base64
import xlwt
from cStringIO import StringIO
from pprint import pprint
import logging
from openerp import tools
_logger = logging.getLogger(__name__)


class SerialNumberXls(models.Model):
    _inherit = 'stock.picking'
    excel_file = fields.Binary(string='Download Report Excel',readonly="1")
    file_name = fields.Char(string='Excel File',readonly="1")

    def generate_bt(self):
        workbook= xlwt.Workbook(encoding="UTF-8")
        filename='SerialNumbers.xls'
        sheet= workbook.add_sheet('Serial Number',cell_overwrite_ok=True)
        style = xlwt.easyxf('font:name Arial,height 200;')
        style2 = xlwt.easyxf('font:name Arial,height 200,bold True;align: horiz center, vert center;')
        style_filter = xlwt.easyxf('font:name Arial; align: horiz center, vert center;')
        style_normal_left = xlwt.easyxf('font:name Arial; align: horiz left, vert center;')
        style_normal_right = xlwt.easyxf('font:name Arial; align: horiz right, vert center;')
        row = 0
        col = 0

        for line in self.move_lines:
            print "EEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEEE"
            for product in line.quant_ids:
                print "SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS"
                col = 0
                product_id = product.product_id and product.product_id.name or False
                sl_no = product.lot_id and product.lot_id.name or False
                sheet.write(row,col,product_id,style)
                col = col + 1
                sheet.write(row,col,sl_no,style)
                row = row + 1

        fp = StringIO()
        workbook.save(fp)
        excel_file = base64.encodestring(fp.getvalue())
        self.excel_file = excel_file
        self.file_name =filename
        fp.close()  

        return {
          'view_type': 'form',
          "view_mode": 'form',
          'res_model': 'stock.picking',
          'res_id': self.id,
          'type': 'ir.actions.act_window',
          }