用python实现提取word中的所有图片

你可以使用python-docx库来处理word文件,然后遍历文件中的所有形状,找到图片。

首先,你需要安装python-docx库。在命令行中输入以下命令进行安装:

复制代码

复制代码

|---|---------------------------|
| | pip install python-docx |

然后,你可以使用以下代码提取word文件中的所有图片:

复制代码

python复制代码

|---|---------------------------------------------|
| | from docx import Document |
| | from docx.shared import Inches |
| | |
| | def extract_images(filename): |
| | # 加载word文件 |
| | doc = Document(filename) |
| | |
| | # 存储图片路径的列表 |
| | image_paths = [] |
| | |
| | # 遍历文件中的所有形状 |
| | for shape in doc.shapes: |
| | # 如果形状是图片,则添加到列表中 |
| | if shape.has_image: |
| | image_paths.append(shape.image.filename) |
| | |
| | return image_paths |

这个函数会返回一个列表,其中包含word文件中所有图片的路径。

请注意,这个函数只能提取word文件中嵌入的图片,无法提取链接的图片。如果你需要提取链接的图片,你可能需要使用更复杂的工具或库,如python-docx的高级版python-docx-image-to-data。

相关推荐
曲幽9 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户83562907805114 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon15 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly15 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程15 小时前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly17 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
明月_清风1 天前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风1 天前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
ZhengEnCi2 天前
08c. 检索算法与策略-混合检索
后端·python·算法
明月_清风2 天前
Python 内存手术刀:sys.getrefcount 与引用计数的生死时速
后端·python