一、往Excel中写数据

  使用pip install xlwt安装xlwt模块,用xlwt模块进行对Excel进行写数据。

import xlwt

# book = xlwt.Workbook()
# sheet = book.add_sheet('students')
#
# sheet.write(0, 0, 'id')
# sheet.write(0, 1, 'name')
# sheet.write(0, 2, 'age')
#
# sheet.write(1, 0, '1')
# sheet.write(1, 1, 'xiaohei')
# sheet.write(1, 2, '18')
#
# book.save('students.xls') # 如果后缀是xlsx,用微软的office打不开 stus = [
[1,'ds','bejing',51],
[2,'fd','shanghai',28],
[3,'zc','shanghai',16],
[4,'lhy','shanghai',21],
[5,'ylm','shanghai',35],
[6,'wxl','beijing',16],
]
stus.insert(0, ['编号', '姓名', '地址', '年龄'])
book = xlwt.Workbook()
sheet = book.add_sheet('sheet1')
# 第一种写法
# row = 0
# for stu in stus:
# col = 0
# for s in stu:
# sheet.write(row, col, s)
# col += 1
# row += 1
# 第二种写法
for row,stu in enumerate(stus):
for col,s in enumerate(stu):
sheet.write(row, col, s) ages = [s[-1] for s in stus if type(s[-1]) != str]
avg_age = round(sum(ages) / len(ages), 2)
sheet.write(row+1, 0, '平均年龄')
sheet.write(row+1, 1, avg_age)
book.save('students.xls')

二、读取Excel中的数据

  使用pip install xlwt安装xlrd模块,用xlrd模块进行对Excel进行读取数据。

import xlrd

book = xlrd.open_workbook('students.xls')
# sheet = book.sheet_by_index(0)
sheet = book.sheet_by_name('students') print(book.sheets()) # 所有的sheet页,返回的是一个list,list里面是每个sheet对象 for s in book.sheets():
print(s.row_values(1)) print(sheet.cell(0, 0).value)
print(sheet.row_values(0))
print(sheet.row_values(1)) print(sheet.col_values(0))
print(sheet.col_values(1)) print(sheet.nrows) # 多少行
print(sheet.ncols) # 多少列

三、修改Excel

  使用pip install xlutils 安装xlutils 模块,用xlutils 中的copy进行复制Excel,用xlrd模块进行对Excel进行读取数据,用copy的数据进行修改。

# 修改Excel, 在copy的sheet进行修改
from xlutils import copy
import xlrd book = xlrd.open_workbook('students.xls')
sheet = book.sheet_by_index(0)
new_book = copy.copy(book)
copy_sheet = new_book.get_sheet(0) for row in range(1, sheet.nrows-1):
addr = sheet.cell(row, 2).value
addr = addr.replace('beijing', '北京').replace('shanghai', '上海')
copy_sheet.write(row, 2, addr) new_book.save('students.xls')

python 09篇 操作Excel的更多相关文章

  1. 转 Python - openpyxl 读写操作Excel

    Python - openpyxl 读写操作Excel   openpyxl特点   openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件,xls和xlsx之间 ...

  2. 09 python学习笔记-操作excel(九)

    python操作excel使用xlrd.xlwt和xlutils模块,xlrd模块是读取excel的,xlwt模块是写excel的,xlutils是用来修改excel的.这几个模块可以使用pip安装, ...

  3. python用openpyxl操作excel

    python操作excel方法 1)自身有Win32 COM操作office但讲不清楚,可能不支持夸平台,linux是否能用不清楚,其他有专业处理模块,如下 2)xlrd:(读excel)表,xlrd ...

  4. python通过openpyxl操作excel

    python 对Excel操作常用的主要有xlwt.xlrd.openpyxl ,前者xlwt主要适合于对后缀为xls比较进行写入,而openpyxl主要是针对于Excel 2007 以上版本进行操作 ...

  5. 用python库openpyxl操作excel,从源excel表中提取信息复制到目标excel表中

    现代生活中,我们很难不与excel表打交道,excel表有着易学易用的优点,只是当表中数据量很大,我们又需要从其他表册中复制粘贴一些数据(比如身份证号)的时候,我们会越来越倦怠,毕竟我们不是机器,没法 ...

  6. Python - openpyxl 读写操作Excel

    openpyxl特点   openpyxl(可读写excel表)专门处理Excel2007及以上版本产生的xlsx文件,xls和xlsx之间转换容易 注意:如果文字编码是“gb2312” 读取后就会显 ...

  7. python使用xlrd操作Excel文件

    一.xlrd读取Excel文件 用xlrd进行读取比较方便,流程和平常手动操作Excel一样,打开工作簿(Workbook),选择工作表(sheets),然后操作单元格(cell). 例子:要打开当前 ...

  8. python使用xlrd 操作Excel读写

    此文章非本人 一.安装xlrd模块 到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境. 二.使用介绍 1.导入模块 i ...

  9. Python使用openpyxl操作excel表格

    前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. PS:如有需要Python学习资料的小伙伴可以加点击下方链接自行获取 ht ...

随机推荐

  1. Cannot resolve class or package 'mysql’处理方法

    在application.properties中配置mybatis_plus碰见报了这个错误 Cannot resolve class or package 'jdbc' 错误产生的原因 其实这个问题 ...

  2. jupyter notebook快捷键使用的注意点

    来源:https://zhidao.baidu.com/question/1800695798976401387.html 本文做进一步的阐释: 1.使行出现,但是光标要点击到有line空白区域 直接 ...

  3. 先进一站式IP及定制

    先进一站式IP及定制 芯动科技15年来立足中国本土,目前已实现从130nm到5nm工艺高速混合电路IP核全覆盖,且所有IP均自主可控,一站式赋能国产芯片发展. 提供经过批量生产验证或硅验证的IP产品, ...

  4. AMD–7nm “Rome”芯片SOC体系结构,支持64核

    AMD–7nm "Rome"芯片SOC体系结构,支持64核 AMD Fully Discloses Zeppelin SOC Architecture Details at ISS ...

  5. 利用NVIDIA NGC的TensorRT容器优化和加速人工智能推理

    利用NVIDIA NGC的TensorRT容器优化和加速人工智能推理 Optimizing and Accelerating AI Inference with the TensorRT Contai ...

  6. jsp页面抽取

    步骤: 1.先将jsp中要抽取的公共部分剪切出来,黏贴到新的jsp文件中,取名叫xxx.jsp 2.在需要引入此公共部分的jsp页面中使用<%@include file="xxx.js ...

  7. UF_CSYS 坐标系操作

    Open C   UF_CSYS_ask_csys_info               获取WCS坐标系的原点坐标和矩阵标识UF_CSYS_ask_matrix_of_object    获得对象 ...

  8. centos 7 iotop 安装

    安装指令:yum -y install iotop 指定查看aubunt 用户的读写状态:iotop -u aubunt -P -k -t 允许在非交互模式下每隔3秒刷新一次,只刷新6次:iotop ...

  9. noip2015 总结

    神奇的幻方 题目描述 幻方是一种很神奇的N*N矩阵:它由数字1,2,3,--,N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个幻方:首先将1写在第一行的 ...

  10. 日志挖掘针对DML语句

    作用: 针对用户的误操作,比如更改数据错误,误删除表等,可以用日志挖掘的方式,跟踪哪个用户什么时候做的操作,并进行数据还原. 一.前期准备: 1.添加最小补充日志,能够记录到更详细的信息,为日志挖掘分 ...