>>> 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. 深度学习笔记(十二)车道线检测 LaneNet

    论文:Towards End-to-End Lane Detection: an Instance Segmentation Approach 代码:https://github.com/MaybeS ...

  2. 【HDOJ5447】Good Numbers(数论)

    题意: 思路:From https://blog.csdn.net/qq_36553623/article/details/76683438 大概就是把1e6里面的质因子能除的都除光之后借助两者gcd ...

  3. Vue.js——vue-resource详细介绍

    概述 Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的DOM选择器,就没有必要引入jQuery.vue-resource是Vue.js的一款插件,它可以通过 ...

  4. python学习之路(5)

    条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age=20 if age>=18: ...

  5. C++入门经典-例6.13-指针与二维数组

    1:代码如下: // 6.13.cpp : 定义控制台应用程序的入口点. // #include"stdafx.h" #include<iostream> using ...

  6. EBS 显示主页面的工作列表和主菜单

    EBS环境: R12.1.3 问题描述:如果系统的“个性化页”做了设置,可能出现登录系统后,如果下图红框中的 主菜单和工作列表没有显示的情况,如果需要重新显示“主菜单”和“工作列表”,可参考以下操作 ...

  7. windos 启动redis服务端与客户端

    服务端:1-win+R 打开命令行2-cd至redis目录,例如 G:\Redis63813-输入 redis-server.exe redis.windows.conf观察是否如图1:至此,已成功: ...

  8. pandas dataframe类型操作

    用python做数据分析pandas库介绍之DataFrame基本操作   怎样删除list中空字符? 最简单的方法:new_list = [ x for x in li if x != '' ] 这 ...

  9. vuex里面的store架构

    将store文件夹分为四个文件夹,分别是actions,getters,mutations,state. action:和mutatation功能是类似的,都是修改state里面的数据,区别是acti ...

  10. electron-Menu创建原生应用菜单和上下文菜单。

    当在MacOS.Windows.Linux中使用menu设置程序菜单时,会设置在各个程序窗体的顶层. Note: 如果没有在app中设置一个菜单,系统会自动生成一个默认菜单, 默认生成的菜单中包含了一 ...