Odoo中文社区可以通过以下三个域名访问:shine-it.net , odoocn.org,odoo.net.cn

原论坛用户的基本信息和发帖这里都予以保留,请注意:原论坛用户无需重新注册新用户,但是您的密码需要重置

开发人员可以登录gitter讨论组: http://gitter.im/odoo-china/Talk, 需要github账号

如果您登录系统碰到问题,请在微信公众号留言:

Odoo 读写excel



  • odoo经常要导入和导出数据,excel是比较常见的文档,读写excel,上代码

    <br /># -*- coding: utf-8 -*- <br /># author:ghoti<br />import xlrd<br />import xlwt<br />import base64<br />import cStringIO<br />class Read_Excel:<br />&nbsp; &nbsp; &#039;&#039;&#039;<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 根据索引获取Excel表格中的数据&nbsp;  参数:file_name:Excel文件路径;file_contents:二进制文件/字符串<br />&nbsp; &nbsp; &nbsp;  col_n:表头列名所在行数 ;del_n:表的后几行不读入; by_index:表的索引(默认第一个sheet)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 返回表格中某一行作为key的字典列表,key为unicode类型<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 校验:不能有重复的项,不能有空项<br />&nbsp; &nbsp; &#039;&#039;&#039;<br />&nbsp; &nbsp; def __init__(self,file_contents=None,file_name=None,col_n=1,del_n=0,by_index=0,):<br />&nbsp; &nbsp; &nbsp; &nbsp; self.file_name = file_name<br />&nbsp; &nbsp; &nbsp; &nbsp; self.file_contents = file_contents<br />&nbsp; &nbsp; &nbsp; &nbsp; self.col_n = col_n<br />&nbsp; &nbsp; &nbsp; &nbsp; self.del_n = del_n<br />&nbsp; &nbsp; &nbsp; &nbsp; self.by_index = by_index<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />&nbsp; &nbsp; def __call__(self):<br />&nbsp; &nbsp; &nbsp; &nbsp; try:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data = xlrd.open_workbook(filename=self.file_name,file_contents=self.file_contents)<br />&nbsp; &nbsp; &nbsp; &nbsp; except Exception,e:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print str(e)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return None<br />&nbsp; &nbsp; &nbsp; &nbsp; col_n = self.col_n<br />&nbsp; &nbsp; &nbsp; &nbsp; del_n = self.del_n<br />&nbsp; &nbsp; &nbsp; &nbsp; by_index = self.by_index<br />&nbsp; &nbsp; &nbsp; &nbsp; table = data.sheets()[by_index]<br />&nbsp; &nbsp; &nbsp; &nbsp; nrows = table.nrows #行数<br />&nbsp; &nbsp; &nbsp; &nbsp; #ncols = table.ncols #列数<br />&nbsp; &nbsp; &nbsp; &nbsp; colnames =&nbsp; table.row_values(col_n-1) #某一行数据做字典的key<br />&nbsp; &nbsp; &nbsp; &nbsp; #validate<br />&nbsp; &nbsp; &nbsp; &nbsp; if colnames:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; repeat_colnames = set([str(i) for i in colnames if colnames.count(i)!=1])<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i in colnames:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if not i:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise NameError(&#039;Null colname exist&#039;)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if repeat_colnames:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; alert_info = &#039;;&#039;.join(repeat_colnames)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; raise NameError(&#039;repeat colname exist : %s&#039;%alert_info)<br />&nbsp; &nbsp; &nbsp; &nbsp; rsp =&#91;]<br />&nbsp; &nbsp; &nbsp; &nbsp; for rownum in range(col_n,nrows-del_n):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row = table.row_values(rownum)<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if row:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app = {}<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for i in range(len(colnames)):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app[colnames[i].strip()] = row[i]<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rsp.append(app)<br />&nbsp; &nbsp; &nbsp; &nbsp; return rsp<br /><br />class Export_Excel:&nbsp;  <br />&nbsp; &nbsp; &#039;&#039;&#039;返回导出excel二进制数据,sheet_name为sheet名,headings为第一行,data为第二行后,2个列表的内容一一对应&#039;&#039;&#039;<br />&nbsp; &nbsp; def __init__(self,headings,data,sheet_name=&#039;export_xls&#039;,file_name=None):<br />&nbsp; &nbsp; &nbsp; &nbsp; self.sheet_name = sheet_name<br />&nbsp; &nbsp; &nbsp; &nbsp; self.headings = headings<br />&nbsp; &nbsp; &nbsp; &nbsp; self.data = data<br />&nbsp; &nbsp; &nbsp; &nbsp; self.file_name = file_name<br />&nbsp; &nbsp; <br />&nbsp; &nbsp; def __call__(self):<br />&nbsp; &nbsp; &nbsp; &nbsp; book = xlwt.Workbook()<br />&nbsp; &nbsp; &nbsp; &nbsp; sheet = book.add_sheet(self.sheet_name)<br />&nbsp; &nbsp; &nbsp; &nbsp; rowx = 0<br />&nbsp; &nbsp; &nbsp; &nbsp; for colx, value in enumerate(self.headings):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sheet.write(rowx, colx, value)<br />&nbsp; &nbsp; &nbsp; &nbsp; sheet.set_panes_frozen(True) # frozen headings instead of split panes<br />&nbsp; &nbsp; &nbsp; &nbsp; sheet.set_horz_split_pos(rowx+1) # in general, freeze after last heading row<br />&nbsp; &nbsp; &nbsp; &nbsp; sheet.set_remove_splits(True) # if user does unfreeze, don&#039;t leave a split there<br />&nbsp; &nbsp; &nbsp; &nbsp; for row in self.data:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowx += 1<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for colx, value in enumerate(row):<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sheet.write(rowx, colx, value.encode(&#039;utf-8&#039;).decode(&#039;utf-8&#039;))<br />&nbsp; &nbsp; &nbsp; &nbsp; buf = cStringIO.StringIO()<br />&nbsp; &nbsp; &nbsp; &nbsp; if self.file_name:<br />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; book.save(self.file_name)<br />&nbsp; &nbsp; &nbsp; &nbsp; book.save(buf)<br />&nbsp; &nbsp; &nbsp; &nbsp; out = base64.encodestring(buf.getvalue())<br />&nbsp; &nbsp; &nbsp; &nbsp; buf.close()<br />&nbsp; &nbsp; &nbsp; &nbsp; return out<br /><br />&#039;&#039;&#039;&nbsp;  <br />##test_for_use<br /><br />r = Read_Excel(file_contents=base64.decodestring(form&#91;&#039;import&#039;]),col_n=3,del_n=1)<br />trades = r()[:]<br />print trades[0][u&#039;收货人姓名&#039;]<br />print len(tables)<br />for row in tables:<br />&nbsp; &nbsp; print row<br />&nbsp; &nbsp; for i in row:<br />&nbsp; &nbsp; &nbsp; &nbsp; print i,row[i]<br />&nbsp; &nbsp; &nbsp; &nbsp; <br />headings = [u&#039;订单号&#039;,u&#039;下单日期&#039;]<br />values = [&#91;&#039;12345&#039;,&#039;2012-12-12&#039;],&#91;&#039;12346&#039;,&#039;2012-12-12&#039;]]<br />out = Export_Excel(headings,values)()<br />_result_fields&#91;&#039;data&#039;]&#91;&#039;default&#039;] = out<br />&#039;&#039;&#039;<br />&#039;&#039;&#039;<br />from read_excel import Read_Excel<br />print Read_Excel(file_name=&#039;a.xls&#039;)()<br />[{&#039;c_1&#039;:&#039;a_1&#039;,&#039;c_2&#039;:&#039;b_1&#039;},{&#039;c_1&#039;:&#039;a_2&#039;,&#039;c_2&#039;:&#039;b_2&#039;}]<br />&#039;&#039;&#039;<br />
    


  • 有用

    已转载,如有不妥请告知



  • jeff 大虾转载啊,记得提出bug



  • 这个不错,先留脚印



  • 留个脚印



  • 留个脚印,暂不知道怎么用,有没大神做成模块?