跳转至内容
  • 版块
  • 标签
  • 热门
  • 用户
  • 群组
皮肤
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • 默认(Flatly)
  • 不使用皮肤
折叠

Odoo 中文社区

  1. 主页
  2. 版块
  3. Odoo 开发与实施交流
  4. Odoo 读写excel

Odoo 读写excel

已定时 已固定 已锁定 已移动 Odoo 开发与实施交流
7 帖子 4 发布者 7.4k 浏览
  • 从旧到新
  • 从新到旧
  • 最多赞同
登录后回复
此主题已被删除。只有拥有主题管理权限的用户可以查看。
  • G 离线
    G 离线
    ghotiv
    写于 最后由 编辑
    #1

    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 />
    
    1 条回复 最后回复
    0
    • wjfonhandW 离线
      wjfonhandW 离线
      wjfonhand
      写于 最后由 编辑
      #2

      有用

      已转载,如有不妥请告知

      GoodERP -- Odoo China fork

      1 条回复 最后回复
      0
      • G 离线
        G 离线
        ghotiv
        写于 最后由 编辑
        #3

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

        1 条回复 最后回复
        0
        • H 离线
          H 离线
          herod
          写于 最后由 编辑
          #4

          这个不错,先留脚印

          1 条回复 最后回复
          0
          • F 离线
            F 离线
            frankwsp163.com
            写于 最后由 编辑
            #5

            留个脚印

            1 条回复 最后回复
            0
            • H 离线
              H 离线
              herod
              写于 最后由 编辑
              #6

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

              1 条回复 最后回复
              0

              • 登录

              • 没有帐号? 注册

              • 登录或注册以进行搜索。
              • 第一个帖子
                最后一个帖子
              0
              • 版块
              • 标签
              • 热门
              • 用户
              • 群组