博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python 操作excel 使用笔记
阅读量:6911 次
发布时间:2019-06-27

本文共 3238 字,大约阅读时间需要 10 分钟。

写入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 styledef 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 xlrddef 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_sdef 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_sdef 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

.

 

转载于:https://www.cnblogs.com/weiok/p/5369741.html

你可能感兴趣的文章
SIP Servlet 开篇
查看>>
我在IT中的那些转变
查看>>
用户管理 之 Linux 用户管理工具介绍
查看>>
UIScrollView 滚动视图
查看>>
vc 客户端服务器程序
查看>>
liquibase
查看>>
配置RADIUS客户端
查看>>
Java闭锁_CountDownLatch
查看>>
openstack I版的搭建八--
查看>>
发一道JS题,不看答案你会吗?
查看>>
我的友情链接
查看>>
oracle数据出现愤怒加密算法
查看>>
Java基础学习总结(19)——Java环境变量配置
查看>>
BZOJ 2818GCD
查看>>
关于爱情
查看>>
nginx部分调优参数
查看>>
MySQL数据库基础(四)——MySQL数据库创建实例
查看>>
提交包到iTunes Connect时构建版本“正在处理”后直接消失的问题
查看>>
我的友情链接
查看>>
QQ空间技术架构之深刻揭密
查看>>