import openpyxl |
from openpyxl.styles import Font, Alignment, Border, Side |
# 打开 Excel 文件 |
workbook = openpyxl.load_workbook( 'example.xlsx' ) |
# 选择工作表 |
sheet = workbook[ 'Sheet1' ] |
# 设置单元格字体、对齐方式和边框 |
font = Font(name = 'Arial' , size = 12 , bold = True , italic = False , color = 'FF0000' ) |
alignment = Alignment(horizontal = 'center' , vertical = 'center' ) |
border = Border(left = Side(style = 'thin' ), right = Side(style = 'thin' ), top = Side(style = 'thin' ), bottom = Side(style = 'thin' )) |
sheet[ 'A1' ].font = font |
sheet[ 'A1' ].alignment = alignment |
sheet[ 'A1' ].border = border |
# 保存 Excel 文件 |
workbook.save( 'example.xlsx' ) |