写入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. 在windows下用toolbox玩会docker

    哈哈哈.

  2. oracle sql developer数据表注释为乱码问题---解决

    参考网址:http://blog.163.com/jackie_howe/blog/static/19949134720121126879265/ 内容: 在windows中创建一个名为“NLS_LA ...

  3. wp8 --未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService...匹配的导出

    今天打算用VisualStudio2012做一个js效果页面测试的时候,打开VS2012新建项目,但是并没有像之前那样顺利的创建页面,而是弹出了一个错误窗口. 我的系统是win8专业版 64位 ,同时 ...

  4. IOS8 空项目全屏

    打开工程项目属性文件,点击Target下面的第一项,再选择General选项卡,向下找到Use Asset Catalog按钮.点击它. 弹出对话框,点击Migrate即可.这样,应用尺寸就能根据屏幕 ...

  5. CC2540开发板学习笔记(二)——按键

    一.实验内容 用按键S1控制LED1的亮和灭 二.实验过程 1.电路原理: 可以看出,当S按下P0.0和P0.1接地,反之则接高电压. 2.寄存器使用: 依旧应该首先进行3个基本寄存器的设定,对LED ...

  6. [转]关于int整形变量占有字节问题

    int的长度由处理器(16位,32位,64位)和比哪一期决定. 首先从处理器来讲 :16位处理器中的int 占有16位 即2个字节                         32位处理器中int ...

  7. Spring的qualifier标签

    @Autowired是根据类型进行自动装配的.如果当Spring上下文中存在不止一个UserDao类型的bean时,就会抛出BeanCreationException异常;如果Spring上下文中不存 ...

  8. http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html

    http://blog.sina.com.cn/s/blog_5f103c9c0101atny.html http://www.oschina.net/question/117304_51525

  9. 【POJ】3150 Cellular Automaton(矩阵乘法+特殊的技巧)

    http://poj.org/problem?id=3150 这题裸的矩阵很容易看出,假设d=1,n=5那么矩阵是这样的 1 1 0 0 1 1 1 1 0 0 0 1 1 1 0 0 0 1 1 1 ...

  10. python 操作execl文件

    http://www.jb51.net/article/60510.htm import xlrdimport xlwt # 打开文件   workbook = xlrd.open_workbook( ...