惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Engineering at Meta
Engineering at Meta
D
DataBreaches.Net
月光博客
月光博客
Recent Announcements
Recent Announcements
Google DeepMind News
Google DeepMind News
U
Unit 42
腾讯CDC
爱范儿
爱范儿
J
Java Code Geeks
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
N
Netflix TechBlog - Medium
B
Blog
Stack Overflow Blog
Stack Overflow Blog
GbyAI
GbyAI
T
The Blog of Author Tim Ferriss
小众软件
小众软件
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
Microsoft Azure Blog
Microsoft Azure Blog
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - DHclly

Aspose.Words 合并单元格的原理、配置方式、代码示例以及常见误区 Gpustack 运行一段时间后出现 Failed to initialize NVML: Unknown Error 解决办法 NVIDIA GPU 计算能力( compute capability,SM version)兼容性查询 Amazon S3 Tools:S3cmd 介绍 wsl 和win主机互相访问 nginx 根路径同时代理 http ws sse 三种请求 在 X86_64(amd64) 平台上的docker支持打包跨平台的镜像(如arm64) 以图搜图功能介绍 docker 容器调试技巧 open ai sdk 的额外请求头说明 x-stainless 大模型常见的概念 创建软连接的几种方式 基于node.js 的 web server 实现 对个人的警醒 jQuery对象与DOM对象之间的转换方法 【BUG】浏览器控制台提示:net::ERR_INVALID_CHUNKED_ENCODING 200 (OK) 的解决思路 实用浏览器脚本 关于Lambda表达式(箭头函数)的get属性访问器和常规的get属性访问器的差异 转换字符串为二进制编码字符串
wps dispimg python 解析实现参考
DHclly · 2025-07-23 · via 博客园 - DHclly

在 wps excel 中,可以把图片嵌入单元格,此时会图片单元格会显示如下内容

=DISPIMG("ID_142D0E21999C4D899C0723FF7FA4A9DD",1)

image

下面是针对这中图片文件的解析实现

参考博客:Python读取wps中的DISPIMG图片格式_wps dispimg-CSDN博客:https://blog.csdn.net/QAZJOU/article/details/139709948

解析出 dispimg_id

简单的字符串替换实现

def save_dispimg_id(self,cell_value):
        img_id=cell_value.replace('=DISPIMG("',"").replace('",1)',"")
        self.wps_dispimg_id_list.append(img_id)
        pass    

解析出 dispimg_id 对应的图片文件流

import zipfile
from lxml import etree

wps_dispimg_map ={}

def get_wps_dispimg_map(excel_file):
    if len(wps_dispimg_map)>0:
        return
    
    xml_content_namespaces = {
        'xdr': 'http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing',
        'a': 'http://schemas.openxmlformats.org/drawingml/2006/main',
        'r':"http://schemas.openxmlformats.org/officeDocument/2006/relationships",
        'etc':"http://www.wps.cn/officeDocument/2017/etCustomData"
    }
    
    ref_xml_content_namespaces = {
        None:"http://schemas.openxmlformats.org/package/2006/relationships",
    }
    # 打开 XLSX 文件
    with zipfile.ZipFile(excel_file, 'r') as zfile:
        xml_content=""
        rel_xml_content=""
        
        # 获取 dispimg_id rId 的映射关系
        with zfile.open('xl/cellimages.xml') as file:
            xml_content = file.read()
            
        # 获取 rId  target 的映射关系
        with zfile.open('xl/_rels/cellimages.xml.rels') as file:
            rel_xml_content = file.read()
            
        xml_content_map={}
        rel_xml_content_map={}
        
        xml_content_root = etree.fromstring(xml_content)
        xdr_pics = xml_content_root.findall(".//xdr:pic",xml_content_namespaces)
        for xdr_pic in xdr_pics:
            dispimg_id = xdr_pic.find('.//xdr:cNvPr', namespaces=xml_content_namespaces).attrib.get('name',None)
            rId = xdr_pic.find('.//a:blip', namespaces=xml_content_namespaces).attrib.get(f'{{{xml_content_namespaces["r"]}}}embed',None)
            if dispimg_id is not None and rId is not None:
                xml_content_map[dispimg_id]=rId
        
        rel_xml_content_root = etree.fromstring(rel_xml_content)
        Relationships=rel_xml_content_root.findall('.//Relationship', namespaces=ref_xml_content_namespaces)
        for Relationship in Relationships:
            rId=Relationship.attrib.get('Id',None)
            Target=Relationship.attrib.get('Target',None)
            if rId is not None and Target is not None:
                rel_xml_content_map[rId]=f"xl/{Target}"
        
        
        for dispimg_id,rId in xml_content_map.items():
            for rId2,Target in rel_xml_content_map.items():
                if rId2 != rId:
                    continue
                if Target is None:
                    continue
                with zfile.open(Target) as img_file:
                    image_binary_data = img_file.read()
                    if image_binary_data is not None and len(image_binary_data)>0:
                        wps_dispimg_map[dispimg_id]=image_binary_data
                        break
                
        return wps_dispimg_map