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

Using openpyxl to realize 99 multiplication table

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

As a member of excel, openpyxl is easy to use and has a wide range of functions.

Environment:

Win 10

Python 3.6

Pip 9.0.1

Openpyxl 2.4.8

Install openpyxl

Open the cmd command line, enter pip install openpyxl, and press enter to execute the command.

Study

Create an excel file (Create a workbook)

First import the Workbook in the openpyxl library

From openpyxl import Workbookwb = Workbook ()

Create worksheet (Create sheet)

Ws = wb.create_sheet (title='math',index=0)

Parameter description:

Title, defining the sheet name

Index, which defines the sort location of the new sheet. When this parameter is omitted, the newly created sheet is added to the last.

Access cells (Accessing one cell)

# method1ws ['A4'] = 4 # method2d = ws.cell (row=4, column=2, value=10)

The method of accessing the cell is the same as excel, and you can use the method of A1 Magi B12.

Access unit block (Accessing many cells)

# method1cell_range = ws ['A _ 1 _ blank _ virtual _ D2'] # method2ws.iter_rows (min_row=1,max_row=3, min_col=2,max_col=3) # selected cell block of A1:C3

Data storage (data storage)

C = ws ['A12'] c.value =' hellos'

Save file (save file)

Wb.save ('balances.xlsx')

Load Fil

From openpyxl import load_workbookwb = load_workbook ('..\..\\ file\\ math.xlsx') print (wb.get_sheet_names ())

learn in order to practise

From openpyxl import Workbook# create a new workbootwb = Workbook () # create a sheet,sheetname is math, put it in the first position ws = wb.create_sheet ('math',0) m = access the cell block using the method Assign for row in ws.iter_rows (min_row=1,max_row=9,min_col=1,max_col=9) to the cell while traversing: # for row in ws ['A11 for cell in row for cell in row I19']: cell n = 0 cell: n=n+1 cell.value=m*nwb.save ('..\..\\ file\\ math.xlsx')

Using the method of accessing the cell block, the 99 multiplication table is implemented, and the result is as follows:

references

Https://openpyxl.readthedocs.io/en/latest/tutorial.htm

Supplementary explanation

When looking at the materials, I also found an article, compared with a variety of excel libraries, good articles, stay and read.

Http://www.jianshu.com/p/be1ed0c5218e

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