创建工作簿
from openpyxl as xl
# 实例化
wb = xl.WorkBook()
# 激活worksheet
ws = wb.active
读取
from openpyxl as xl
wb = xl.load_workbook('file_path')
# sheet的列表,工作簿中所有的工作表,返回**列表**
list_ws = wb.sheetnames
# 读取表格
ws = wb['Sheet']
# 获取表格最大行
row = ws.max_row
# 获取表格最大列
column = ws.max_column
创建表格
# 在最后插入表格
ws1 = wb.create_sheet('MySheet')
# 在指定位置插入表格
ws2 = wb.create_sheet("MySheet", 1) # 1是重左往右第二张表,根据列表的索引位置
访问
# 单一单元格访问
c = ws['A1'].value
d = ws.cell(row=4, column=2).value
# 多单元访问
cs = ws['A1': 'C2']
for i in cs:
print(i.value)
cs1 = ws['A'] #访问列
for i in cs1:
print(i.value)
cs2 = ws[10] #访问行,范围是1到max_column
for i in cs2:
print(i.value)
# 指定行列
for i in ws.iter_cols(min_row=1, max_col=3, max_row=3, min_col=2):
for j in i:
print(j)
--------------------------------------------
<Cell 'Sheet'.B1>
<Cell 'Sheet'.B2>
<Cell 'Sheet'.B3>
<Cell 'Sheet'.C1>
<Cell 'Sheet'.C2>
<Cell 'Sheet'.C3>
---------------------------------------------
for i in ws.iter_rows(min_row=1, max_col=3, max_row=3, min_col=2):
for j in i:
print(j)
----------------------------------------------
<Cell 'Sheet'.B1>
<Cell 'Sheet'.C1>
<Cell 'Sheet'.B2>
<Cell 'Sheet'.C2>
<Cell 'Sheet'.B3>
<Cell 'Sheet'.C3>
-----------------------------------------------
# 遍历所有
tuple(ws.rows)
------------------------------------------------
((<Cell 'Sheet'.A1>, <Cell 'Sheet'.B1>, <Cell 'Sheet'.C1>),
(<Cell 'Sheet'.A2>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.C2>),
(<Cell 'Sheet'.A3>, <Cell 'Sheet'.B3>, <Cell 'Sheet'.C3>))
-------------------------------------------------
for i in ws.rows:
for j in i:
print(j)
-----------------------------------------------------
<Cell 'Sheet'.A1>
<Cell 'Sheet'.B1>
<Cell 'Sheet'.C1>
<Cell 'Sheet'.A2>
<Cell 'Sheet'.B2>
<Cell 'Sheet'.C2>
<Cell 'Sheet'.A3>
<Cell 'Sheet'.B3>
<Cell 'Sheet'.C3>
---------------------------------------------------
tuple(ws.columns)
------------------------------------------------
((<Cell 'Sheet'.A1>, <Cell 'Sheet'.A2>, <Cell 'Sheet'.A3>),
(<Cell 'Sheet'.B1>, <Cell 'Sheet'.B2>, <Cell 'Sheet'.B3>),
(<Cell 'Sheet'.C1>, <Cell 'Sheet'.C2>, <Cell 'Sheet'.C3>))
-------------------------------------------------
for i in ws.columns:
for j in i:
print(j)
--------------------------------------------------
<Cell 'Sheet'.A1>
<Cell 'Sheet'.A2>
<Cell 'Sheet'.A3>
<Cell 'Sheet'.B1>
<Cell 'Sheet'.B2>
<Cell 'Sheet'.B3>
<Cell 'Sheet'.C1>
<Cell 'Sheet'.C2>
<Cell 'Sheet'.C3>
--------------------------------------------------
for i in range(1, ws.max_row):
for j in range(1, ws.max_column):
print(ws.cell(i, j))
-------------------------------------------------
<Cell 'Sheet'.A1>
<Cell 'Sheet'.B1>
<Cell 'Sheet'.A2>
<Cell 'Sheet'.B2>
--------------------------------------------------
字母与数字的相互转换
from openpyxl.utils improt get_column_letter, column_index_from_string
# 数字转字母
print(get_column_letter(2)) #B
# 字母转数字
print(column_index_from_string('D')) #4
删除工作表
del wb['Sheet']
矩阵置换
rows = [
['Number', 'data1', 'data2'],
[2, 40, 30],
[3, 40, 25],
[4, 50, 30],
[5, 30, 10],
[6, 25, 5],
[7, 50, 10]]
list(zip(*rows))
------------------------------------------------------------------------
[('Number', 2, 3, 4, 5, 6, 7),
('data1', 40, 40, 50, 30, 25, 50),
('data2', 30, 25, 30, 10, 5, 10)]
------------------------------------------------------------------------
rows = [
['Number', 'data1', 'data2'],
[2, 40 ], # 这里少一个数据
[3, 40, 25],
[4, 50, 30],
[5, 30, 10],
[6, 25, 5],
[7, 50, 10],
]
list(zip(*(rows)))
-----------------------------------------------------------------------------------------------
[('Number', 2, 3, 4, 5, 6, 7), ('data1', 40, 40, 50, 30, 25, 50)]
-----------------------------------------------------------------------------------------------
设置单元格样式
字体
# 指定了等线(name)24号(size),加粗斜体(blod),字体颜色红色(color)。直接使用cell的font属性,将Font对象赋值给它
from openpyxl.styles import Font
bold_itatic_24_font = Font(name='等线', size=24, italic=True, color=colors.RED, bold=True)
sheet['A1'].font = bold_itatic_24_font
对齐方式
# 直接使用cell的属性aligment,这里指定垂直(vertical)居中和水平(horizontal)居中。除了center,还可以使用right、left等等参数
sheet['B1'].alignment = Alignment(horizontal='center', vertical='center')
行高,列宽
# 第2行行高
sheet.row_dimensions[2].height = 40
# C列列宽
sheet.column_dimensions['C'].width = 30
合并,拆分单元格
# 合并单元格, 往左上角写入数据即可
sheet.merge_cells('B1:G1') # 合并一行中的几个单元格
sheet.merge_cells('A1:C3') # 合并一个矩形区域中的单元格
sheet.unmerge_cells('A1:C3') #拆分单元格
单元格背景色
from openpyxl.styles import PatternFill
fille = PatternFill('solid', fgColor='ffffff') # 设置填充颜色为 橙色
sheet1.cell(row=2, column=8).fill = fille # 序列