6-5 如何读写excel文件
>>> import xlrd,xlwt
一、读excel
1、打开一个excel(读模式)
>>> book = xlrd.open_workbook(r"C:\视频\python高效实践技巧笔记\6数据编码与处理相关话题\6-6.xls") #读模式打开一个excel,读一个excel要先实例化一个workbook
>>> book.sheets() #查看excel的各个表
[<xlrd.sheet.Sheet object at 0x02CD7270>, <xlrd.sheet.Sheet object at 0x02CD7530>, <xlrd.sheet.Sheet object at 0x02CD7590>]
2、打开一个sheet表
>>> book.sheet_by_index(0)
<xlrd.sheet.Sheet object at 0x02CD7270>
>>> sheet0 =book.sheet_by_index(0) #按索引打开一个表
>>> sheet0.nrows #表的行数
4
>>> sheet0.ncols #表的列数
4
>>> sheet0.name #表的名字
u'\u6210\u7ee9'
>>> print sheet0.name
成绩
3、获取表里的一个表格
>>> cell = sheet0.cell(0,0) #获取表里的一个单元格cell,按坐标选取 >>> cell #打印结果看出是text(文本)类型,内容为u'\u59d3\u540d'(成绩的unicode)
text:u'\u59d3\u540d'
>>> cell.ctype #查看单元格内容的类型,其结果是一个玫举类型,玫举定义在xlrd.XL_CELL_
1
>>> xlrd.XL_CELL_TEXT #文本内容的类型是1
1 >>> print cell.value
姓名 >>> cell = sheet0.cell(1,2) #取坐标为(1,2)的表格
>>> cell #单元格类型为数字,内容为99.0
number:99.0 >>> cell.ctype
2
>>> xlrd.XL_CELL_NUMBER
2
>>> cell.value
99.0
4、查看整行
>>> sheet0.row(1) #获取1行的内容,参数为行号,返回一个列表。每一个对象都是一个cell对象
[text:u'\u674e\u96f7', number:95.0, number:99.0, number:96.0]
>>> sheet0.row_values(1) #获取1行的内容(不带内容类型的签名,直接就是内容)返回也是一个列表
[u'\u674e\u96f7', 95.0, 99.0, 96.0] >>> sheet0.row_values(1,1) #获取1行内容,从第1项开始,带有切片功能
[95.0, 99.0, 96.0]
>>> help(sheet0.row_values)
Help on method row_values in module xlrd.sheet: row_values(self, rowx, start_colx=0, end_colx=None) method of xlrd.sheet.Sheet instance
Returns a slice of the values of the cells in the given row.
help(sheet0.row_values)
5、查看整列,与行row操作类似
>>> sheet0.col(1)
[text:u'\u8bed\u6587', number:95.0, number:98.0, number:94.0]
>>> sheet0.col_values(1)
[u'\u8bed\u6587', 95.0, 98.0, 94.0]
>>> sheet0.col_values(1,1)
[95.0, 98.0, 94.0]
6、添加一个单元格
>>> help(sheet0.put_cell)
Help on method put_cell_unragged in module xlrd.sheet: put_cell_unragged(self, rowx, colx, ctype, value, xf_index) method of xlrd.sheet.Sheet instance
help(sheet0.put_cell)
参数分别为,行号、列号、内容类型,值,xf_index为字体、对齐等格式(可以填None)
二、写excel
1、#写一个excel要先实例化一个workbook
>>> wbook = xlwt.Workbook() #注意与xlrd的不同
2、添加一个sheet,参数为sheet名字
>>> wsheet1 = wbook.add_sheet('sheet1')
3、写一个单元格
>>> help(wsheet1.write)
Help on method write in module xlwt.Worksheet: write(self, r, c, label='', style=<xlwt.Style.XFStyle object>) method of xlwt.Worksheet.Worksheet instance
This method is used to write a cell to a :class:`Worksheet`.
help(wsheet1.write)
4、保存excel到文件中
>>> help(wbook.save)
Help on method save in module xlwt.Workbook: save(self, filename_or_stream) method of xlwt.Workbook.Workbook instance
This method is used to save the Workbook to a file in native Excel
format. :param filename_or_stream:
This can be a string containing a filename of
the file, in which case the excel file is saved to disk using the name
provided. It can also be a stream object with a write method, such as
a :class:`~io.StringIO`, in which case the data for the excel
file is written to the stream.
help(wbook.save)
将6-6.xls最右列增加一列总分,注意,xlrd可以增加一列,但是没有保存的功能。脚本文件代码为:
# -*- coding: cp936 -*- import xlrd,xlwt rbook = xlrd.open_workbook(r'C:\视频\python高效实践技巧笔记\6数据编码与处理相关话题\6-6.xls')
rsheet = rbook.sheet_by_index(0) #打开sheet nc = rsheet.ncols #获取列数
nr = rsheet.nrows #获取行数 rsheet.put_cell(0,nc,xlrd.XL_CELL_TEXT,u'总分',None) #放置一个单元格,内容格式是文本,内容是总分
for row in xrange(1,nr): #迭代表的每一行(除标题栏)
t = sum(rsheet.row_values(row,1)) #获取行的值并求和(除标题列)
rsheet.put_cell(row,nc,xlrd.XL_CELL_NUMBER,t,None)#在每一行放置一个单元格,内容是计算的求和的值 wbook = xlwt.Workbook() #创建一个excel
wsheet = wbook.add_sheet(rsheet.name) #添加一个sheet名字是读sheet的名字
style = xlwt.easyxf('align: vertical center,horizontal center')#定义单元格的格式,为write准备,和put_cell不同put_cell可以传None,write不行 for row in xrange(rsheet.nrows):
for col in xrange(rsheet.ncols): #迭代读sheet的每一行每一列,对每一个单元格进行迭代,因为写时只能对单元格逐一写,不能整行写
cell = rsheet.cell_value(row,col)#获取读sheet的单元格的内容
wsheet.write(row,col,cell,style) #写入写sheet单元格 wbook.save(r'C:\视频\python高效实践技巧笔记\6数据编码与处理相关话题\6-6output.xlsx') #将excel保存为
输出格式为:
6-5 如何读写excel文件的更多相关文章
- MFC vs2012 Office2013 读写excel文件
近期在忙一个小项目(和同学一起搞的),在这里客户要求不但读写txt,而且可以读写excel文件,这里本以为很简单,结果...废话少说,过程如下: 笔者环境:win7 64+VS2012+Office2 ...
- 用Python读写Excel文件(转)
原文:google.com/ncr 虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TA ...
- C# 使用 NPOI 库读写 Excel 文件
NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...
- [转]用Python读写Excel文件
[转]用Python读写Excel文件 转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...
- python使用xlrd模块读写Excel文件的方法
本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...
- python读写Excel文件的函数--使用xlrd/xlwt
python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket The ...
- C++读写EXCEL文件OLE,java读写excel文件POI 对比
C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...
- Python使用openpyxl读写excel文件
Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...
- 使用phpexcel类读写excel文件
使用原生php读写excel文件的博文地址: 基于使用原生php读写excel文件的不靠谱,本文将简单介绍如何使用第三方类库phpexcel来读写excel文件. 首先,需要到githut下载phpe ...
- 用Python读写Excel文件的方式比较
虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Ex ...
随机推荐
- windows下 java使用zookeeper案例
依赖: <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeep ...
- 动态数组C语言实现
/* * DynamicArray.h * * Created on: 2019年7月22日 * Author: Jarvis */ #ifndef SRC_DYNAMICARRAY_H_ #defi ...
- yue
1. 字节流与二进制文件 1.我的代码 public class Student { private int id; private String name; private int age; pri ...
- LeetCode 96. 不同的二叉搜索树(Unique Binary Search Trees )
题目描述 给定一个整数 n,求以 1 ... n 为节点组成的二叉搜索树有多少种? 示例: 输入: 输出: 解释: 给定 n = , 一共有 种不同结构的二叉搜索树: \ / / / \ \ / / ...
- 通过daemon.json配置docker 2375 API端口,配置私有http仓库
编辑daemon.json vi /etc/docker/daemon.json 配置如下内容即可 { "hosts":[ "tcp://0.0.0.0:2375&quo ...
- DB2基础维护手册
诊断DB2系统性能:db2top -d DEMODB db2top详解:http://blog.sina.com.cn/s/blog_636d62310102v7lm.html
- golang context学习记录1
1.前言 一个请求,可能涉及多个API调用,多个goroutine,如何在多个API 之间,以及多个goroutine之间协作和传递信息,就是一个问题. 比如一个网络请求Request,需要开启一些g ...
- LeetCode 409——最长回文串
1. 题目 2. 解答 我们先来看一看回文子串的规律,如果回文子串的长度为偶数,那么其中所有的每个元素都出现了偶数次:如果回文子串的长度为奇数,那么有一个元素出现了奇数次而其余每个元素都出现了偶数次. ...
- 关系代数(Relation Algebra)与SQL语句的对应关系
SQL语句的执行一般是先翻译为关系代数再被执行的(能有效提高执行速度),所以我们有必要 了解关系代数与SQL语句间的对应关系. 就像高中代数由+-*/和数字组成,关系代数是由union.interse ...
- 【flask】flask项目配置 app.config
[理论] 在很多情况下,你需要设置程序的某些行为,这时你就需要使用配置变量.在Flask中,配置变量就是一些大写形式的Python变量, 你也可以称之为配置参数或配置键.使用统一的配置变量可以避免在程 ...