Python 解析 Excel 图表(Chart)信息实战:从 xlsx 中提取标题、字体和数据

摘要

在数据分析与报表自动化场景中,Excel 图表往往承载着关键业务信息,但常规库对图表结构与样式的解析能力有限。本文基于 OpenXML 规范,通过将 .xlsx 文件视为 ZIP 压缩包,直接解析 xl/charts/chart*.xml,实现了对 Excel 图表元数据的精准提取。使用 Python 的 urllib、zipfile 与 xml.etree.ElementTree,完整获取了图表标题、系列名称、X/Y 轴数据,以及标题、坐标轴和数据系列的字体与字号信息。实践结果表明,该方法无需依赖 Excel 环境,适用于线上 Excel 文件解析、图表规范校验及报表自动化处理,为 Excel 图表的深度解析与二次利用提供了一种高效可行的技术方案。

一、背景介绍

在实际项目中,我们经常会遇到这样的需求:

  • 线上 Excel 文件(HTTP 地址)

  • 不关心单元格数据,而是需要:

    • 图表标题
    • 系列名称
    • X / Y 轴数据
    • 图表、坐标轴、系列的字体和字号

然而,openpyxl 等库并不能完整解析 Excel 图表的样式和结构

事实上,.xlsx 本质上是一个 ZIP 压缩包,图表信息存储在:

text 复制代码
xl/charts/chart*.xml

只要我们直接解析这个 XML,就能拿到几乎全部图表元数据。


二、整体思路

技术路线

  1. 通过 urllib 下载 Excel 文件

  2. 使用 ZipFile 读取 xlsx 内部结构

  3. 定位 xl/charts/chart1.xml

  4. 使用 xml.etree.ElementTree 解析图表 XML

  5. 按 OpenXML 规范解析:

    • 标题(title)
    • 系列(ser)
    • 分类轴(catAx)
    • 数值轴(valAx)
    • 字体、字号
    • X / Y 轴数据

三、核心代码实现

1️⃣ 完整函数代码

python 复制代码
import xml.etree.ElementTree as ET
from zipfile import ZipFile
import io
import urllib.request

def get_chat_info(direct_link):
    result = {}
    res = {}

    try:
        # 下载 Excel 文件
        file = urllib.request.urlopen(direct_link).read()
        archive = ZipFile(io.BytesIO(file))

        try:
            # 读取图表 XML
            data = archive.read('xl/charts/chart1.xml')
            res['code'] = 200
            res['msg'] = "获取图表信息成功"

            tree = ET.parse(io.BytesIO(data))
            root = tree.getroot()

            # 命名空间
            ns = {
                'c': 'http://schemas.openxmlformats.org/drawingml/2006/chart',
                'a': 'http://schemas.openxmlformats.org/drawingml/2006/main'
            }

            # ================== 图表标题 ==================
            title_element = root.find('.//c:title/c:tx/c:rich', ns)
            if title_element is not None:
                title_text = ""
                for t in title_element.iter('{http://schemas.openxmlformats.org/drawingml/2006/main}t'):
                    title_text += t.text
                result['title'] = title_text

            title_ax_element = root.find('.//c:chart/c:title/c:tx', ns)
            if title_ax_element is not None:
                tx_pr = title_ax_element.find('.//a:defRPr', ns)
                if tx_pr is not None:
                    latin = tx_pr.find('.//a:latin', ns)
                    result['title_font'] = latin.get('typeface') if latin is not None else "no"
                    result['title_size'] = tx_pr.get('sz') or "no"

            # ================== 系列名称 ==================
            ser_elements = root.findall('.//c:chart/c:plotArea/*/c:ser', ns)
            series_name = ""
            for ser in ser_elements:
                v = ser.find('.//c:v', ns)
                if v is not None:
                    series_name = v.text
            result['series_name'] = series_name

            # ================== X / Y 数据 ==================
            x_values, y_values = [], []

            for num_ref in root.findall('.//c:numRef', ns):
                for v in num_ref.findall('.//c:v', ns):
                    y_values.append(v.text)

            for pt in root.findall('.//c:cat/c:strRef/c:strCache/c:pt', ns):
                v = pt.find('.//c:v', ns)
                if v is not None:
                    x_values.append(v.text)

            result['x_values'] = x_values
            result['y_values'] = y_values

            # ================== 数值轴 ==================
            val_ax = root.find('.//c:valAx', ns)
            if val_ax is not None:
                tx_pr = val_ax.find('.//c:txPr/a:p/a:pPr/a:defRPr', ns)
                if tx_pr is not None:
                    latin = tx_pr.find('.//a:latin', ns)
                    result['valAx_font'] = latin.get('typeface') if latin is not None else "no"
                    result['valAx_size'] = tx_pr.get('sz') or "no"

            # ================== 分类轴 ==================
            cat_ax = root.find('.//c:catAx', ns)
            if cat_ax is not None:
                tx_pr = cat_ax.find('.//c:txPr/a:p/a:pPr/a:defRPr', ns)
                if tx_pr is not None:
                    latin = tx_pr.find('.//a:latin', ns)
                    result['catAx_font'] = latin.get('typeface') if latin is not None else "no"
                    result['catAx_size'] = tx_pr.get('sz') or "no"

            # ================== 系列字体 ==================
            ser = root.find('.//c:chart/c:plotArea/*/c:ser', ns)
            if ser is not None:
                tx_pr = ser.find('.//a:defRPr', ns)
                if tx_pr is not None:
                    latin = tx_pr.find('.//a:latin', ns)
                    result['ser_font'] = latin.get('typeface') if latin is not None else "no"
                    result['ser_size'] = tx_pr.get('sz') or "no"

            res['data'] = result

        except:
            res['code'] = 404
            res['msg'] = "未找到图表信息"

    except:
        res['code'] = 500
        res['msg'] = "未获取excel信息"

    return res

