Python 自动化操作 Excel - 01 - xlrd

简介: Python 自动化操作 Excel - 01 - xlrd

Python 自动化操作 Excel

Python 自动化操作 Excel - 01 - xlrd

1.安装xlrd

2.建立表格

3.使用xlrd

4.代码下载

Python 自动化操作 Excel - 01 - xlrd

1.安装xlrd

pip install xlrd



pip3 install xlrd


2.建立表格

image.png

3.使用xlrd

import xlrd

# 1 读取xlsx
book = xlrd.open_workbook("./xlrd_excel.xlsx")

# 读取sheet
# 获取第1个sheet 方法1
sheet = book.sheets()[0]
# 方法2
# sheet = people.sheet_by_index(0)
# 方法3
# sheet = people.sheet_by_name("Sheet1")

# 读取表格
# sheet.cell_value(row, col)
data1 = sheet.cell_value(1, 0)
data2 = sheet.cell_value(1, 1)
print(f"data1: {data1}")
print(f"data2: {data2}")

# 读取所有的工作表
sheets = book.sheets()
for sheet in sheets:
    # 行数
    nrows = sheet.nrows
    # 列数
    ncols = sheet.ncols
    for row in range(0, nrows):
        for col in range(0, ncols):
            print(sheet.cell_value(row, col))

# 读入日期
time_value1 = sheet.cell_value(1, 6)
time_value2 = sheet.cell_value(2, 6)
# 将读入的日期转换为元组的形式
# (2019, 5, 6, 12, 10, 32)
time_tuple = xlrd.xldate_as_tuple(time_value1, 0)

# 将日期数据转换为datetime对象
time_datetime = xlrd.xldate_as_datetime(time_value1, 0)
# 将datetime对象格式化转化为对应的字符串
# 2019-05-06
time_str = time_datetime.strftime("%Y-%m-%d %H:%M:%S")

4.代码下载

GitHub点个star 非常感谢!

代码下载: Python 自动化操作 Excel


相关文章
|
1天前
|
Python
Python 自动化操作 Excel - 02 - xlwt
Python 自动化操作 Excel - 02 - xlwt
23 14
|
1天前
|
Python
Python 自动化操作 Excel - 03 - xlutils
Python 自动化操作 Excel - 03 - xlutils
17 13
|
1天前
|
IDE 开发工具 数据安全/隐私保护
Python编程--实现用户注册信息写入excel文件
Python编程--实现用户注册信息写入excel文件
|
19小时前
|
自然语言处理 搜索推荐 程序员
【Python】如何使用pip,安装第三方库和生成二维码、操作Excel
【Python】如何使用pip,安装第三方库和生成二维码、操作Excel
10 0
|
1天前
|
IDE 开发工具 Python
Python自动化操作word--批量替换word文档中的文字
Python自动化操作word--批量替换word文档中的文字
|
2月前
|
关系型数据库 MySQL Shell
不通过navicat工具怎么把查询数据导出到excel表中
不通过navicat工具怎么把查询数据导出到excel表中
35 0
|
1月前
|
数据采集 存储 数据挖掘
使用Python读取Excel数据
本文介绍了如何使用Python的`pandas`库读取和操作Excel文件。首先,需要安装`pandas`和`openpyxl`库。接着,通过`read_excel`函数读取Excel数据,并展示了读取特定工作表、查看数据以及计算平均值等操作。此外,还介绍了选择特定列、筛选数据和数据清洗等常用操作。`pandas`是一个强大且易用的工具,适用于日常数据处理工作。
|
2月前
|
SQL JSON 关系型数据库
n种方式教你用python读写excel等数据文件
n种方式教你用python读写excel等数据文件
|
2月前
|
存储 Java Apache
|
2月前
|
数据可视化 Python
我是如何把python获取到的数据写入Excel的?
我是如何把python获取到的数据写入Excel的?
41 2