使用Python openpyxl模块读取excel文件

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

null
sudo pip3 install openpyxl 

输入文件:

图片[1]-使用Python openpyxl模块读取excel文件-yiteyi-C++库

代码#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)


输出:

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 NAMECOURSEBRANCHSEMESTER

代码#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 NAMEANKIT RAIRAHUL RAIPRIYA RAIAISHWARYAHARSHITA 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

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享