四、测试示例

python 复制代码
aa = get_chat_info("http://192.168.31.161:8080/555.xlsx")
print(aa)

五、运行结果示例

json 复制代码
{
  "code": 200,
  "msg": "获取图表信息成功",
  "data": {
    "title": "各季度采购合理性折线图",
    "title_font": "宋体",
    "title_size": "1200",
    "series_name": "采购合理性",
    "x_values": [
      "2018-1", "2018-2", "2018-3", "2018-4",
      "Jan-19", "2019-2", "2019-3", "2019-4",
      "2020-1", "2020-2", "2020-3", "2020-4",
      "2021-1", "2021-2", "2021-3", "2021-4",
      "2022-1", "2022-2", "2022-3", "2022-4"
    ],
    "y_values": [
      "0.99", "0.92", "0.91", "0.37", "0.85",
      "0.97", "0.8", "0.88", "0.67", "0.91",
      "0.76", "0.75", "0.99", "0.95", "0.89",
      "0.83", "0.44", "0.75", "0.94", "0.41"
    ],
    "valAx_font": "宋体",
    "valAx_size": "1000",
    "catAx_font": "宋体",
    "catAx_size": "1000",
    "ser_font": "宋体",
    "ser_size": "1000"
  }
}

六、关键知识点总结

.xlsx 是 ZIP 文件

✅ 图表数据在 xl/charts/chart*.xml

✅ Excel 图表完全遵循 OpenXML 规范

✅ 字体大小单位为 1/100 磅(pt)

  • 1000 = 10pt
  • 1200 = 12pt

七、适用场景

  • 自动化审计 Excel 报表
  • 图表规范校验(字体 / 标题 / 数据完整性)
  • Excel → 图表数据 → Web 可视化
  • 报表 AI / LLM 解析前的数据结构化

文章关联

Python 使用 openpyxl 从 URL 读取 Excel 并获取 Sheet 及单元格样式信息

相关推荐
weixin_462446233 小时前
使用 Python 脚本自动化管理 Docker 容器:启动、修改密码、删除及系统资源监控
python·docker·自动化·系统监控
weixin_462446234 小时前
Python 异步下载文件实战:使用 asyncio + aiohttp 实现高并发下载
python·并发下载
bloglin999994 小时前
anaconda环境中如何生成requirements
python
【赫兹威客】浩哥4 小时前
【赫兹威客】框架模板-后端bat脚本部署教程
python·django
Cestb0n4 小时前
某果app 加密校验算法逆向分析
python·算法·逆向安全
薛定谔的猫喵喵4 小时前
解决 xlrd 2.0+ 版本只支持 xls 格式的问题
python·excel
2501_941805314 小时前
使用Python和Go构建高性能分布式任务调度系统的实践分享
分布式·python·golang
OLOLOadsd1234 小时前
基于YOLO11-C3k2-LFE的岩石颗粒智能检测与识别系统_2
python
_pass_4 小时前
Agent-Chat模式核心流程解析
python