Practical basic read and write operations of openpyxl

installation

pip install openpyxl

Read file

basic method

from openpyxl import load_workbook

filepath = r'H:\tmp\test.xlsx'

# 读取指定Excel文件
wbook = load_workbook(filepath)

# 通过属性sheetnames获取所有Worksheet的名字
print(wbook.sheetnames)

# wsheet = wbook.get_sheet_by_name('Sheet1') //老版本
wsheet = wbook['Sheet1']

# 如果读第一个sheet也可以使用下面的方式
# wsheet = wbook[wbook.sheetnames[0]]
# wsheet = wbook.active

# 获取sheet总行数和总列数
rows = wsheet.max_row
cols = wsheet.max_column

# 通过行列获取单元格
scell = wsheet.cell(row=1, column=1)

# cell常用的属性:
# row:cell所在的行,column:cell所在的列
# column_letter:列的英文标签,value:cell的值
# number_format:数据格式,data_type:数据类型
print("cell row:{}".format(scell.row))
print("cell column:{}".format(scell.column))
print("cell column_letter:{}".format(scell.column_letter))
print("cell value:{}".format(scell.value))
print("cell number_format:{}".format(scell.number_format))
print("cell data_type:{}".format(scell.data_type))

# 可以像在excel中一样,直接通过列行定位获取单元格值
print(wsheet['A1'].value)

The contents of the file are as follows:

document content

The output is roughly as follows:

['Sheet1', 'Sheet2', 'Sheet3']
cell row:1
cell column:1
cell column_letter:A
cell value:id
cell number_format:General
cell data_type:s
id

Traverse

from openpyxl import load_workbook

filepath = r'H:\tmp\test.xlsx'

# 读取指定Excel文件
wbook = load_workbook(filepath)

wsheet = wbook['Sheet1']

# 获取A1到C2之间的所有cell
print(wsheet['A1':'C2'])

# 获取2到3行的所有cell
print(wsheet[2:3])

# 获取B到C列的所有cell
print(wsheet['B:C'])

# iter_rows按行迭代,min_row设置起始行从1开始,max_row设置读取最大行
# min_col,max_col同理,设置读取最小最大列
for row in wsheet.iter_rows(min_row=1, max_col=3, max_row=2):
    for cell in row:
        print(cell)

# values_only参数只获取值
for row in wsheet.iter_rows(min_row=1, max_col=3, max_row=2, values_only=True):
    print(row)

# iter_cols按列迭代
for col in wsheet.iter_cols(min_row=1, max_col=3, max_row=2):
    for cell in col:
        print(cell)

# wsheet.values按行获取,每行一个tuple
for row in wsheet.values:
    for value in row:
        print(value)

Write data

from openpyxl import Workbook

filepath = r'H:\tmp\base.xlsx'

# 创建一个excel工作簿
wb = Workbook()

# 创建之后可以通过active获取默认的第一个
default_wsheet = wb.active
# 可以通过title设置sheet的名字
default_wsheet.title = "default_wsheet"

# create_sheet新创建的sheet默认是添加在末尾的
appendWs = wb.create_sheet("append-wsheet")

# 可以通过参数来指定sheet位置,0表示第一个
first_sheet = wb.create_sheet("0-wsheet", 0)

# 插入到倒数第二的位置
wb.create_sheet("-1-wsheet", -1)

# sheet可以通过append来添加一行(row)
for row in range(1, 3):
    first_sheet.append(range(10))

# 通过cell设置值,参数:行,列,值
first_sheet.cell(4, 2, "HELLO")

# 也可以通过excel坐标定位设置值
first_sheet['A1'] = "A1 value"

# 保存文件
wb.save(filepath)

The content of the output file is roughly as follows:

Result file

We can see that 0-wsheet is in the first position, because it was inserted later, default_wsheet was originally in the first position, but because it was re-inserted, it became the second.
append-wsheet is passed without specifying the position, so at the end, -1-wsheet specifies the position, which is the second to last.

Guess you like

Origin blog.csdn.net/trayvontang/article/details/107883497