Home >Backend Development >Python Tutorial >Introducing the basic usage of the openpyxl module in Python

Introducing the basic usage of the openpyxl module in Python

coldplay.xixi
coldplay.xixiforward
2021-02-02 18:02:473121browse

Introducing the basic usage of the openpyxl module in Python

Free learning recommendation: python video tutorial

In interface automation testing, for Test data is usually managed using Excel. Openpyxl can read and modify .xls files. Before using Openpyxl, you must first master three objects, namely: Workbook (an Excel file containing multiple Sheets), Worksheet (a Workbook has multiple Worksheets, and a table). Name recognition, such as "Sheet1", "Sheet2", etc.) and Cell (cell, which stores specific data objects).
Introducing the basic usage of the openpyxl module in Python
Common methods of Openpyxl module:
1. Open Excel:
wb =openpyxl.load_workbook("apicases.xlsx")
2. Position the form:
sheet = wb["login"]
3. Read the form data:
data=sheet.cell(3,7).value
4. Get the maximum row and column:
max_row = sheet. max_row
max_column= sheet.max_column
5. Write data:
sheet.cell(10,10, "This is a write test")
wb.save(file)
6. Traverse all the data in the table:
datas = []
for i in range(1, max_row 1):
for j in range(1, max_column 1):
data = sheet. cell(i,j).value
datas.append(data)
print(datas)

Openpyxl module operation Excel code encapsulation:
Introducing the basic usage of the openpyxl module in Python
Introducing the basic usage of the openpyxl module in Python
Running results:
Introducing the basic usage of the openpyxl module in Python

For a large number of free learning recommendations, please visit python tutorial(Video)

##This article comes from:

https://blog.csdn.net/qq_44848764/article/details/113501899?spm=1001.2014.3001.5501 , thank you Original author: Fantasy will bloom one day, share it!

The above is the detailed content of Introducing the basic usage of the openpyxl module in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete