Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use openpyxl of python

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use python's openpyxl". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use python's openpyxl.

Basic use of openpyxl

Environment

Pycharm2020.1

Python3.8

Openpyxl 2.1.4

What I demonstrate here is openpyxl 2.1.4.

Import openpyxl Modul

Import openpyxl

Read operation

Open the file to be processed, and this function needs to pass in a file path. Wb is a Workbook object.

Wb = openpyxl.load_workbook ("province, city, county. Xlsx")

An Excel document consists of multiple sheet.

Get all the sheet of the Excel document.

SheetList = wb.get_sheet_names ()

Get the corresponding table based on the sheet name. The table is represented by a Worksheet object, and sheet is a Worksheet object.

Sheet = wb.get_sheet_by_name ("sheet name")

Get the values in the sheet table

Numerical positioning: row is the line number and column is the column number.

Character positioning: Excle represents columns with letters and rows with numbers.

X = sheet.cell (row=1, column=1). Value # the first expression y = sheet ['A1']. Value # the second expression

The conversion between column letters and numbers.

Openpyxl.cell.column_index_from_string () # letters-> numbers openpyxl.cell.column_letter () # numbers-> letters

Gets how big the sheet is.

Sheet.get_highest_row () # return number of rows sheet.get_highest_column () # return number of columns

Traversing sheet

For i in range (1, sheet.get_highest_row () + 1): for j in range (1, sheet.get_highest_column () + 1): print (str (sheet1.cell (row=i, column=j) .value) + ", end=") print ()

Write operation

Create a new Workbook object.

Wb = openpyxl.Workbook ()

Save the file. (pass in file name)

Wb.save (".xlsx")

The creation of sheet,index represents the order of sheet, and title represents the name of sheet.

Wb.create_sheet (index=0,title='')

To delete sheet, you need to pass in a Worksheet object. If you know the name of sheet, you can delete it using the following code

Wb.remove_sheet (wb.get_sheet_by_name ("sheet name"))

Write data

Sheet.cell (row=1, column=1) = x # the first means sheet ['A1'] = y # so far, I believe you have a deeper understanding of "how python's openpyxl is used", you might as well do it in practice! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report