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
.
python 操作excel 使用笔记的更多相关文章
- python学习笔记(八)python操作Excel
一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...
- python学习笔记(十八)python操作excel
python操作excel需要安装通过pip安装xlwt, xlrd这两个模块: pip install xlwt pip insall xlrd 操作excel ,写入excel: import x ...
- python操作excel表格(xlrd/xlwt)
最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...
- Python操作Excel
一.系统性学习 对于操作Excel,需要Xlrd/xlwt这两个模块,下面推荐出系统性学习的网址: python操作Excel读写--使用xlrd 官方文档 Python 使用 Xlrd/xlwt 操 ...
- Python操作excel(xlrd和xlwt)
Python操作excel表格有很多支持的库,例如:xlrd.xlwt.openpyxl.win32com,下面介绍使用xlrd.xlwt和xlutils模块这三个库不需要其他的支持,在任何操作系统上 ...
- Python操作excel表格
用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件 注:本篇代码在Python3环境下运行 首先导入两个模块xlrd和xlwt,xlrd用来读取Exc ...
- Python 利用Python操作excel表格之openyxl介绍Part2
利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436) ## 绘图 c = LineChart() ...
- Python 利用Python操作excel表格之openyxl介绍Part1
利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436),免费获取以下性能监控工具(类似Nmon精简版) ...
- 【转】python操作excel表格(xlrd/xlwt)
[转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...
随机推荐
- 标准C实现基于TCP/IP协议的文件传输
上学期集成程序设计的课堂作业,对于理解TCP/IP实现还是挺有帮助的. TCP/IP编程实现远程文件传输在LUNIX中一般都采用套接字(socket)系统调用. 采用客户/服务器模式,其程序编写步骤如 ...
- 《Effective Java》笔记 使类和成员的可访问性最小化
类和接口 第13条 使类和成员的可访问性最小化 1.设计良好的模块会隐藏所有的实现细节,把它的API与实现清晰的隔离开来,模块之间只通过它们的API进行通信,一个模块不需要知道其他模块的内部工作情况: ...
- WSGI服务器实践二--实践一个基本功能的WSGI服务器
由于各种PYTHON框架都实现了WSGI接口,所以,通用性很广的. 在调试过程过,有一个字母拼错,搞了一个小时. 看来PYTHON自带的编辑器没有高亮,不爽. 在有提示的编辑器里一看就看了来啦..:) ...
- HTML <img> 标签的 height 和 width 属性
定义和用法 <img> 标签的 height 和 width 属性设置图像的尺寸. 提示:为图像指定 height 和 width 属性是一个好习惯.如果设置了这些属性,就可以在页面加载时 ...
- Codeforces Round #313 (Div. 2) D. Equivalent Strings
D. Equivalent Strings Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/559/ ...
- lr_save_var字符串截取总结
函数作用: 将一个变化长度的字符串保存到parameter中. 用法实例: 此处讲解函数: Action() { web_save_timestamp_param("tStamp&q ...
- 学习C# XmlSerializer 序列化反序列化XML
类.变量常用头: [XmlRootAttribute]:对根节点的描述,在类声明中使用 如:下例的Html类 [XmlType]:对节点描述,在类声明中使用 如:下例的Head类 [X ...
- android Intent.ACTION_SEND
ACTION_SEND intent 可以把自己的应用添加到系统的发送(分享)列表中. <intent-filter> <action android:name="andr ...
- ural 1272. Non-Yekaterinburg Subway
1272. Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to co ...
- DevExpress DXperience 的ASPxFilterControl 不显示 Like 菜单的方法
当使用Linq 作为数据源时,如果使用 ASPxFilterControl 的 Like 菜单筛选数据,就会出现以下错误 LINQ to Entities does not recognize the ...