【PYTHON,EXCEL】利用python进行EXCEL处理3 批量处理数据

此生再无相见时 提交于 2020-02-13 23:02:34

上次问介绍了Excel的基本操作,现在我们便谈一下如何批量处理这些数据
1.查看表格内字体的样式

font.name=字体名称,font.size=字体大小,
font.bold=字体是否加粗,font.italic=字体是否斜体

font.name,font.size,font.bold,font.italic

from openpyxl import load_workbook
from openpyxl.styles import Font

workbook = load_workbook(filename="2.xlsx")
sheet = workbook.active
cell = sheet["A1"]
font = cell.font
print(font.name,font.size,font.bold,font.italic)
workbook.save(filename="2.xlsx")
等线 11.0 False False

2.修改字体样式

from openpyxl import load_workbook
from openpyxl.styles import Font

workbook = load_workbook(filename="2.xlsx")
sheet = workbook.active
cell = sheet["A1"]
cell.font = Font(name="宋体",size="20",bold=True,italic="True",color="FF0000")
font = cell.font
print(font.name,font.size,font.bold,font.italic)
workbook.save(filename="2.xlsx")
宋体 20.0 True True

3.对齐样式
Alignment(horizontal=水平对齐模式",vertical=“垂直对齐模式”,text_rotation=旋转角度,wrap_text=是否自动换行)

from openpyxl import load_workbook
from openpyxl.styles import Font
from openpyxl.styles import Alignment

workbook = load_workbook(filename="2.xlsx")
sheet = workbook.active
cell = sheet["A1"]
alignment = Alignment(horizontal="center",vertical="center",text_rotation=45)
cell.alignment = alignment
workbook.save(filename="2.xlsx")

在这里插入图片描述
horizontal代表水平方向,可以左对齐left,还有居中center和右对齐right,分散对齐distributed,跨列居中centerContinuous,两端对齐justify,填充fill,常规general

vertical代表垂直方向,可以居中center,还可以靠上top,靠下bottom,两端对齐justify,分散对齐distributed

另外还有自动换行:wrap_text,这是个布尔类型的参数,这个参数还可以写作wrapText
4.边框样式

from openpyxl import load_workbook
from openpyxl.styles import Side,Border

workbook = load_workbook(filename="2.xlsx")
sheet = workbook.active
cell = sheet["A1"]
side1 = Side(style="dotted",color="FF0000")
side2 = Side(style="thin",color="FF0000")
border =Border(left=side1,right=side1,top=side2,bottom=side2)
cell.border = border
workbook.save(filename="2.xlsx")

在这里插入图片描述
边框样式dashDot’,‘dashDotDot’,‘dashed’,‘dotted’,‘double’,‘hair’,‘medium’,‘mediumDashDot’,‘mediumDashDotDot’,‘mediumDashed’,‘slantDashDot’,‘thick’,‘thin’
5.设置填充样式

from openpyxl import load_workbook
from openpyxl.styles import PatternFill,GradientFill

workbook = load_workbook(filename="2.xlsx")
sheet = workbook.active
cell1 = sheet["A3"]
cell2 = sheet["B4"]
pattern_fill = PatternFill(fill_type="solid",fgColor="99ccff")
gradient_fill = GradientFill(stop=('FF0000','FFFFFF','000000'))
cell1.fill = pattern_fill
cell2.fill = gradient_fill
workbook.save(filename="2.xlsx")

在这里插入图片描述

PatternFill(fill_type=填充样式,fgColor=填充颜色)
GradientFill(stop=(色1,色2,…,色n))

6.设置行高列宽

workbook = load_workbook(filename="2.xlsx")
sheet = workbook.active
sheet.row_dimensions[1].height = 30
sheet.column_dimensions["A"].width = 20
workbook.save(filename="2.xlsx")

在这里插入图片描述

sheet.row_dimensions[行编号].height = 行高
sheet.column_dimensions[“列编号”].width = 列宽

7.单元格的操作
合并单元格:

workbook = load_workbook(filename="2.xlsx")
sheet = workbook.active
sheet.merge_cells("A0:D5")
sheet.merge_cells(start_row=1,start_column=3,end_row=6,end_column=6)
workbook.save(filename="2.xlsx")

在这里插入图片描述

sheet.merge_cells(“从哪到哪”)
sheet.merge_cells(start_row=起始行,start_column=起始列,end_row=终止行,end_column=终止列)

取消合并:

sheet.unmerge_cells("A0:D5")
sheet.unmerge_cells(start_row=1,start_column=3,end_row=6,end_column=6)

以上便是excel与python结合的第三部分内容,后续将会持续更新excel,ppt,爬虫,人工智能等相关内容,敬请关注

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!