"Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-in Event, click to view the details of the event ."
Python to operate Excel
This blog will introduce you a way to operate Excel in Python. The core uses the xlrd and xlwt modules.
xlrd
for reading excel ;xlwt
Used to write excel .
Module installation is relatively simple
pip install xlrd,xlwt
复制代码
Before the official start, you also need to understand the three major objects in Excle
WorkBook
: Workbook objectSheet
: table objectCell
: cell object
Here WorlBook
represents the entire Excel file, Sheet
represents the Sheet in Excel, that Cell
is, each cell.
Cell
Data types in common cells ( )
- empty
- string(text)
- number
- date
- boolean
- error
- blank (blank form)
Open Excel file to read data
Prepare an Excel spreadsheet in advance for testing.
Just look at the code
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
print(data)
# 输出
# <xlrd.book.Book object at 0x00000272DE9276A0>
复制代码
Note that the latest version of the xlrd
module no longer supports xlsx
files. If you want to support this file format, please use the following version xlrd
.
pip uninstall xlrd # 卸载当前版本
pip install xlrd==1.2.0 # 安装1.2.0版本
复制代码
Get Sheet in Excel
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
table = data.sheets()[0] # 通过索引顺序获取
print(table)
table = data.sheet_by_index(1) # 通过索引顺序获取
sheet_name = '测试1'
table = data.sheet_by_name(sheet_name) # 通过 Sheet 名称获取
print(table)
复制代码
The above method returns an xlrd.sheet.Sheet()
object . If it does not exist Sheet
, or the name is wrong, the following BUG will appear
xlrd.biffh.XLRDError: No sheet named <'测试1'>
复制代码
Call the sheet_names()
method to return the names of all sheet pages in the workbook.
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
sheet_names = data.sheet_names()
print(sheet_names)
复制代码
Use this method to get all of them first sheet_name
, which can prevent illegal names from being called.
row operation
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
table = data.sheet_by_index(0)
nrows = table.nrows # 获取该sheet中的有效行数
print(nrows)
复制代码
Get the first line of content
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
sheet = data.sheet_by_index(0)
nrows = sheet.nrows # 获取该sheet中的有效行数
row_data = sheet.row_values(0) # 获取第1行的内容
print(row_data)
复制代码
Get all row data
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
sheet = data.sheet_by_index(0)
nrows = sheet.nrows # 获取该sheet中的有效行数
for i in range(nrows):
print(sheet.row_values(i)) # 获取第几行的数据
复制代码
Returns a list of all cell objects in the column
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
sheet = data.sheet_by_index(0)
print(sheet.row_slice(0))
# 输出
# [text:'姓名', text:'班级', text:'年龄']
复制代码
The rest of the content can be viewed by referring to the code
# 返回由该行中所有单元格的数据类型组成的列表
sheet .row_types(行索引, start_colx=0, end_colx=None)
# 返回由该行中所有单元格的数据组成的列表
sheet .row_values(行索引, start_colx=0, end_colx=None)
# 返回该列的有效单元格长度
sheet .row_len(行索引)
复制代码
column operations
Get the number of valid columns
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
sheet = data.sheet_by_index(0)
ncols = sheet.ncols #获取列表的有效列数
print(ncols)
复制代码
cell manipulation
Get cell content cell(rowx, colx)
: Get the cell object of rowx row and colx column in the sheet object, and the format of the returned value is 单元类型:单元值
.
import xlrd
data = xlrd.open_workbook('测试表格.xlsx') # filename 表示文件名以及路径
sheet = data.sheet_by_index(0)
data = sheet.cell(2, 1)
print(data)
复制代码
get cell value
data = sheet.cell_value(2, 1)
print(data)
复制代码
The xlwt module (manipulate the .xls
file for write operation) can be viewed directly in the comments.
import xlwt
xl = xlwt.Workbook(encoding='utf-8')
# 创建1个sheet对象,第2个参数是指单元格是否允许重设置,默认为False
sheet = xl.add_sheet('测试', cell_overwrite_ok=True)
# 第1个参数代表行,第2个参数是列,第3个参数是内容,第4个参数是格式
sheet.write(0, 0, '姓名')
sheet.write(0, 1, '年纪')
xl.save('橡皮擦的测试.xls')
复制代码
Basic usage of setting cell styles
import xlwt
xl = xlwt.Workbook(encoding='utf-8')
# 创建1个sheet对象,第2个参数是指单元格是否允许重设置,默认为False
sheet = xl.add_sheet('测试', cell_overwrite_ok=False)
# 初始化样式
style = xlwt.XFStyle()
# 为样式创建字体
font = xlwt.Font()
font.name = 'Arial Black'
style.font = font
# 第1个参数代表行,第2个参数是列,第3个参数是内容,第4个参数是格式
sheet.write(0, 0, '姓名')
sheet.write(0, 1, '年纪', style)
xl.save('橡皮擦的测试.xls')
复制代码
Merge Cells
# 合并 第1行到第2行 的 第1列到第2列
sheet.write_merge(1, 2, 1, 2, '合并单元格')
复制代码
record time
Flag of the Year 2022, 575 / 1024 articles written. You can follow me, like me, comment me, bookmark me.