通过Python提取PowerPoint文本属性



我试图在PowerPoint中提取与文本相关的属性,但得到了奇怪的输出。。。shape.fill的输出与预期不同。我也很想找到其他属性,比如shape.font和形状的位置——这可能吗?

问题:

f = shape.fill
Output: <pptx.dml.fill.FillFormat object at 0x00000215C4D6DD90>

代码:

mylist = []
mylist2 = []
mylist3 = []
mylist4 = []
mylist5 = []
mylist6 = []
mylist7 = []
for eachfile in glob.glob(direct):
s = 1
file = os.path.basename(eachfile)
try:
prs = Presentation(eachfile)
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):            
x = nltk.word_tokenize(shape.text)
t = shape.text
f = shape.fill
print(f)
mylist4.append(file)
mylist5.append(t)
mylist7.append(f)
mylist6.append('Slide: ' + str(s))
#                x = shape.text.split() #looks for words with punctuation included
for word in x:
word = word.lower()
if word in terms:
mylist.append("Slide " + str(s))
mylist2.append(file)
mylist3.append(word)
s = s + 1
except:
pass
#mylist = list(dict.fromkeys(mylist))
d = {'FileName':mylist2,'Slide':mylist, 'Match':mylist3}
d2 = {'FileName':mylist4, 'Slide':mylist6, 'Text':mylist5, 'Color':mylist7}
search = phrases + terms
d3 = {'Text':search}
df = pd.DataFrame(d)
df = df.drop_duplicates()

<pptx.dml.fill.FillFormat object at 0x00000215C4D6DD90>是一个python对象。您需要查找这些类型对象的文档,并使用其属性函数来从中获取信息

我能找到的关于这种类型的对象的唯一文档是这个,尽管它不是";正常的";一个,但只是源代码。可以使用的函数是在FillFormat类内部编写的,从back_color(self, ...)开始

API文档描述了您对任何给定属性的期望。例如,此处:https://python-pptx.readthedocs.io/en/latest/api/dml.html#fillformat-对象您可以了解如何查询Shape.fill返回的FillFormat对象。

在许多情况下,事情比普通情况复杂得多,API将反映这一点。例如,填充有几种:RGB颜色(最常见(、图案(重复位图掩码(、图像(平铺或以各种方式拟合(和"填充";空";填满适应所有这些选项需要你了解比你最初想知道的更多的PowerPoint:(

API的总体文档如下:https://python-pptx.readthedocs.io/en/latest/#api-文件

最新更新