python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库。

xlrd和xlwt这两个库用之前需要安装:cmd -> pip install xlrd  /   cmd -> pip install xlwt

一、写Excel

1. 手动一条条的添加数据

import xlwt
book = xlwt.Workbook() #新建一个excel
sheet = book.add_sheet('sheet1') #添加一个sheet页 sheet.write(0,0,'编号') #(行,列,'输入的内容') 第一行/列是0
sheet.write(0,1,'名字')
sheet.write(0,2,'性别') sheet.write(1,0,'')
sheet.write(1,1,'马春波')
sheet.write(1,2,'男') book.save('stu.xls') #wps xls xlsx ,微软的office 需以 .xls结尾

  运行结果,生成一个Excel,显示内容如下:

  

2. 循环添加行、列

import xlwt
book = xlwt.Workbook() #新建一个excel
sheet = book.add_sheet('sheet1') #添加一个sheet页 stu_info = [
['编号','姓名','密码','性别','地址'],
[1,'machunbo','sdfsd23sdfsdf2','男','北京'],
[2,'machunbo2','sdfsd23sdfsdf2','男','北京'],
[3,'machunb3','sdfsd23sdfsdf2','男','北京'],
[4,'machunbo4','sdfsd23sdfsdf2','男','北京'],
[5,'machunbo5','sdfsd23sdfsdf2','男','北京'],
[6,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[7,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[8,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[9,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[10,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[11,'machunbo6','sdfsd23sdfsdf2','男','北京'],
]#6行5列 #方法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:
#stu
col = 0 # 列
# [1, 'machunbo', 'sdfsd23sdfsdf2', '男', '北京'],
for s in stu: #控制列
sheet.write(row,col,s) #0 3 男
col+=1
row+=1 #方法3:
for index,value in enumerate(stu_info):
# 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('stu001.xls') #wps xls xlsx ,微软的office xls

  运行结果,生成一个Excel,显示内容如下:

  

二、 修改Excel

使用前,安装xlutils模块

import xlrd
from xlutils import copy #编写思路如下:
#1、先打开原来的excel
#2、复制一份
#3、在复制的excel上修改
#4、保存 book = xlrd.open_workbook('stu.xls')
new_book = copy.copy(book) #复制一份原excel
sheet = new_book.get_sheet(0) #修改excel的时候,得用get_sheet()
sheet.write(0,0,'id') #把第一行第一列中的数据变为id
sheet.write(0,3,'password') #第一行第四列的数据变为password
new_book.save('stu.xls') #保存

  运行结果,修改了原Excel,显示内容如下:

三、读取Excel

import xlrd
book = xlrd.open_workbook('stu3.xls')
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:['id', '姓名', '密码', 'password', '地址']
print(sheet.row_values(1)) #[1.0, 'machunbo', 'sdfsd23sdfsdf2', '男', '北京']
print(sheet.col_values(0))#获取整列的数据:['id', 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

  输出结果如下:

id
1.0
['id', '姓名', '密码', 'password', '地址']
[1.0, 'machunbo', 'sdfsd23sdfsdf2', '男', '北京']
['id', 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]
['姓名', 'machunbo', 'machunbo2', 'machunb3', 'machunbo4', 'machunbo5', 'machunbo6', 'machunbo6', 'machunbo6', 'machunbo6', 'machunbo6', 'machunbo6']
12
5
[1.0, 'machunbo', 'sdfsd23sdfsdf2', '男', '北京']
[2.0, 'machunbo2', 'sdfsd23sdfsdf2', '男', '北京']
[3.0, 'machunb3', 'sdfsd23sdfsdf2', '男', '北京']
[4.0, 'machunbo4', 'sdfsd23sdfsdf2', '男', '北京']
[5.0, 'machunbo5', 'sdfsd23sdfsdf2', '男', '北京']
[6.0, 'machunbo6', 'sdfsd23sdfsdf2', '男', '北京']
[7.0, 'machunbo6', 'sdfsd23sdfsdf2', '男', '北京']
[8.0, 'machunbo6', 'sdfsd23sdfsdf2', '男', '北京']
[9.0, 'machunbo6', 'sdfsd23sdfsdf2', '男', '北京']
[10.0, 'machunbo6', 'sdfsd23sdfsdf2', '男', '北京']
[11.0, 'machunbo6', 'sdfsd23sdfsdf2', '男', '北京']

内容补充:https://www.cnblogs.com/zhoujie/p/python18.html

python操作Excel-写/改/读的更多相关文章

  1. python操作excel表格(xlrd/xlwt)

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  2. Python操作excel表格

    用Python操作Excel在工作中还是挺常用的,因为毕竟不懂Excel是一个用户庞大的数据管理软件 注:本篇代码在Python3环境下运行 首先导入两个模块xlrd和xlwt,xlrd用来读取Exc ...

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

    [转]python操作excel表格(xlrd/xlwt) 最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异, ...

  4. 使用python操作excel表格

    首先 pip install xlrd  安装相关模块 其次:使用方法: 1 导入模块 import xlrd 2 打开excel文件读取数据 worksheet=xlrd.open_workbook ...

  5. python基础(六)python操作excel

    一.python操作excel,python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的 ...

  6. python操作excel xlrd和xlwt的使用

    最近遇到一个情景,就是定期生成并发送服务器使用情况报表,按照不同维度统计,涉及python对excel的操作,上网搜罗了一番,大多大同小异,而且不太能满足需求,不过经过一番对源码的"研究&q ...

  7. python 操作excel 的包 函数

    ###########sample 1 https://blog.csdn.net/chengxuyuanyonghu/article/details/54951399 python操作excel主要 ...

  8. Python 操作excel day5

    一.Python操作excel python操作excel使用xlrd.xlwt和xlutils模块 1.xlrd模块是读取excel的: 2.xlwt模块是写excel的: 3.xlutils是用来 ...

  9. python - 操作excel表格

    说明:由于公司oa暂缺,人事妹子在做考勤的时候,需要通过几个excel表格去交叉比对员工是否有旷工或迟到,工作量大而且容易出错. 这时候it屌丝的机会来啦,花了一天时间给妹子撸了一个自动化脚本. 1. ...

  10. 自动化办公:python操作Excel

    1.安装 -- upgrade pippython -m pip install --- install pypipip install pypi 执行python setup.py install进 ...

随机推荐

  1. java 红黑树

    背景:总结面试中关于红黑树的相关题目 红黑树(一)之 原理和算法详细介绍 漫画:什么是红黑树? 红黑树是一种自平衡的二叉查找树 红黑树的5个特征:根.叶子都是黑的 节点非红即黑.不能是连续红的.节点到 ...

  2. Windows android SDK环境配置及判断安装成功

  3. struts2 防止表单的重复提交

    防止表单重复提交(拦截器) 1.回顾之前的解决办法: 2.Struts2中的解决办法: 2.1.使用重定向 <result type="redirect">/succe ...

  4. Eclipse搭建SpringBoot之HelloWorld

    你的eclipse需要先安装 Spring Tool Suite™ 第一种方法(不建议,之所以贴上是因为探索的过程) 首先新建Maven工程 勾选第一个按钮,第三个是选择working set ,你可 ...

  5. c#UDP协议

    UDP协议是不可靠的协议,传输速率快 服务器端: using System; using System.Collections.Generic; using System.Linq; using Sy ...

  6. Going Home POJ - 2195 (最小费用最大流)

    On a grid map there are n little men and n houses. In each unit time, every little man can move one ...

  7. pickle详解

    一.简介 在机器学习中,我们常常需要把训练好的模型存储起来,这样在进行决策时直接将模型读出,而不需要重新训练模型,这样就大大节约了时间 Python提供的pickle模块就很好地解决了这个问题,它可以 ...

  8. PubMed数据下载

    目标站点分析 目标:抓取页面中的机构名称,日期,标题,作者, 作者信息, 摘要 程序实现 # -*- coding: utf-8 -*- """ @Datetime: 2 ...

  9. Linux CGroup

    catalog . 引言 . Cgroup安装配置 . Cgroup使用方式 . CGroup的子系统 1. 引言 我们已经讨论了Linux下命名空间(Namespace)的基本知识,详情请参阅另一篇 ...

  10. Mark标记功能的实现(像手工标记的一样)

    今天看到一个网站的标记功能,觉得很不错,先记录一下,emm <!DOCTYPE html> <html lang="en"> <head> &l ...