python操作excel之xlrd

xlrd是专门用来在python中读取微软execel的模块,可以自己直接下载安装,也可以通过包管理器安装。组件的官网地址:http://www.python-excel.org/

基本操作:

#-*-encoding:utf-8-*-       #设置编码方式

import xlrd                    #导入xlrd模块

#打开指定文件路径的excel文件

xlsfile = r‘D:\AutoPlan\apisnew.xls‘

book = xlrd.open_workbook(xlsfile)     #获得excel的book对象

#获取sheet对象,方法有2种:

sheet_name=book.sheet_names()[0]          #获得指定索引的sheet名字

print sheet_name

sheet1=book.sheet_by_name(sheet_name)  #通过sheet名字来获取,当然如果你知道sheet名字了可以直接指定

sheet0=book.sheet_by_index(0)     #通过sheet索引获得sheet对象

#获取行数和列数:

nrows = sheet.nrows    #行总数

ncols = sheet.ncols   #列总数

#获得指定行、列的值,返回对象为一个值列表

row_data = sheet.row_values(0)   #获得第1行的数据列表

col_data = sheet.col_values(0)  #获得第一列的数据列表,然后就可以迭代里面的数据了

#通过cell的位置坐标获得指定cell的值

cell_value1 = sheet.cell_value(0,1)  ##只有cell的值内容,如http://xxx.xxx.xxx.xxx:8850/2/photos/square/

print cell_value1

cell_value2 = sheet.cell(0,1) ##除了cell值内容外还有附加属性,如:text:u‘http://xxx.xxx.xxx.xxx:8850/2/photos/square/‘

  1. print cell_value2

是不是很方便啊,恩,比用vbs调用的excel COM对象简便多了。而且这个支持linux平台。

========xls的写方法使用xlwt块==========

#encoding:utf-8       #设置编码方式

import xlwt

wbk = xlwt.Workbook(encoding=‘utf-8‘, style_compression=0)

heet = wbk.add_sheet(‘sheet 1‘, cell_overwrite_ok=True)  ##第二参数用于确认同一个cell单元是否可以重设值。

sheet.write(0,0,‘some text‘)

sheet.write(0,0,‘this should overwrite‘)   ##重新设置,需cell_overwrite_ok=True

style = xlwt.XFStyle()

font = xlwt.Font()

font.name = ‘Times New Roman‘

font.bold = True

style.font = font

sheet.write(0, 1, ‘some bold Times text‘, style)

wbk.save(‘D:\TestData2.xls‘)    ##该文件名必须存在

时间: 2024-08-26 18:24:48

python操作excel之xlrd的相关文章

【转】python操作excel表格(xlrd/xlwt)

[转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究"(用此一词让我觉得颇有成就感)之后,基本解决了日常所需.主要记录使用过程的常见问题及解决. python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库.可从这里下载https://pyp

python基础(六)python操作excel

一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的.这几个模块使用pip安装即可,下面是这几个模块的使用. 二.xlrd模块,xlrd模块用来读excel,具体用法如下: import xlrd #打开excel wb=xlrd.open_workbook('abc.xlsx')#打开的这个excel必须存在,否则会报错 print(wb.shee

八、python操作excel及网络编程和异常处理

一.python操作excel 1.读excel,xlrd模块用来读excel # book = xlrd.open_workbook(r'students.xlsx')#打开excel# print(book.sheet_names())#获取所有sheet的名字# sheet = book.sheet_by_index(0)#根据sheet页的位置去取sheet# sheet2 = book.sheet_by_name('Sheet2')#根据sheet页的名字获取sheet页# print

python 操作 excel

python操作execel主要是读写 读 通过 http://pypi.python.org/pypi/xlrd 写 通过 http://pypi.python.org/pypi/xlwd 下载tar包,解压出来,安装即可, 如果没有权限,将xlrd/xlrd拷贝到当前目录下,也可以使用. 如下是xlrd的使用例子 1 # -*- coding: utf-8 -*- 2 import xdrlib ,sys 3 import xlrd 4 import sys 5 def open_excel

python 读 excel 模块: xlrd

主要来自:[ python中使用xlrd.xlwt操作excel表格详解 ] 为了方便阅读, 我将原文两个模块拆分为两篇博文: [ python 读 excel 模块: xlrd ] [ python 写 excel 模块: xlwt ] xlrd 基本操作 这个过程有几个比较麻烦的问题,比如读取日期.读合并单元格内容. 读一个有2个 sheet 的 excel 文件, 读入sheet2的内容, 内容如下: 使用 python 读入 #!/usr/bin/env python # -*- cod

Python操作excel工具

python操作excel的工具类有很多,下面举几个常见的工具类: 一. 1.xlrd 只能读取excel操作,支持xls和xlsx两种格式的 2.xlwt 只能写入excel操作,只支持 xls格式的 3.xlsxwtiter 只能写入excel操作,同时支持xls格式和xlsx的 附python操作excel的常见参考链接: https://xlsxwriter.readthedocs.io/format.html https://blog.csdn.net/qq_24495287/arti

自动化办公:python操作Excel

1.安装 -- upgrade pippython -m pip install --- install pypipip install pypi 执行python setup.py install进行安装xlrd Python操作Excel,主要用到xlrd和xlwt这两个库,即xlrd是读Excel,xlwt是写Excel的库 pip install xlrdpip install xlwtpip install xlutilspip install xlsxwriter 2.操作一个简单的

python 操作excel表格

Python 操作excel 表格 #coding=utf-8 import xlsxwriter #1.创建excel 对象 work = xlsxwriter.Workbook('hello.xlsk')#在当前目录下创建一个Excel文件 #2. 创建表格 worksheet = work.add_worksheet('int') #3.写入内容 title_index = ["A","B","C","D","

Python操作Excel之数据提取

最近发现excel数据量极大,并且通过简单的数据操作不能提取到我需要的数据,如果单独操作,数据量太大耗时太长.想着通过简单的方式,并且快速提取数据,就想到了Python.python操作Excel使用的openyxl和pandas对Excel进行操作.代码如下: #!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2020-02-24 下午 03:43 # @Author : Zhanxing # @Site : # @File : 提