批量处理调整Excel内容
修改字体样式
Font(name=字体名称,size=字体大小, bold=是否加粗,italic=是否斜体,color=字体颜色)
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl.styles import Font
from openpyxl import load_workbook
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
cell = sheet['A1']
font = Font(name = '思源黑体 Regular',size = 12,bold=True,italic = True,color = 'FF0000' )
cell.font = font
workbook.save(filename='这是一个表格.xlsx')
获取表格中字体的样式
cell.font.属性
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl import load_workbook
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
cell = sheet['A2']
font = cell.font
print(font.name, font.size, font.bold, font.italic)
–> 输出结果为:宋体 11.0 False False
设置对齐样式
Alignment(horizontal=水平对齐模式,vertical=垂直对齐模式,text_rotation=旋转角度,wrap_text=是否自动换行)
关于水平样式和垂直样式具体的设置,可以通过参数配置进行修改
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl import load_workbook
from openpyxl.styles import Alignment
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
cell = sheet['A1']
alignment = Alignment(horizontal='center',vertical='center',text_rotation=45 )
cell.alignment = alignment
workbook.save(filename='这是一个表格.xlsx')
设置边框样式
Side(style=边线样式,color=边线颜色)
Border(left=左边线样式,right=右边线样式,top=上边线样式,bottom=下边线样式)
关于边线样式的设置,可以通过参数配置进行修改
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl.styles import Border,Side
from openpyxl import load_workbook
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
cell = sheet['A3']
side = Side(style='thin',color='FF000000')
border = Border(left=side,right=side,top=side,bottom=side)
cell.border = border
workbook.save(filename='这是一个表格.xlsx')
填充
PatternFill(fill_type=填充样式, fgColor=填充颜色)
GradientFill(stop=(渐变颜色1,渐变颜色2,…))
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl.styles import PatternFill,GradientFill
from openpyxl import load_workbook
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
cell_3 = sheet['A3']
pattern_fill = PatternFill(fill_type='solid',fgColor='99ccff')
cell_3.fill = pattern_fill
cell_4 = sheet['A4']
gradient_fill = GradientFill(stop=('FFFFFF','99CCFF','000000'))
cell_4.fill = gradient_fill
workbook.save(filename='这是一个表格.xlsx')
设置行高和列宽
.row_dimensions[行编号].height = 行高
.column_dimensions[列编号].width = 列宽
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl import load_workbook
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
sheet.row_dimensions[1].height = 100
sheet.column_dimensions['B'].width = 40
workbook.save(filename='这是一个表格.xlsx')
合并单元格
.merge_cells(待合并的格子编号)
.merge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl import load_workbook
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
sheet.merge_cells('C1:D2')
sheet.merge_cells(start_row=7,start_column=1,end_row=8,end_column=4)
workbook.save(filename='这是一个表格.xlsx')
取消合并单元格
.unmerge_cells(待合并的格子编号)
.unmerge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)
import os
os.chdir('C:\\Users\\Administrator\\Desktop\\test')
from openpyxl import load_workbook
workbook = load_workbook(filename = '这是一个表格.xlsx')
sheet = workbook.active
sheet.unmerge_cells('C1:D2')
sheet.unmerge_cells(start_row=7,start_column=1,end_row=8,end_column=4)
workbook.save(filename='这是一个表格.xlsx')
来源:CSDN
作者:Python学习中的进阶者
链接:https://blog.csdn.net/weixin_42850424/article/details/104638128