📌  相关文章
📜  使用Python openpyxl 模块读取 excel 文件

📅  最后修改于: 2022-05-13 01:55:09.615000             🧑  作者: Mango

使用Python openpyxl 模块读取 excel 文件

Openpyxl是一个Python库,用于读取和写入 Excel(扩展名为 xlsx/xlsm/xltx/xltm)文件。 openpyxl 模块允许Python程序读取和修改 Excel 文件。
例如,用户可能必须浏览数千行并挑选出少量信息才能根据某些标准进行小的更改。使用 Openpyxl 模块,这些任务可以非常高效和轻松地完成。
使用此命令安装 openpyxl 模块:

sudo pip3 install openpyxl 


输入文件 :

代码#1:打印特定单元格值的程序

Python3
# Python program to read an excel file
 
# import openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# To open the workbook
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
# Get workbook active sheet object
# from the active attribute
sheet_obj = wb_obj.active
 
# Cell objects also have a row, column,
# and coordinate attributes that provide
# location information for the cell.
 
# Note: The first row or
# column integer is 1, not 0.
 
# Cell object is created by using
# sheet object's cell() method.
cell_obj = sheet_obj.cell(row = 1, column = 1)
 
# Print value of cell object
# using the value attribute
print(cell_obj.value)


Python3
# import openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# to open the workbook
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
 
# print the total number of rows
print(sheet_obj.max_row)


Python3
# importing openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
sheet_obj = wb_obj.active
 
# print total number of column
print(sheet_obj.max_column)


Python3
# importing openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
sheet_obj = wb_obj.active
max_col = sheet_obj.max_column
 
# Loop will print all columns name
for i in range(1, max_col + 1):
    cell_obj = sheet_obj.cell(row = 1, column = i)
    print(cell_obj.value)


Python3
# importing openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
sheet_obj = wb_obj.active
m_row = sheet_obj.max_row
 
# Loop will print all values
# of first column
for i in range(1, m_row + 1):
    cell_obj = sheet_obj.cell(row = i, column = 1)
    print(cell_obj.value)


Python3
# importing openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
sheet_obj = wb_obj.active
 
max_col = sheet_obj.max_column
 
# Will print a particular row value
for i in range(1, max_col + 1):
    cell_obj = sheet_obj.cell(row = 2, column = i)
    print(cell_obj.value, end = " ")


输出 :

STUDENT 'S NAME


代码#2:确定总行数

Python3

# import openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# to open the workbook
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
 
# print the total number of rows
print(sheet_obj.max_row)

输出 :

6


代码#3:确定总列数

Python3

# importing openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
sheet_obj = wb_obj.active
 
# print total number of column
print(sheet_obj.max_column)

输出 :

4


代码#4:打印所有列名

Python3

# importing openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
sheet_obj = wb_obj.active
max_col = sheet_obj.max_column
 
# Loop will print all columns name
for i in range(1, max_col + 1):
    cell_obj = sheet_obj.cell(row = 1, column = i)
    print(cell_obj.value)

输出 :

STUDENT 'S NAME
COURSE
BRANCH
SEMESTER


代码#5:打印第一列值

Python3

# importing openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
sheet_obj = wb_obj.active
m_row = sheet_obj.max_row
 
# Loop will print all values
# of first column
for i in range(1, m_row + 1):
    cell_obj = sheet_obj.cell(row = i, column = 1)
    print(cell_obj.value)

输出 :

STUDENT 'S NAME
ANKIT RAI
RAHUL RAI
PRIYA RAI
AISHWARYA
HARSHITA JAISWAL


代码 #6:打印特定的行值

Python3

# importing openpyxl module
import openpyxl
 
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
 
sheet_obj = wb_obj.active
 
max_col = sheet_obj.max_column
 
# Will print a particular row value
for i in range(1, max_col + 1):
    cell_obj = sheet_obj.cell(row = 2, column = i)
    print(cell_obj.value, end = " ")

输出 :

ANKIT RAI B.TECH CSE 4