Python读取Word文档中的Excel嵌入文件的方法详解
作者:小小明-代码实体 时间:2022-12-21 14:11:23
今天群友提出一个问题:
给出Word示例如下:
对于这种嵌入文件在Word中都属于ole文件。
下面我们假设需要读取每个嵌入的Excel文件中的python工作表中的A1单元格。
python调用宏实现
首先我们看看如何调用com接口的宏代码实现这个效果,最终完整代码如下:
from win32com import client as win32
import os
word = win32.Dispatch("Word.Application")
word.Visible = True
wdDoc = word.Documents.Open(os.path.abspath("test.docx"))
try:
for shape in wdDoc.InlineShapes:
if shape.Type != 1 or not shape.OLEFormat.ProgID.startswith("Excel.Sheet"):
# 要求形状类型为wdInlineShapeEmbeddedOLEObject,是Excel类型的OLE对象
continue
shape.OLEFormat.Open()
xlApp = win32.GetActiveObject('Excel.Application')
book = xlApp.Workbooks(1)
print([sht.Name for sht in book.Sheets])
print(book.Sheets("python").Range("A1").Value)
book.Close()
finally:
wdDoc.Close()
xlApp.Quit()
word.Quit()
执行结果:
['java', 'forever', 'python']
python
['java', 'forever', 'python']
python hello world
['java', 'forever', 'python']
python
注意:此方法仅支持在已安装办公软件(office或WPS)的windows环境下使用。
python解析ole文件实现
我通过压缩软件看到三个Excel文件其实是以ole的bin文件形式存储:
我们也只需要理解并解析这些文件就可以得到对应的Excel文件,然后直接使用openpyxl或pandas解析。
HY.Li大佬提供了对应的代码:
思路与我的想法不谋而合,不过我不知道用olefile这个现成的库可以解析这些文件,原本还打算自己实现一下。
参考上面的代码,最终我的实现如下:
import olefile
from zipfile import ZipFile
from openpyxl import load_workbook
filename = "test.docx"
with ZipFile(filename, "r") as zip_file:
for name in zip_file.namelist():
if not name.startswith("word/embeddings/"):
continue
with zip_file.open(name) as f:
if not olefile.isOleFile(f):
continue
ole = olefile.OleFileIO(f)
try:
book = load_workbook(ole.openstream("package"))
print(book.sheetnames)
print(book["python"]["A1"].value)
except Exception as e:
print(name, "当前ole对象不是Excel文件:", e)
结果:
['java', 'forever', 'python']
python
['java', 'forever', 'python']
python hello world
['java', 'forever', 'python']
python
相对来说,此方法跨平台,速度快。
来源:https://blog.csdn.net/as604049322/article/details/128308639
标签:Python,读取,Word,Excel
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python实现随机取一个矩阵数组的某几行
2021-10-04 16:45:52
Google Chrome的hack写法以及CSS的支持程度
2008-09-04 12:28:00
九个Python列表生成式高频面试题汇总
2023-06-04 20:09:51
Bowman vs Google-设计与数据彼此相互需要
2009-08-16 16:06:00
pip安装提示Twisted错误问题(Python3.6.4安装Twisted错误)
2021-09-26 04:05:50
![](https://img.aspxhome.com/file/2023/7/128447_0s.png)
rs.getrows的使用方法
2008-04-05 14:01:00
oracle 彻底删除方法
2009-07-02 12:22:00
Django model序列化为json的方法示例
2022-08-25 01:39:03
基于Python OpenCV实现图像的覆盖
2022-09-12 22:05:02
![](https://img.aspxhome.com/file/2023/2/99132_0s.jpg)
Python TCPServer 多线程多客户端通信的实现
2021-07-05 22:33:00
![](https://img.aspxhome.com/file/2023/2/75092_0s.png)
Mootools 1.2教程(1)——MooTools介绍
2008-11-12 13:01:00
![](https://img.aspxhome.com/file/UploadPic/up/9-Mootools-1.2.gif)
Pandas缺失值填充 df.fillna()的实现
2023-11-24 00:01:41
![](https://img.aspxhome.com/file/2023/3/95903_0s.png)
tensorflow获取变量维度信息
2021-01-30 01:21:41
Python里disconnect UDP套接字的方法
2023-03-26 02:48:00
详细了解 MySQL锁机制
2010-08-08 09:04:00
原生JS实现九宫格抽奖
2024-05-02 17:24:19
JavaScript版无组件上传类
2007-10-06 23:16:00
MySQL的索引原理以及查询优化详解
2024-01-13 14:04:38
![](https://img.aspxhome.com/file/2023/4/126244_0s.jpg)
ASP正则表达式技巧
2011-04-10 11:00:00
Django自定义User模型、认证、权限控制的操作
2022-08-03 16:06:17
![](https://img.aspxhome.com/file/2023/6/123646_0s.png)