如何使用python docx获取word文档中文本的实际样式 [英] How to get actual style of text in word document using python docx

查看:44
本文介绍了如何使用python docx获取word文档中文本的实际样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 python docx 库来读取 MS word 文件 (.docx).当我阅读段落时,我使用字体函数来获取所有样式属性.但有时它会为字体大小属性提供 None .有没有办法获得段落包含的实际字体大小.下面给出了我用来解析段落的示例代码

I am using python docx library to read MS word file(.docx). When i read paragraph i use font function to get all style properties. But sometimes it gives None for font size attribute. Is there any way to get actual font size which paragraph contains. Example code is given below which i am using to parse paragraphs

from docx import Document
d = Document(document_path)
for paragraph in d.paragraphs:
    for run in paragraph.runs:
        print (run.font.size)

推荐答案

简短的回答是否定的.您要求的是 有效 字体大小,而 python-docx 只能看到明确设置的字体大小.当 font.size 报告 None 时,它​​是该段落的默认值,无论是什么,这取决于样式层次结构.

Short answer is no. What you're asking for is effective font size and python-docx can only see an explicitly set font size. When font.size reports None, it is the default for that paragraph, whatever that is, which depends on the style hierarchy.

在许多情况下,它可能是适用段落样式的字体大小,但唯一确定知道的方法是遍历该文本节点的样式层次结构到第一个显式定义.

In many cases it might be the font size of the applicable paragraph style, but the only way to know for sure is to traverse the style hierarchy for that text node to the first explicit definition.

这篇关于如何使用python docx获取word文档中文本的实际样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