写入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

.

python 操作excel 使用笔记的更多相关文章

  1. python学习笔记(八)python操作Excel

    一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...

  2. python学习笔记(十八)python操作excel

    python操作excel需要安装通过pip安装xlwt, xlrd这两个模块: pip install xlwt pip insall xlrd 操作excel ,写入excel: import x ...

  3. python操作excel表格(xlrd/xlwt)

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  4. Python操作Excel

    一.系统性学习 对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址: python操作Excel读写--使用xlrd 官方文档 Python 使用 Xlrd/xlwt 操 ...

  5. Python操作excel(xlrd和xlwt)

    Python操作excel表格有很多支持的库,例如:xlrd.xlwt.openpyxl.win32com,下面介绍使用xlrd.xlwt和xlutils模块这三个库不需要其他的支持,在任何操作系统上 ...

  6. Python操作excel表格

    用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件 注:本篇代码在Python3环境下运行 首先导入两个模块xlrd和xlwt,xlrd用来读取Exc ...

  7. Python 利用Python操作excel表格之openyxl介绍Part2

    利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436) ## 绘图 c = LineChart()    ...

  8. Python 利用Python操作excel表格之openyxl介绍Part1

    利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436),免费获取以下性能监控工具(类似Nmon精简版) ...

  9. 【转】python操作excel表格(xlrd/xlwt)

    [转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...

随机推荐

  1. [译]ASP.NET 性能监控

    概述:在性能测试中性能监视器是非常强大的工具,本文主要简述了几个关键的计数器和相关的阈值. 监控性能计数器 很多的性能计数器在性能调优中是非常重要的,由于性能计数器太多,很多人不知道选择那些计数器. ...

  2. [杂]SQL Server 之 Service Broker

    由于某些原因,我们的缓存依赖于数据库,而数据库反向通知需要依赖和使用ServiceBroker, 由于Deploy的人往往不是很清楚这个SB需要如何部署,特此记录. 判断数据库是否启用了Service ...

  3. Struts表单格局;theme三个属性值:simple,xhtml,css_xhtml

    转自:http://www.educity.cn/wenda/7156.html 解决Struts2 Form表单自己布局之前先看看 theme 属性, theme属性提供 三个属性值:simple, ...

  4. HDU 4612 Warm up tarjan缩环+求最长链

    Warm up Problem Description   N planets are connected by M bidirectional channels that allow instant ...

  5. HDU 4292 Food 最大流

    Food Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. kali实用链接

    1.Kali-linux安装之后的简单设置  :http://xiao106347.blog.163.com/blog/static/215992078201342410347137/ 2.kali文 ...

  7. sample

    sample是在数据源取样. val data = sc.parallelize(1 to 20) data.sample(true,0.4).collect sample第一个参数表示是否是重复抽样 ...

  8. android native开发时:java.lang.UnsatisfiedLinkError: Native method not found的处理

    这个异常一般是由于JNI的链接器不能正常识别C++的函数名造成的.处理的方法是用exern "C" {},来包裹需要export的C++的native方法. 如果native的方法 ...

  9. HTML-Canvas02

    文字对齐方式 : 水平对齐 //是用 textAlign 属性设置水平对齐方式(默认坐标点) ctx.textAlign = "start"; ctx.font = "3 ...

  10. POJ3177 Redundant Paths(边双连通分量+缩点)

    题目大概是给一个无向连通图,问最少加几条边,使图的任意两点都至少有两条边不重复路径. 如果一个图是边双连通图,即不存在割边,那么任何两个点都满足至少有两条边不重复路径,因为假设有重复边那这条边一定就是 ...