列宽
from openpyxl.utils import get_column_letter
ws.column_dimensions[get_column_letter(5)].width = 24
行高
ws.row_dimensions[1].height = 15
合并单元格
range_string = 'A1:D1'
range_string = 'A3:F5'
ws.merge_cells(range_string)
冻结窗口
# 冻结首列
ws.freeze_panes = 'B1'
# 冻结首行
ws.freeze_panes = 'A2'
# 冻结首列和首行
ws.freeze_panes = 'B2'
超链接
# 方法1
value='=HYPERLINK("http://www.baidu.com", "baidu")'
ws.cell(row, colum, value=value)
# 方法2
_ = sheet.cell(row, column, value='baidu')
_.hyperlink = 'http://www.baidu.com'
公式
value = '=A2+A5'
value = '=SUM(A1:A5)'
ws.cell(row, column, value=value)
批注
from openpyxl.comments import Comment
cell.comment = Comment('批注内容', '姓名')
添加图片
img = openpyxl.drawing.image.Image(img_path)
anchor = 'C2'
ws.add_image(img, anchor=anchor)
筛选与排序
需要点击才可以触发筛选
ws.auto_filter.ref = "A1:B5"
ws.auto_filter.add_filter_column(0, ["Kiwi", "Apple", "Mango"])
ws.auto_filter.add_sort_condition("B2:B5")