zoukankan      html  css  js  c++  java
  • python 操作excel 使用笔记

    写入excel, 保存的过程中需要注意,保存格式xls后缀,如果用xlsx会报错

    def set_style(name,height,bold=False):
        """"""
        import xlwt
        style = xlwt.XFStyle() # 初始化样式
    
        font = xlwt.Font() # 为样式创建字体
        font.name = name # 'Times New Roman'
        font.bold = bold
        font.color_index = 4
        font.height = height
    
        # borders= xlwt.Borders()
        # borders.left= 6
        # borders.right= 6
        # borders.top= 6
        # borders.bottom= 6
    
        style.font = font
        # style.borders = borders
    
        return style
    
    
    def write_excel(rows=[{}]):
        """"""
        try:
            import xlwt
    
            book = xlwt.Workbook()
            # sheet1 = book.add_sheet('Sheet 1')
            # sheet1.write(0,0,'AAAAAAAAA1')
            # book.save("demo1.xls")  # 保存文件
            sheet1 = book.add_sheet(u'sheet1', cell_overwrite_ok=True)  # 创建sheet1
            row0 = [u"证书编号", u"执行结果"]
            for i, row in enumerate(row0):
                sheet1.write(0, i, str_unicode(row0[i]), set_style('Times New Roman', 220, True))
            for i, row in enumerate(rows):
                sheet1.write(i+1, 0, str_unicode(row[0]))
                sheet1.write(i+1, 1, str_unicode(row[2]))
            now = datetime.datetime.now()
            new_file_dir = '%s/%s' % ('temp_excel', now.strftime("%Y/%m/%d"))
            new_file_name = fileutil.reset_file_name('demo1.xls')   # 重命名文件
            xls_name = fileutil.get_absolute_file_path(new_file_name, new_file_dir)
            book.save(xls_name)  # 保存文件
            relative_xls_name = xls_name.split(settings.MEDIA_ROOT)[1]
            relative_xls_name = "/upload_media/%s" % relative_xls_name
            return relative_xls_name
        except Exception, e:
            log.error("card_write_excel:%s" % e)
            print e
            return ""

    读取excel

    from django.utils.encoding import smart_str, smart_unicode
    import xlrd
    def str_encode(s, encoding="utf-8"): """""" code_s = s try: code_s = smart_str(s, encoding=encoding) except Exception, e: code_s = smart_unicode(s, encoding=encoding) return code_s def str_unicode(s, encoding="utf-8"): """""" code_s = s try: code_s = smart_unicode(s, encoding=encoding) except Exception, e: code_s = smart_str(s, encoding=encoding) return code_s def read_excel(request): """""" xls_name = request.POST.get("excel", '') extract_name = request.POST.get("zip", "") # 读取远程文件 # response = urllib2.urlopen(file_url, timeout=0.2) # content = response.read() # book = xlrd.open_workbook(file_contents=content) # 读取本地文件 xls_name = fileutil.get_absolute_file_path(xls_name) book = xlrd.open_workbook(filename=xls_name) sheet = book.sheets()[0] rows = [] for i in xrange(1, sheet.nrows): row = Struct() row.line = i + 1 row.type_id = str_encode(sheet.cell(i, 0).value) row.card_no = str(str_encode(str(sheet.cell(i, 1).value))) row.card_id = str_encode(sheet.cell(i, 2).value) row.user_name = str_encode(sheet.cell(i, 3).value) row.spell_name = str_encode(sheet.cell(i, 4).value) row.level = str_encode(sheet.cell(i, 5).value) row.title = str_encode(sheet.cell(i, 6).value) row.ftitle = str_encode(sheet.cell(i, 7).value) row.ability = str_encode(sheet.cell(i, 8).value) rows.append(row) print rows

    .

  • 相关阅读:
    [转]C#进阶系列——WebApi 接口参数不再困惑:传参详解
    Netty中的三种Reactor(反应堆)
    I/O模型之三:两种高性能 I/O 设计模式 Reactor 和 Proactor
    【转】第8章 前摄器(Proactor):用于为异步事件多路分离和分派处理器的对象行为模式
    mysql 数据库 自动截取数据的问题---mysql的sql_model的四种模式:宽松模式、严格模式
    spring-session之四:Spring Session下的Redis存储结构
    Mysql auto_increment总结
    mysql实战优化之一:sql优化
    mysql字符集和校对规则(Mysql校对集)
    Oracle B-tree、位图、全文索引三大索引性能比较及优缺点汇总
  • 原文地址:https://www.cnblogs.com/weiok/p/5369741.html
Copyright © 2011-2022 走看看