首页 > 编程语言 >python操作excel之openpyxl

python操作excel之openpyxl

时间:2023-02-22 22:32:45浏览次数:46  
标签:sheet openpyxl python excel list max print data row

1、安装

pip install openpyxl

2、打开工作簿

wb = openpyxl.load_workbook("./test.xlsx")

3、选择表单

sheet = wb["sheet1"]

4、获取指定单元格的数据

print(sheet.cell(1, 2).value)

python操作excel之openpyxl_数据

5、获取单元格最大行数和列数

print(sheet.max_row)
print(sheet.max_column)

python操作excel之openpyxl_数据_02

6、输出excel文件中的所有内容

# 方法一
m_row = sheet.max_row
m_column = sheet.max_column
for i in range(1, m_row + 1):
for j in range(1, m_column + 1):
print(sheet.cell(i, j).value, end="\t")
print()

# 方法二
m_data_list = list(sheet.values)
for i in range(len(m_data_list)):
for j in range(len(m_data_list[i])):
print(m_data_list[i][j], end="\t")
print()

python操作excel之openpyxl_表单_03

注意:excel单元格下标是从1开始的

标签:sheet,openpyxl,python,excel,list,max,print,data,row
From: https://blog.51cto.com/u_15694134/6079461

相关文章