标签:ane 表数据 cal 从表 lin 表格 remove 自动 编号
openpyxl
- 可以读取和写入Excel文件
- 需要单独安装,不包含在Python标准模块中
- 处理Excel数据
- 处理Excel公式
- 处理Excel样式
- 在表格内插入图表
pip install openpyxl
from openpyxl import load_workbook
# 只能打开存在的表格。不能用该方法创建一个新表格文件
# 获取表格文件内的sheet名称
workbook = load_workbook(filename=‘演示.xlsx‘)
print(workbook.sheetnames)
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,按列获取
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
sheet[‘A1‘] = ‘A1‘
workbook.save(filename=‘演示.xlsx‘)
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‘)
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
sheet[‘F1002‘] = ‘AVERAGE(F2:F1001)‘
workbook.save(filename=‘演示.xlsx‘)
from openpyxl.utils import FORMUALE
print(FORMUALE)
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:上方插入一行或者多行
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
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‘)
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
workbook.create_sheet(‘新表格‘)
workbook.save(filename=‘演示.xlsx‘)
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
# 获取sheet实例
sheet = workbook[‘表格2‘]
workbook.remove(sheet)
workbook.save(filename=‘演示.xlsx‘)
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
# 获取sheet实例
sheet = workbook[‘表格1‘]
workbook.copy_worksheet(sheet)
workbook.save(filename=‘演示.xlsx‘)
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook[‘表格1‘]
sheet.title=‘表格2‘
workbook.save(filename=‘演示.xlsx‘)
from openpyxl import Workbook
workbook = Workbook()
sheet = workbook.active
sheet.title=‘表格1‘
workbook.save(filename=‘新表格.xlsx‘)
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook.active
sheet.freeze_panes=‘G2‘
workbook.save(filename=‘演示.xlsx‘)
from openpyxl import load_workbook
workbook = load_workbook(filename=‘演示.xlsx‘)
sheet = workbook[‘表格1‘]
sheet.auto_filter.ref = sheet.dimensions # 整个表格筛选
workbook.save(filename=‘演示.xlsx‘)
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‘)
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)
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‘)
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‘)
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‘)
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‘)
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‘)
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‘)
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‘)
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‘)
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自动化办公视频全集!!!
标签:ane 表数据 cal 从表 lin 表格 remove 自动 编号
原文地址:https://www.cnblogs.com/luckyzs/p/13381717.html