odoo import .xlsx sale_order,purchase_order

时间:2018-12-07 23:50:30

标签: python odoo xlsx odoo-10

我在这里尝试将.xlsx导入到sale_order 下面的代码说明了我是如何使用csv.DictReader()实现的。为了使它能在.xlsx文件上工作,我找到了XLS to Dict Reader using xlrd,但我不知道如何实现。

ps:我是python和odoo 10的新手

file_data = fields.Binary('Archive', required=True,)

def import_button(self):
    file_path = tempfile.gettempdir()+'/file.csv'
    data = self.file_data
    f = open(file_path,'wb')
    f.write(data.decode('base64'))
    f.close() 
    archive = csv.DictReader(open(file_path),delimiter=';')

    archive_lines = []
    for line in archive:
        archive_lines.append(line)
  

编辑:我确实使用了该功能XLSDictReader

> archive = XLSDictReader(open(file_path))
  

我收到此错误'import.purchase.order' object has no attribute 'fileno'

1 个答案:

答案 0 :(得分:2)

始终与xlrd一起,谢谢@Datanovice先生的帮助

def import_button(self):
    file_path = tempfile.gettempdir()+'/file.xls'
    data = self.file_data
    f = open(file_path,'wb')
    f.write(data.decode('base64'))
    f.close() 

    workbook = xlrd.open_workbook(file_path)
    workbook = xlrd.open_workbook(file_path, on_demand = True)
    worksheet = workbook.sheet_by_index(0)
    first_row = [] # The row where we stock the name of the column
    for col in range(worksheet.ncols):
        first_row.append( worksheet.cell_value(0,col) )
    # transform the workbook to a list of dictionaries
    archive_lines = []
    for row in range(1, worksheet.nrows):
        elm = {}
        for col in range(worksheet.ncols):
            elm[first_row[col]]=worksheet.cell_value(row,col)

        archive_lines.append(elm)