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

wps dispimg python 解析实现参考

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

=DISPIMG("ID_142D0E21999C4D899C0723FF7FA4A9DD",1)

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

参考博客: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 etreewps_dispimg_map ={}def get_wps_dispimg_map(excel_file):if len(wps_dispimg_map)>0:returnxml_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]=rIdrel_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:continueif Target is None:continuewith 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_databreakreturn wps_dispimg_map
http://www.lryc.cn/news/596080.html

相关文章:

  • 二分查找-852.山峰数组的峰顶索引-力扣(LeetCode)
  • 函数——C语言的重要部分
  • React Three Fiber 实现昼夜循环:从光照过渡到日月联动的技术拆解
  • 金山办公WPS项目产品总监陈智新受邀为第十四届中国PMO大会演讲嘉宾
  • 两个android,一个客户端一个服务器端
  • 深入解析 Spark:关键问题与答案汇总
  • 在easyui中如何自定义表格里面的内容
  • Python爬虫实战:研究pymorphy2库相关技术
  • Python爬虫实战:研究PyPLN库相关技术
  • 【文献笔记】ARS: Automatic Routing Solver with Large Language Models
  • PHP获取淘宝拍立淘(以图搜图)API接口操作详解
  • 如何迁移jenkins至另一台服务器
  • 一个基于现代C++智能指针的优雅内存管理解决方案
  • 探索飞算JavaAI:AI赋能Java开发的新范式
  • docker 设置镜像仓库代理
  • 碰一碰发视频源码搭建:支持OEM
  • 初识opencv01——基本api操作
  • 分布式高可用ELK平台搭建及使用保姆级教程指南
  • 大数据之Hive:Hive中week相关的几个函数
  • 分布式数据库中间件ShardingSphere
  • Protobuf学习
  • SysMind:Go 语言驱动的AI系统运维助手
  • 用Python实现神经网络(六)
  • 【计算机网络 篇】TCP基本认识和TCP三次握手相关问题
  • WebSocket心跳机制实现要点
  • 深入浅出理解 TCP 与 UDP:网络传输协议的核心差异与应用
  • 基于SpringBoot+Vue的高校特长互助系统(WebSocket实时聊天、协同过滤算法、ECharts图形化分析)
  • JavaScript,发生异常,try...catch...finally处理,继续向上层调用者传递异常信息
  • zabbix“专家坐诊”第295期问答
  • 服务器无法访问公网的原因及解决方案