2-自动化操作Excel格式文件
Posted luckyzs
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了2-自动化操作Excel格式文件相关的知识,希望对你有一定的参考价值。
2-自动化操作Excel格式文件
一、模块安装和介绍
openpyxl
- 可以读取和写入Excel文件
- 需要单独安装,不包含在Python标准模块中
- 处理Excel数据
- 处理Excel公式
- 处理Excel样式
- 在表格内插入图表
1、安装
pip install openpyxl
二、打开及读取Excel表格内容
1、Excel基本术语
- 行(row):以数字表示
- 列(column):以字母表示
- 格子:cell
- 表:sheet
2、打开Excel表格并获取表格名称
from openpyxl import load_workbook
# 只能打开存在的表格。不能用该方法创建一个新表格文件
# 获取表格文件内的sheet名称
workbook = load_workbook(filename=‘演示.xlsx‘)
print(workbook.sheetnames)
3、通过sheet名称获取表格
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
print(workbook.sheetnames)
# 一、通过sheet名称获取表格
sheet = workbook[‘Sheet1‘]
sheet = workbook.active # 打开活跃的/唯一的表格
# 二、获取表格尺寸
print(sheet.dimensions) # 输出A1:G1001 代表 左上 A1 , 右下 G1001
# 三、获取单个格子
# 1、获取 A1 格子
cell = sheet[‘A1‘]
# 2、用 行 列 获取格子
cell = sheet.cell(row=1,column=1)
# 四、获取格子属性。value:数据;row:行;column:列;coordinate:坐标
print(cell.value,cell.row,cell.column,cell.coordinate)
# 五、获取多个格子
# 1、
cells = sheet[‘A1:A5‘] # 获取A1到A5之间的格子
cells = sheet[‘A‘] # A列格子
cells = sheet[‘A:C‘] # A-C列格子
cells = sheet[5] # 第5行
cells = sheet[5:6] # 第5-6行
# 2、获取一些列格子。指定行和列的范围,按行获取
# min_row:最低行数 max_row:最高行数
# min_col:最低列数 max_col:最高列数
for row in sheet.iter_rows(min_row=2,max_row=3,min_col=1,max_col=2):
for cell in row:
print(cell)
# iter_cols同理 iter_rows,按列获取
三、向Excel写入数据
1、向某个格子写入内容并保存
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
sheet[‘A1‘] = ‘A1‘
workbook.save(filename=‘演示.xlsx‘)
2、列表数据插入一行
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
data = [
[‘张三‘,1],
[‘李四‘,2],
[‘王五‘,3],
[‘赵六‘,4]
]
for row in data:
sheet.append(row)
workbook.save(filename=‘演示.xlsx‘)
3、插入公式
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
sheet[‘F1002‘] = ‘AVERAGE(F2:F1001)‘
workbook.save(filename=‘演示.xlsx‘)
4、 查看 openpyxl 支持的公式
from openpyxl.utils import FORMUALE
print(FORMUALE)
5、插入(空)列/行
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
# 在第2列左侧插入1列空列
sheet.insert_cols(idx=2) # ids:数字编号
# 在第2列左侧插入3列空列
sheet.insert_cols(idx=2,amount=3)
workbook.save(filename=‘演示.xlsx‘)
# 同理:insert_rows:上方插入一行或者多行
6、删除列
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
# 从ids这一列开始,删除3列,包含ids这一列
sheet.delete_cols(idx=2,amount=3)
workbook.save(filename=‘演示.xlsx‘)
# 同理:delete_rows
7、移动格子
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
# 行:+ 下 ; - 上。
# 列:+ 右 - 左
# 从C1-D4的一组格子,向下移动2行,向左移动2列
sheet.move_range("C1:D4",rows=2,cols=-2)
workbook.save(filename=‘演示.xlsx‘)
8、创建新Sheet
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
workbook.create_sheet(‘新表格‘)
workbook.save(filename=‘演示.xlsx‘)
9、删除Sheet
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
# 获取sheet实例
sheet = workbook[‘表格2‘]
workbook.remove(sheet)
workbook.save(filename=‘演示.xlsx‘)
10、复制一个Sheet
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
# 获取sheet实例
sheet = workbook[‘表格1‘]
workbook.copy_worksheet(sheet)
workbook.save(filename=‘演示.xlsx‘)
11、修改表格名称
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook[‘表格1‘]
sheet.title=‘表格2‘
workbook.save(filename=‘演示.xlsx‘)
12、创建新的Excel表格文件
from openpyxl import Workbook
workbook = Workbook()
sheet = workbook.active
sheet.title=‘表格1‘
workbook.save(filename=‘新表格.xlsx‘)
13、冻结窗格
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
sheet.freeze_panes=‘G2‘
workbook.save(filename=‘演示.xlsx‘)
14、添加筛选
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook[‘表格1‘]
sheet.auto_filter.ref = sheet.dimensions # 整个表格筛选
workbook.save(filename=‘演示.xlsx‘)
四、批量调整字体、样式
1、修改字体样式
from openpyxl.styles import Font
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
cell = sheet[‘A1‘]
font = Font(name=‘宋体‘,size=12,bold=True,italic=True,color=‘FF0000‘)
cell.font = font
workbook.save(filename=‘演示.xlsx‘)
2、获取表格字体样式
from openpyxl.styles import Font
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
cell = sheet[‘A1‘]
font = cell.font
print(font.name.font.size)
3、设置对齐样式
from openpyxl.styles import Font
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
cell = sheet[‘A1‘]
# horizontal:水平对齐
# vertical:垂直对齐
# text_rotation:旋转角度
# warp_text:自动换行
alignment = Alignment(horizontal=‘center‘,vertical=‘center‘,text_rotation=45)
alignment = cell.alignment
workbook.save(filename=‘演示.xlsx‘)
4、设置边框样式
- side
- style:边线样式
- color:边线颜色
- border
- left:左边线样式
- right:右边线样式
- top:上边线样式
- bottom:下边线样式
from openpyxl.styles import Side,Border
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‘)
5、设置填充样式
- PatternFill
- fill_type:填充样式
- fgColor:填充颜色
- GradientFill
- stop:(渐变颜色1,渐变颜色2,,,)
from openpyxl.styles import PatternFill,GradientFill
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
cell_a3 = sheet[‘A3‘]
pattern_fill = PatternFill(fill_type=‘solid‘,fgColor=‘99ccff‘)
cell_a3.fill = pattern_fill
cell_a4 = sheet[‘A4‘]
gradient_fill = GradientFill(stop=(‘FFFFFF‘,‘99ccff‘,‘000000‘))
cell_a4.fill = gradient_fill
workbook.save(filename=‘演示.xlsx‘)
6、行高和列宽
row_dimensions[行编号].height = 行高
column_dimensions[列编号].width = 列宽
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
sheet.row_dimensions[1].height = 50
sheet.column_dimensions[‘B‘].width = 20
workbook.save(filename=‘演示.xlsx‘)
7、合并单元格
merge_cells(待合并的格子编号)
merge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)
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‘)
8、取消合并单元
unmerge_cells(待取消合并的格子编号)
unmerge_cells(start_row=起始行号,start_column=起始列号,end_row=结束行号,end_column=结束列号)
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‘)
五、生成Excel内图表
1、插入图片
from openpyxl import load_workbook
from openpyxl.drawing.image import Image
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
logo = Image("图片.png")
logo.height = 100
logo.width = 100
sheet.add_image(logo,"C1")
workbook.save(filename=‘演示.xlsx‘)
2、插入柱状图
from openpyxl import load_workbook
from openpyxl.chart import BarChart,Reference
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
# 新建一个柱状图
chart = BarChart()
# 从表中获取数据,设定数据引用范围
data = Reference(worksheet=sheet,min_row=1,max_row=7,min_col=2,max_col=3)
# 设置x轴的项目名称
categories = Reference(sheet,min_col=1,min_row=2,max_row=7)
# 给柱状图添加数据。如果data中包含标题,则titles_from_data=True
chart.add_data(data,titles_from_data=True)
# 设置x轴的类目
chart.set_categories(categories)
# 将chart添加到sheet。左上角从E2开始
sheet.add_chart(chart,"E2")
workbook.save(filename=‘演示.xlsx‘)
3、插入条形图
from openpyxl import load_workbook
from openpyxl.chart import LineChart,Reference
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
# 新建一个条形图
chart = LineChart()
# 从表中获取数据,设定数据引用范围
data = Reference(worksheet=sheet,min_row=1,max_row=7,min_col=2,max_col=3)
# 设置x轴的项目名称
categories = Reference(sheet,min_col=1,min_row=2,max_row=7)
# 给条形图添加数据。from_rows数据是横向的。如果data中包含标题,则titles_from_data=True
chart.add_data(data,from_rows=True,titles_from_data=True)
# 设置x轴的类目
chart.set_categories(categories)
# 将chart添加到sheet。左上角从E2开始
sheet.add_chart(chart,"E2")
workbook.save(filename=‘演示.xlsx‘)
该内容学习自 Python自动化办公视频全集!!!
以上是关于2-自动化操作Excel格式文件的主要内容,如果未能解决你的问题,请参考以下文章
Python3 关于excel 文件格式xls之读取写入和追加