1) Introduction of libraries
- Import OPENPYXL #引入整个库, use openpyxl.xxx form when calling corresponding
- From openpyxl import Workbook #引入Workbook对象, calling Workbook new workbook
- From OPENPYXL import Load_workbook #引入load_workbook, import the completed workbook
2) Create a new workbook
- WB = Workbook ()
- WB = OPENPYXL. Workbook ()
3) Open an existing workbook
- WB = Load_workbook (' sample.xlsx ')
- WB = Openpyxl.load_workbook (' sample.xlsx ')
4) Get sheet control handle
- WS = Wb.active #取得活动页
- WS = Wb.create_sheet (' newsheet ') #在已有sheet位置后新建sheet
- WS = Wb.create_sheet (' newsheet ', 0) #在已有sheet位置前新建sheet
- ws = wb[' Oldsheet '] #获得已有sheet
5) Sheet Property
- Ws.title = ' new title ' #修改sheet名字
- Ws.sheetnames #所有sheet名列表 Example: Print (ws.sheetnames) →[' Sheet1 ', ' New Title ', ' Sheet3 ']
6) Circulating-sheet
for inch WB: Print (Sheet.title)
7) Select cell
- c = ws[' A4 ')
- ws[' A4 '] = 8
- D = Ws.cell (row=4, column=2, value=10)
- Cell_range = ws[' A1 ': ' C2 ']
- Colc = ws[' C ')
- Col_range = ws[' c:d ']
- ROW10 = ws[10]
- Row_range = Ws[5:10]
- Ws.rows
- Ws.columns
8) Circulating-cell
for in Ws.iter_rows (Min_row=1, max_col=3, max_row=2): ... for inch row: ... Print (cell)<cell Sheet1.a1><cell Sheet1.b1><cell Sheet1.c1><cell Sheet1.a2><cell Sheet1.b2><cell sheet1.c2>
for in Ws.iter_cols (Min_row=1, max_col=3, max_row=2): ... for inch col: ... Print (cell)<cell Sheet1.a1><cell Sheet1.a2><cell Sheet1.b1><cell Sheet1.b2><cell Sheet1.c1><cell sheet1.c2>
9) Data Preservation
>>> c.value = " hello, World Span style= "COLOR: #800000" > " >>> print (C.value) " hello, World " >>> d.value = 3.14>>> print (d.value) 3.14
>>> wb = Workbook (guess_types=True)'12%'print(c.value) Import datetime>>> d.value = datetime.datetime.now ()print D.valuedatetime.datetime (9, ten, +, +)'31.50' Print(c.value)31.5
A) Save the file
- Wb.save (' sample.xlsx ')
Openpyxl Method Records