引用:https://www.cnblogs.com/programmer-tlh/p/10461353.html
# 创建工作簿
wb = openpyxl.Workbook()
# 保存excel
wb.save('d:/test.xls')
打开已有excel
# 打开excel
wb = openpyxl.load_workbook("d:/test.xlsx")
# 打开sheet
ws = wb['test1']
# 方式一:数据可以直接分配到单元格中(可以输入公式)
ws['A1'] = 'test'
# 方式二:可以附加行,从第一列开始附加(从最下方空白处,最左开始)(可以输入多行)
ws.append(['id', 'name', 'age'])
# 方式一:插入到最后(default)
ws = wb.create_sheet('test2')
# 方式二:插入到指定的位置
ws = wb.create_sheet('test3', 0)
# 如果sheet名已存在,会在sheet名后面追加数字(如已有test、test1、test3,新创建的为test4)
ws = wb.create_sheet('test')
# sheet 名称可以作为 key 进行索引
ws = wb['test']
ws1 = wb.get_sheet_by_name('test1')
# 显示所有表名
wb.sheetnames
['test3', 'test', 'test4', 'test1', 'Sheet', 'Sheet1', 'test2', 'test5']
# 判断表格是否存在
'test' in wb.sheetnames
True
# 方式一
wb.remove(sheet)
# 方式二
del wb[sheet]
# 方法一:没有值返回None
>>> c = ws['A1']
# 方法二:row 行,column 列,默认值
>>> d = ws.cell(4, 2, value=10)
切片多单元格应当符合:从左到右,从上到下
# 获取某行
>>> ws['1']
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>)
# 获取某列
>>> ws['A']
(<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>)
# A列到B列
>>> ws['A':'B']
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.B4>))
# 第行到第4行
>>> ws['1':'4']
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>),
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>),
(<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>))
# 单元格到单元格:一行中的某段
>>> ws['A1':'B1']
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>),)
# 单元格到单元格:从左到右,从上到下
>>> ws['A1':'B2']
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>))
# 不合法的姿势
>>> ws['A2':'B1']
()
>>> ws['B2':'A1']
()
>>> ws['B2':'A1':-1]
()
ws.iter_rows
、ws.iter_cols
四个可填参数:
# 指定范围(行 → 行) 与ws['A1':'C2']效果相同
>>> ws.iter_rows(min_row=1, max_col=3, max_row=2)
<generator object Worksheet._cells_by_row at 0x00000181E5939648> # 返回生成器
>>> tuple(ws.iter_rows(min_row=1, max_col=3, max_row=2))
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>))
# 指定范围(列 → 列)
>>> tuple(ws.iter_cols(max_col=3, max_row=2))
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>),
(<Cell 'Sheet1'.C1>, <Cell 'Sheet1'.C2>))
返回生成器,行–>行
>>> ws.rows
<generator object Worksheet._cells_by_row at 0x00000181E597F448>
>>> tuple(ws.rows)
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.B1>, <Cell 'Sheet1'.C1>),
(<Cell 'Sheet1'.A2>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.C2>),
(<Cell 'Sheet1'.A3>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.C3>),
(<Cell 'Sheet1'.A4>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.C4>),
(<Cell 'Sheet1'.A5>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.C5>),
(<Cell 'Sheet1'.A6>, <Cell 'Sheet1'.B6>, <Cell 'Sheet1'.C6>))
返回生成器,列–>列
>>> tuple(ws.columns)
((<Cell 'Sheet1'.A1>, <Cell 'Sheet1'.A2>, <Cell 'Sheet1'.A3>, <Cell 'Sheet1'.A4>, <Cell 'Sheet1'.A5>, <Cell 'Sheet1'.A6>),
(<Cell 'Sheet1'.B1>, <Cell 'Sheet1'.B2>, <Cell 'Sheet1'.B3>, <Cell 'Sheet1'.B4>, <Cell 'Sheet1'.B5>, <Cell 'Sheet1'.B6>),
(<Cell 'Sheet1'.C1>, <Cell 'Sheet1'.C2>, <Cell 'Sheet1'.C3>, <Cell 'Sheet1'.C4>, <Cell 'Sheet1'.C5>, <Cell 'Sheet1'.C6>))
wb.save('d:/test.xlsx')
ws.sheet_properties.tabColor = "1072BA"
# 第2行行高
sheet.row_dimensions[2].height = 40
# C列列宽
sheet.column_dimensions['C'].width = 30
# 获得最大列和最大行
print(sheet.max_row)
print(sheet.max_column)
from openpyxl.utils import get_column_letter, column_index_from_string
# 根据列的数字返回字母
print(get_column_letter(2)) # B
# 根据字母返回列的数字
print(column_index_from_string('D')) # 4
# 合并单元格, 往左上角写入数据即可
sheet.merge_cells('B1:G1') # 合并一行中的几个单元格
sheet.merge_cells('A1:C3') # 合并一个矩形区域中的单元格
sheet.unmerge_cells('A1:C3')