python 操作excel

python操作excel首先需要安装相关的库。xlrd是读取excel,xlwt是写入excel, xlutils是对excel进行修改。

安装方法如下:

1、打开cmd

2、分别运行 pip install xlrd,  pip install xlwt, pip install xlutils(注意:python目录已经加入到环境变量)

一、写入excel

一般操作即先新建一个excel表格,再新建一个sheet工作表,然后往里面写数据。注意,若excel已经被打开,那么会报错PermissionError: [Errno 13] Permission denied。

版本 
只能处理Excel97-2003或Excel 97之前版本的xls格式

存储数据过大 
存储数据过大时,会报错Exception: String longer than 32767 characters

可以使用其它模块

    • 1、创建excel  book=xlwt.Workbook()
    • 2、创建sheet  sheet = book.add_sheet(sheet_name)  
    • 3、创建单元格  sheet.write(行号,列号,value)
    • 4、保存excel  book.save('name.xls‘) #一定要用xls的

按照单元格操作:

import xlwt
book = xlwt.Workbook()#新建一个excel
sheet = book.add_sheet('sheet1')#新建一个sheet页
#往里面添加数据,一个个单元格插入
sheet.write(0,0,'编号')
sheet.write(0,1,'名字')
sheet.write(0,2,'性别')

sheet.write(1,0,'1')
sheet.write(1,1,'小明')
sheet.write(1,2,'')
book.save('myexcel.xls')#注意,使用wps的时候,后缀可以是xls或xlsx,但是使用ms office时,必须用xls

按照循环操作:

import xlwt
book = xlwt.Workbook()#新建一个excel
sheet = book.add_sheet('sheet1')#新建一个sheet页

#列表循环插入
stu_info  = [
    ['编号','姓名','密码','性别','地址'],
    [1,'小美','sdfsd23sdfsdf2','','北京'],
    [2,'小明','sdfsd23sdfsdf2','','北京'],
    [3,'小祁','sdfsd23sdfsdf2','','北京'],
    [4,'小凡','sdfsd23sdfsdf2','','北京'],
    [5,'小范','sdfsd23sdfsdf2','','北京'],
    [6,'小刘','sdfsd23sdfsdf2','','北京'],
    [7,'小坡','sdfsd23sdfsdf2','','北京'],
    [8,'小康','sdfsd23sdfsdf2','','北京'],
    [9,'小学','sdfsd23sdfsdf2','','北京'],
    [10,'小刘','sdfsdf2','','北京'],
    [11,'小看','sdfsd23sdfsdf2','','北京'],
]
#方法1,
row = 0 #定义一个参数,进行行循环
for stu in stu_info:
    sheet.write(row,0,stu[0])
    sheet.write(row,1,stu[1])
    sheet.write(row,2,stu[2])
    sheet.write(row,3,stu[3])
    sheet.write(row,4,stu[4])
    row+=1
#方法2
row = 0 #
for stu in stu_info: #控制行
    colu = 0 #
    for s in stu: #控制列
        sheet.write(row,colu,s)
        colu+=1
    row+=1
#方法3
for index,value in enumerate(stu_info):#enumerate同时取下标和对应的元素
    #  index 0
    # value ['编号','姓名','密码','性别','地址']
    #index 1
    #value [1,'machunbo','sdfsd23sdfsdf2','男','北京']
    for index2,v2 in enumerate(value): #enumerate同时取下标和对应的元素
        print(index,index2,v2)
        #0 1
        #1 machunbo
        #2 sdfsd23sdfsdf2
        #4 北京
        sheet.write(index,index2,v2)
book.save('myexcel.xls')#注意,使用wps的时候,后缀可以是xls或xlsx,但是使用ms office时,必须用xls

生成的excel显示如下:

二、读取excel

读取excel的顺序即:打开excel,选择要读取的sheet页。然后进行读取

  • 1、打开excel  book=xlrd.open_workbook(‘excel文件名称')
  • 2、打开sheet  
    • 根据索引值打开  sheet = book.sheet_by_index(sheet_index)  #索引值从0开始累计
    • 根据sheet名称打开  sheet = book.sheet_by_name('sheet_name')
  • 3、读取单元格  
    • 返回某个单元格的值    sheet.cell(行号,列号)  #行号,列号从0开始累计,返回如 text:'单元格的值'
    • 返回某个单元格的值  sheet.cell(行号,列号).value  #行号,列号从0开始累计,直接返回单元格的值
    • 返回某一行的数据  sheet.row_values( 行号)  #行号,行号从0开始累计,返回某一行的数据组成的列表
    • 返回sheet的行数  sheet.nrows  #返回sheet总共有多少行
    • 返回某一列的数据  sheet.col_values( 列号)   #列号,列号从0开始累计,返回某一列的数据组成的列表
    • 返回sheet的列数  sheet.ncols  #返回sheet总共有多少列
    • 返回表格的sheet数  book.nsheets  #返回excel总共有多少sheet
import xlrd
book = xlrd.open_workbook('myexcel.xls')#打开excel
sheet = book.sheet_by_index(0) #查看哪个sheet的数据,根据索引指定,第一个sheet,无返回值
sheet = book.sheet_by_name('sheet1')#查看哪个sheet的数据,根据sheet名称指定,无返回值
print(sheet.cell(0,0).value) #获取指定单元格的内容,单元格位置cell(行、列):id
print(sheet.cell(1,0).value) #输出1.0而不是整数1
print(sheet.row_values(0)) #获取整行的数据,返回list:['编号', '姓名', '密码', '性别', '地址']
print(sheet.row_values(1)) #输出[1.0, '小美', 'sdfsd23sdfsdf2', '男', '北京']
print(sheet.col_values(0))#获取整列的数据:['编号', 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]
print(sheet.col_values(1))#获取这一列的所有数据

print(sheet.nrows) #输出总行数:12
print(sheet.ncols) #输出总列数:5
for row in range(1,sheet.nrows): #从第一行开始取值,取到最后一行
    print(sheet.row_values(row)) #输出每行的数据,每行数据都是一个list

三、修改excel

修改excel的步骤:

1、先打开原来的excel

2、复制一份

3、在复制的excel上修改

4、保存修改

import xlrd
from xlutils import copy
book = xlrd.open_workbook('myexcel.xls')
new_book = copy.copy(book) #复制一份
sheet = new_book.get_sheet(0) #修改excel时,用get_sheet获取
sheet.write(0,0,'id')#修改的时候直接写对应的单元格
sheet.write(0,2,'password')
sheet.write(0,3,'sex')
new_book.save('myexcel.xls')

其他更多:

https://www.cnblogs.com/zhoujie/p/python18.html  合并单元格和日期的操作

http://www.cnblogs.com/snake-hand/p/3153158.html  其他样式等

原文地址:https://www.cnblogs.com/blueteer/p/10142508.html