当前位置: 首页 > news >正文

openpyxl 3.0.7 中文教程

openpyxl 3.0.7 中文教程

python Execl 处理库教程 https://openpyxl-chinese-docs.readthedocs.io/zh-cn/latest/tutorial.html#id2

案例:

生成表格,设置单元格样式

from openpyxl import Workbook
from openpyxl.styles import Font, Border, Side, PatternFill# 创建一个新的工作簿
wb = Workbook()
# 获取活动的工作表
ws = wb.active# 定义表头样式
header_font = Font(color='FFFFFF', bold=True)
header_fill = PatternFill(start_color='0000FF', end_color='0000FF', fill_type='solid')
header_border = Border(left=Side(style='thick'),right=Side(style='thick'),top=Side(style='thick'),bottom=Side(style='thick'))# 定义普通单元格样式
cell_border = Border(left=Side(style='thin'),right=Side(style='thin'),top=Side(style='thin'),bottom=Side(style='thin'))# 设置表头
headers = ['姓名', '年龄', '性别']
ws.append(headers)# 应用表头样式
for cell in ws[1]:cell.font = header_fontcell.fill = header_fillcell.border = header_border# 添加一些数据行
data = [('张三', 25, '男'),('李四', 30, '女'),('王五', 28, '男')
]
for row in data:ws.append(row)# 应用普通单元格样式
for row in ws.iter_rows(min_row=2, max_row=ws.max_row, min_col=1, max_col=ws.max_column):for cell in row:cell.border = cell_border# 保存工作簿
wb.save('example.xlsx')

在这里插入图片描述

生成图表

from openpyxl import Workbook
from openpyxl.chart import BarChart, Reference, Series
from openpyxl.chart.label import DataLabelList# 创建一个新的工作簿
wb = Workbook()
# 获取活动的工作表
ws = wb.active# 添加数据
data = [['月份', '产品A', '产品B', '产品C'],[1, 10, 20, 30],[2, 15, 25, 35],[3, 20, 30, 40],[4, 25, 35, 45],[5, 30, 40, 50],
]# 将数据添加到工作表中
for row in data:ws.append(row)# 创建堆叠条形图
chart = BarChart()
chart.type = "bar"
chart.grouping = "standard"# 设置数据范围
categories = Reference(ws, min_col=1, min_row=2, max_row=6)
values = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=6)# 添加数据系列
chart.add_data(values, titles_from_data=True)
chart.set_categories(categories)# 设置图表标题
chart.title = "堆叠条形图"
chart.x_axis.title = "月份"
chart.y_axis.title = "销售额"# 添加值标签
for series in chart.series:series.dLbls = DataLabelList()series.dLbls.showVal = True# 设置 x 轴和 y 轴刻度
chart.y_axis.scaling.min = 0
chart.y_axis.scaling.max = 60
chart.y_axis.majorUnit = 10  # 设置 y 轴主刻度间隔为 10# 设置 x 轴刻度间隔
chart.x_axis.tickLblSkip = 1  # 每个类别都显示标签# 将图表添加到工作表中
ws.add_chart(chart, "F2")# 保存工作簿
wb.save('stacked_bar_chart_with_ticks.xlsx')

在这里插入图片描述

复制单元格样式

from openpyxl import load_workbook
from openpyxl.styles import Font, Alignment, PatternFill, Border, Sidedef copy_style(from_cell, to_cell):"""复制一个单元格的样式到另一个单元格。:param from_cell: 源单元格:param to_cell: 目标单元格"""if from_cell.has_style:to_cell.font = Font(name=from_cell.font.name,size=from_cell.font.size,bold=from_cell.font.bold,italic=from_cell.font.italic,vertAlign=from_cell.font.vertAlign,underline=from_cell.font.underline,strike=from_cell.font.strike,color=from_cell.font.color)to_cell.fill = PatternFill(fill_type=from_cell.fill.fill_type,start_color=from_cell.fill.start_color,end_color=from_cell.fill.end_color)to_cell.border = Border(left=Side(border_style=from_cell.border.left.style, color=from_cell.border.left.color),right=Side(border_style=from_cell.border.right.style, color=from_cell.border.right.color),top=Side(border_style=from_cell.border.top.style, color=from_cell.border.top.color),bottom=Side(border_style=from_cell.border.bottom.style, color=from_cell.border.bottom.color))to_cell.alignment = Alignment(horizontal=from_cell.alignment.horizontal,vertical=from_cell.alignment.vertical,text_rotation=from_cell.alignment.text_rotation,wrap_text=from_cell.alignment.wrap_text,shrink_to_fit=from_cell.alignment.shrink_to_fit,indent=from_cell.alignment.indent)# 如果需要复制数字格式等其他样式属性,可以在这里添加更多的复制操作# 加载现有的工作簿
wb = load_workbook('example.xlsx')
ws = wb.active# 假设我们要从 A1 单元格复制样式到 G1 单元格
copy_style(ws['A1'], ws['G1'])# 保存工作簿
wb.save('example_copied_styles.xlsx')

在这里插入图片描述

http://www.lryc.cn/news/460767.html

相关文章:

  • idea开发工具使用
  • Android map 获取值
  • Leetcode.13 罗马数字转整数
  • 骨架提取(持续更新)
  • B3622 枚举子集
  • git submodule add用法
  • 计算机毕业设计 基于Python的汽车销售管理系统的设计与实现 Python毕业设计 Python毕业设计选题【附源码+安装调试】
  • 主流的安全测试工具知识点
  • 如何接入实时期货行情数据 - 2024最新教程
  • 通义灵码:AI 编码的强大助力
  • docker 容器启动详解
  • 【spring ai】java 实现RAG检索增强,超快速入门
  • 【Java SE 题库】递归的魅力之--> 汉诺塔问题
  • 《为什么要在三层交换机 VLAN 上配置 IP 地址?》
  • Git的基本使用入门
  • Elasticsearch 入门
  • WebSocket 集成 Spring Boot 的实战指南
  • 无人机集群路径规划:四种优化算法(BKA、CO、PSO、PIO)求解无人机集群路径规划,提供MATLAB代码
  • 第二届 龙信杯 电子数据取证竞赛部分Writeup
  • 偷啥的都有!
  • 【中文注释】planning_scene_tutorial.cpp
  • 【Vue3】 h()函数的用法
  • Flask如何实现前后端分离项目
  • 二维码生成器 1.02.41| 一站式QR码生成器和美化工具
  • 腾讯云视立方·直播 SDK 合规使用指南
  • 在 Spring 中使用 @EhCache 注解作为缓存
  • npm install进度卡在 idealTree:node_global: sill idealTree buildDeps
  • 力扣1031. 两个非重叠子数组的最大和
  • 【Unity实战篇】 接入百度翻译,实现文本自动翻译功能
  • ubuntu samba