我正在尝试使用xlwt开发excel报告。不幸的是我收到了以下错误,即"属性错误:' NoneType'对象没有属性' fields_view_get'"来自以下代码:
Xlwt在odoo v8中报告代码。
使用xlwt生成excel文件。
import xlwt
from StringIO import StringIO
import base64
from datetime import datetime
from xlwt import Workbook,easyxf
from openerp import models, fields, api
class all_invoice_wizard(models.TransientModel):
_name = 'all.invoice.wizard'
from_date = fields.Date('From Date', help='Select Start Date')
to_date = fields.Date('To Date', help='Select End Date')
def initialize_report(self, data):
'''This method initializes the excel report'''
fl = StringIO()
wbk = xlwt.Workbook(encoding = 'utf-8')
sheet = wbk.add_sheet('Sheet 1',cell_overwrite_ok=True)
return fl,wbk,sheet
def generate_xlwt_report_file(self, report_name, fl, wbk, sheet):
'''This method generates the report file.'''
wbk.save(fl)
fl.seek(0)
buf = base64.encodestring(fl.read())
ctx = dict(self._context)
ctx.update({'file': buf, 'report_name': report_name})
try:
form_id = self.env['ir.model.data'].get_object_reference('account', 'wiz_excel_file_download_form_view')[1]
except ValueError:
form_id = False
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'wizard.excel.report.file',
'views': [(form_id, 'form')],
'view_id': form_id,
'target': 'new',
'context': ctx,
}
@api.multi
def print_invoice(self):
data = self.read()[0]
ctx = self._context
#Initialize the Report
fl,wbk,sheet = self.initialize_report(data)
#Report Formatting
bold_center = xlwt.easyxf('font: bold 1; align: vert centre, horiz center')
sheet.header_str = 'Invoice Report: '+self.from_date+' - '+self.to_date
sheet.col(2).width = 6000
sheet.col(3).width = 5000
sheet.col(5).width = 7000
sheet.col(7).width = 7000
#Report Heading
sheet.write(0, 0, "Sl No", bold_center)
sheet.write(0, 1, "Invoice No.", bold_center)
sheet.write(0, 2, "Date", bold_center)
sheet.write(0, 3, "Location", bold_center)
sheet.write(0, 4, "Taxable Value", bold_center)
sheet.write(0, 5, "SGST", bold_center)
sheet.write(0, 6, "CGST", bold_center)
sheet.write(0, 7, "IGST", bold_center)
sheet.write(0, 8, "Total Taxes", bold_center)
sheet.write(0, 9, "TCS", bold_center)
sheet.write(0, 10, "Total Invoice Value", bold_center)
#Report Data
invoice_data = self.env['account.invoice'].search([('date_invoice','>=',self.from_date),('date_invoice','<=',self.to_date)])
if invoice_data:
bulk_invoice_data = []
for test in invoice_data:
single_invoice_data = []
single_invoice_data.append(test.number)
single_invoice_data.append(test.date_invoice)
single_invoice_data.append(test.issuing.name)
single_invoice_data.append(test.amount_untaxed)
for invoice_computation_lines in test.tax_line:
if 'SGST' in invoice_computation_lines.name:
single_invoice_data.append(invoice_computation_lines.amount)
else:
single_invoice_data.append(0.00)
if 'CGST' in invoice_computation_lines.name:
single_invoice_data.append(invoice_computation_lines.amount)
else:
single_invoice_data.append(0.00)
if 'IGST' in invoice_computation_lines.name:
single_invoice_data.append(invoice_computation_lines.amount)
else:
single_invoice_data.append(0.00)
single_invoice_data.append(test.amount_tax)
single_invoice_data.append(test.comment)
single_invoice_data.append(test.amount_total)
bulk_invoice_data.append(single_invoice_data)
row_count = 1
for single_invoice in bulk_invoice_data:
sheet.write(row_count, 0, row_count)
sheet.write(row_count, 1, single_invoice[0])
sheet.write(row_count, 2, single_invoice[1])
sheet.write(row_count, 3, single_invoice[2])
sheet.write(row_count, 4, single_invoice[3])
sheet.write(row_count, 5, single_invoice[4])
sheet.write(row_count, 6, single_invoice[5])
sheet.write(row_count, 7, single_invoice[6])
'''sheet.write(row_count, 8, single_invoice[7])
sheet.write(row_count, 9, single_invoice[8])
sheet.write(row_count, 10, single_invoice[9])'''
row_count += 1
return self.generate_xlwt_report_file('Invoice Report.xls', fl, wbk, sheet)
我想这是我出错的部分
def generate_xlwt_report_file(self, report_name, fl, wbk, sheet):
'''This method generates the report file.'''
wbk.save(fl)
fl.seek(0)
buf = base64.encodestring(fl.read())
ctx = dict(self._context)
ctx.update({'file': buf, 'report_name': report_name})
try:
form_id = self.env['ir.model.data'].get_object_reference('account', 'wiz_excel_file_download_form_view')[1]
except ValueError:
form_id = False
return {
'type': 'ir.actions.act_window',
'view_type': 'form',
'view_mode': 'form',
'res_model': 'wizard.excel.report.file',
'views': [(form_id, 'form')],
'view_id': form_id,
'target': 'new',
'context': ctx,
}
错误追溯:
Traceback (most recent call last):
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/http.py", line 537, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/http.py", line 574, in dispatch
result = self._call_function(**self.params)
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/http.py", line 310, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/service/model.py", line 118, in wrapper
return f(dbname, *args, **kwargs)
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/http.py", line 307, in checked_call
return self.endpoint(*a, **kw)
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/http.py", line 803, in __call__
return self.method(*args, **kw)
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/http.py", line 403, in response_wrap
response = f(*args, **kw)
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/addons/web/controllers/main.py", line 944, in call_kw
return self._call_kw(model, method, args, kwargs)
File "/home/administrator/odoo_ext/apps/odoo/lib/odoo-8.0.post20151126-py2.7.egg/openerp/addons/web/controllers/main.py", line 936, in _call_kw
return getattr(request.registry.get(model), method)(request.cr, request.uid, *args, **kwargs)
AttributeError: 'NoneType' object has no attribute 'fields_view_get'
不幸的是没有行号的痕迹。
任何帮助纠正我出错的地方都会非常有帮助。
答案 0 :(得分:0)