python openpyxl module study notes

python openpyxl module study notes


Preface

Xiaobai, please give me your advice!
Welcome to make mistakes! ! !

One, openpyxl library

The openpyxl library is a python library that can read and write Excel (it seems to be at least the 2010 version of Excel)

Second, use basic operations

1. Create a new Excel table/open an existing Excel table

1>Create a new Excel table

The code is as follows (example):

from  openpyxl import  Workbook

wb = Workbook() # 实例化
ws = wb.active #激活 worksheet

'''这里加上读写操作代码块'''

wb.save('workbook1.xlsx')#保存

2>Open an existing Excel sheet

from openpyxl import load_workbook

wb = load_workbook('workbook1.xlsx')#实例化
ws = wb.active#激活当前活跃的工作表,获取当前活跃的sheet对象
#或者是ws = wb['Sheet']

'''这里加上读写操作代码'''

wb.save('workbook1.xlsx')#保存文件

ws = wb.active can be written as ws = wb['Sheet'].
These two functions are the same to obtain a sheet object. The
former is to obtain the currently active worksheet as the object, and the latter is to obtain the worksheet in Excel by name. Object (the two can be selected and used according to needs)

2. Write data

1>Specify cells to fill in

from openpyxl import Workbook

wb = Workbook()
ws = wb['Sheet']# 获取Sheet工作表对象

# 设置内容
ws['A1'] = 'Hello World!'#在A列第1行填入 Hello World!

import datetime#一般的import都放在前面,这里为了方便阅读
ws['A2'] = datetime.datetime.today()#在A2单元格填入现在的时间

wb.save('new_sample.xlsx')#保存

2>Fill in line by line

from openpyxl import Workbook
wb1 = Workbook()
ws1 = wb1.active
ws1.append(['A1','B1','C1','D1'])
ws1.append([1,2,3,4,])
wb1.save('workbook1.xlsx')

The effect is like this
The effect is like this

3. Read the data

First of all, I have to talk about three generators:
rows: get cells by row (cell object)
columns: get cells by column (cell object)
values: get cell content (cell data) by row
Note: For the above three I don’t really understand the specific definition of the actor. The values ​​obtained by values ​​seem to be tuple types.

You can try the printout more to explore the role of each one, but it will be unspeakable (limited ability, will not describe-.-)

1>Print out the content of a certain cell

from openpyxl import load_workbook
wb1 = load_workbook('workbook1.xlsx')
ws1 = wb1.active
a1 = ws1['A1']
print(a1)#打印结果为<Cell 'Sheet'.A1> a1应该是为类里的一个cell对象
print(type(a1))#打印结果为<class 'openpyxl.cell.cell.Cell'>,
print(a1.value)#打印结果为 A1 (.value ->单元格的值。加上.value之后应该是打印 对象a1的属性值。)
wb1.save('workbook1.xlsx')

Note: The a1 and its type here are the author’s guess, waiting for comments from the big guys in the comment area to answer~~

2>Print out the value of a certain column

from openpyxl import load_workbook
wb1 = load_workbook('workbook1.xlsx')
ws1 = wb1.active
a = ws1['A']
for i in a:
    print(i.value)
wb1.save('workbook1.xlsx')

3>Print out the value of each cell (print line by line)

from openpyxl import load_workbook
wb1 = load_workbook('workbook1.xlsx')
ws1 = wb1.active

for cell in ws1.values:
    print(*cell)
    
wb1.save('workbook1.xlsx')

The printing result is as follows.
Insert picture description here
Note: The cell is a tuple type here, and * is printed in front of it when printing. I don’t know why this effect can be achieved. Please comment in the comments section~~

to sum up

Questions and errors please point out in the comment section

Guess you like

Origin blog.csdn.net/qq_51182221/article/details/112344539