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的操作,上网搜罗了一番,大多大同小异, ...
随机推荐
- 二、JavaScript语言--JS基础--JavaScript入门篇
1.如何插入JS 使用<script>标签在HTML网页中插入JavaScript代码.注意, <script>标签要成对出现,并把JavaScript代码写在<scri ...
- 解决TIME_WAIT过多造成的问题
sh-4.1# netstat -an |awk '/tcp/ {++S[$NF]}END {for (a in S) print a , S[a]}' TIME_WAIT CLOSE_WAIT ES ...
- 字典树(Trie树)的实现及应用
>>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...
- 对线程调度中Thread.sleep(0)的深入理解
在Java或者C#中,都会用到 Thread.Sleep()来使线程挂起一段时间.那么你有没有正确的理解这个方法的用法呢?思考下面这两个问题: 1.假设现在是 2014-8-13 17:00:00.0 ...
- Spring中的设计模式学习
Spring提供了一种Template的设计哲学,包含了很多优秀的软件工程思想. 1. 简单工厂 又叫做静态工厂方法(StaticFactory Method)模式,但不属于23种GOF设计模式之一. ...
- 使用awk排除第一行和第二行的数据
因为linux shell命令行输出的前面几行一般是指导或是格式字段说明, 而不是实现的数据,所以在作过滤时,一般需要排除前面的几行. 现需要找出指定机器开放的所有端口. 我遇到的情况是要排除前面两行 ...
- linux tricks 之数据对齐。
转载:http://blog.chinaunix.net/uid-20608849-id-3027953.html 内核为了保持最大的兼容性和代码灵活性,不可能直接对某个数据类型定义它的大小范围. ...
- poj 1273 最大流入门
明天再拍一遍 #include <iostream> #include <queue> using namespace std; ; const int INF = 0x7FF ...
- Android SQLite总结(一) (转)
Android SQLite总结(一) 郑海波 2012-08-21 转载请声明:http://blog.csdn.net/nuptboyzhb/article/details/7891887 前言 ...
- loj 1165(bfs+康托展开)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26879 思路:题目意思很简单,就是通过一些位置的交换,最后变成有序 ...