Detailed openpyxl

1. Create / Read excel file

Creating excel

from openpyxl import Workbook
wb = Workbook()

Read excel

from openpyxl import load_workbook
wb = load_workbook('1.xlsx')

Save excel

wb.save('filename.xlsx')

2.sheet form action

Gets sheet

# Way to list all sheet returns excel file name (-> list [str, str ..]) 
wb.sheetnames 
wb.get_sheet_names ()   

Select the sheet objects

 

# Select The sheet name 
WS WB = [ 'Sheet1'] 
WS = wb.get_sheet_by_name ( 'Sheet1')   
# the currently displayed, the active sheet 
WS = wb.active 
WS = wb.get_active_sheet ()

Create a new sheet

# Default inserted in the last 
WS = wb.create_sheet ( "NewSheet") 
# inserted into the beginning position (counted from 0) 
WS = wb.create_sheet ( "NewSheet", 0)

Copy a sheet objects

source = wb.active
target = wb.copy_worksheet(source)

Delete sheet

wb.remove(sheetobject) 
del wb['sheet'] #sheetname

Common property sheet

#sheet name 
sheet.title 
# maximum row and maximum column 
sheet.max_row 
sheet.max_column 
# ranks generator 
sheet.rows # row generator, which is a target cell for each row by a tuple package. 
sheet.columns # column generator, which is a target cell for each column, a tuple from the package. 

May be used list (sheet.rows) [0] .value the like to obtain data, or 
for in sheet.rows Row: 
    for in Row Cell: 
        Print (Cell.Value) 
to traverse value, or the value generator only sheet.values traversal value

Other operations of the sheet

# Insert rows, insert before the line 7 
ws.insert_rows (7) 
# insert a column, insert column before 7 
ws.insert_cols (7) 
# delete ranks 
ws.delete_rows (7) 
ws.delete_cols (7) 
# can be deleted a plurality 
ws.delete_cols (6, 3)

3. The cell object

Cell object selected cell

# The Name Access 
column cell row 1 subject WS = A1 [ 'A1'] #A 
A2 = WS [ 'A2'] # or lowercase 
#cell access method 
b2 = ws.cell (row = 2, column = 2) 
ws.cell = B3 (3,2-) 
# returns the list from cell 
B3 = list (ws.rows) [2] [. 1] 
B3 = list (ws.columns) [. 1] [2]

Cell Properties

# Returns the column 
cell.column 
# returns rows 
cell.row 
# Return Value 
cell.value 
Note: If the cell is the use of the formula, the value is a value instead of calculating equation 
# return unit formatting attributes 
cell.number_format 
default General format 
# cell styles 
cell.font

Change the value in cell

# Direct assignment 
WS [ 'A2'] = 222 
WS [ 'A2'] = 'AAA' 
WS [ 'B2'] = '= the SUM (A1: A17)' using Equation # 

#value property assignment 
cell.value = 222 
or 
ws.cell (1,2, value = 222)

Move cells

ws.move_range ( "D4: F10", rows = -1, cols = 2) 
represents cells D4: F10 moves up one line, the right two columns. Cell will overwrite any existing cells. (The latest version of this method will use pip list to see whether the latest version) 
ws.move_range ( "G4: H10", rows = 1, cols = 1, Translate = True) 
move automatically included in the conversion formula

Combined with the split cells

# Merged cell, the upper left corner to write data or read data 
ws.merge_cells ( 'A2: D2 of') 
# split cells 
ws.unmerge_cells ( 'A2: D2')

Guess you like

Origin blog.csdn.net/weixin_45198279/article/details/94655601