python使用openpyxl操作excel

# 获取所有sheet页
wb.sheetnames
# 根据名字获取sheet
wb['name']
# 创建sheet
wb.create_sheet(title='name',index = 1)

设置边框/字体/对齐方式/背景

from openpyxl.styles import Border, Side, Alignment, Font, Color, PatternFill
from openpyxl.utils import get_column_letter, column_index_from_string

# 字体
cell.font = Font(name=u'微软雅黑', size=10, bold=True, italic=True)  # 字体,大小,加粗,斜体
# 边框
cell.border = Border(left=Side(border_style='thin', color='00000000'),
                                    right=Side(border_style='thin', color='00000000'),
                                    top=Side(border_style='thin', color='00000000'),
                                    bottom=Side(border_style='thin', color='00000000'))
# 对齐方式
cell.alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)  # 左右居中,上下居中,自动换行
# 合并
ws.merge_cells('J1:Q1')
ws.merge_cells(strat_row,start_column,end_row,end_column)
# 获取合并的位置
ws.merged_cells(1,1). # 左上角为第一行第一列 返回(1,1,2,2) 即开始行列,结束行列
# 取消合并
ws.unmerge_cells(1,1). # 左上角为第一行第一列 
# 设置行高 列宽
ws.row_dimensions[1].height = 58
ws.column_dimensions['A'].wight = 150  # 列宽
# 背景颜色
cell.fill = PatternFill('solid', fgColor='B8BBDE')
# 根据列的数字返回字母
print(get_column_letter(2))  # B
# 根据字母返回列的数字
print(column_index_from_string('D'))  # 4
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值