>>> 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文件的更多相关文章

  1. MFC vs2012 Office2013 读写excel文件

    近期在忙一个小项目(和同学一起搞的),在这里客户要求不但读写txt,而且可以读写excel文件,这里本以为很简单,结果...废话少说,过程如下: 笔者环境:win7 64+VS2012+Office2 ...

  2. 用Python读写Excel文件(转)

    原文:google.com/ncr 虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TA ...

  3. C# 使用 NPOI 库读写 Excel 文件

    NPOI 是开源的 POI 项目的.NET版,可以用来读写Excel,Word,PPT文件.在处理Excel文件上,NPOI 可以同时兼容 xls 和 xlsx.官网提供了一份 Examples,给出 ...

  4. [转]用Python读写Excel文件

    [转]用Python读写Excel文件   转自:http://www.gocalf.com/blog/python-read-write-excel.html#xlrd-xlwt 虽然天天跟数据打交 ...

  5. python使用xlrd模块读写Excel文件的方法

    本文实例讲述了python使用xlrd模块读写Excel文件的方法.分享给大家供大家参考.具体如下: 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi ...

  6. python读写Excel文件的函数--使用xlrd/xlwt

    python中读取Excel的模块或者说工具有很多,如以下几种: Packages 文档下载 说明 openpyxl Download | Documentation | Bitbucket  The ...

  7. C++读写EXCEL文件OLE,java读写excel文件POI 对比

    C++读写EXCEL文件方式比较 有些朋友问代码的问题,将OLE读写的代码分享在这个地方,大家请自己看.http://www.cnblogs.com/destim/p/5476915.html C++ ...

  8. Python使用openpyxl读写excel文件

    Python使用openpyxl读写excel文件 这是一个第三方库,可以处理xlsx格式的Excel文件.pip install openpyxl安装.如果使用Aanconda,应该自带了. 读取E ...

  9. 使用phpexcel类读写excel文件

    使用原生php读写excel文件的博文地址: 基于使用原生php读写excel文件的不靠谱,本文将简单介绍如何使用第三方类库phpexcel来读写excel文件. 首先,需要到githut下载phpe ...

  10. 用Python读写Excel文件的方式比较

    虽然天天跟数据打交道,也频繁地使用Excel进行一些简单的数据处理和展示,但长期以来总是小心地避免用Python直接读写Excel文件.通常我都是把数据保存为以TAB分割的文本文件(TSV),再在Ex ...

随机推荐

  1. zabbix微信发送消息脚本

    cat /usr/local/zabbix/share/zabbix/alertscripts/sed_messages_weixin.py python2.x #!/usr/bin/env pyth ...

  2. 本机向window服务器传送数据

    我们需要在远程服务器中安装 FileZilla_Server,在本机安装 FileZilla 客户端 FileZilla 服务器下载地址:https://pan.baidu.com/s/1o7Aen2 ...

  3. [BZOJ2286][Sdoi2011]消耗战(虚树上DP)

    2286: [Sdoi2011]消耗战 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6457  Solved: 2533[Submit][Statu ...

  4. Zookeeper(四))持久化日志文件

    Zookeeper(四))持久化日志文件 持久化用途 存储两种文件 snapshot:内存快照 log:事务日志,类似MySQL的binlog,存储数据节点的操作日志 问题 序列化的本质其实就是将原数 ...

  5. 利用 IntelliJ IDEA 进行代码对比的方法

    Sometimes,我们会有这样的需求,即:想对比出两个不同版本代码的区别.如何实现? 第 1 种:如果我们是从 SVN 检出的项目,并且想比较本地代码与从 SVN 检出时的代码相比都有那些区别,可以 ...

  6. jenkins部署java项目

    #########################################jenkins部署#################################3 一.jenkins是什么? J ...

  7. 五一 DAY 6

    五一  DAY 6 TypeName   类型名 VariableName  变量名 Part 1 数据结构 函数库:# include < utility > Pair 定义一个变量,它 ...

  8. Interface default method介绍

    一.introduce interface default method Introduce default methodWrite the default method at interfaceTh ...

  9. 报错:Original error: Could not proxy command to remote server. Original error: Error: read ECONNRESET

    问题:Appium的android真机启动手机时,会遇到以下问题: An unknown server-side error occurred while processing the command ...

  10. vim技巧2

    vim技巧总结-查找 1.查找命令1.1 执行一次查找普通模式下,/会调用查找提示符,如果vim扫描到文档尾部仍没有找到目标,会提示"search hit BOTTOM, continuin ...