我尝试使用python-docx读取一个ms word文档,我没有在API中找到一个函数或方法。如果我想在ms中读取公式,有什么建议吗? 更新: 我尝试用跟随代码打印文档对象的所有文本属性,它根本不能显示任何公式信息。
from docx import Document
from docx.shared import Inches
import collections
def object_walk(obj,stack):
result=set()
id_hex=hex(id(obj))
if id_hex in id_set:
return result
else:
id_set.add(id_hex)
if len(stack)==8 or obj is None:
return result
for attr in (name for name in dir(obj) if not name.startswith('_')):
if attr=="text":
print(getattr(obj,attr),"============",stack)
if isinstance(obj, collections.Iterable):
i=0
for item in obj:
stack.append(attr+str(i))
object_walk(item,stack)
stack.pop()
i+=1
else:
stack.append(attr)
try:
object_walk(getattr(obj,attr),stack)
except:
pass
stack.pop()
document=Document("demo.docx")
id_set=set()
object_walk(document,["root"])