我正在尝试使用以下代码从powerpoint文本框中提取文本:
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
def iter_textable_shapes(shapes):
for shape in shapes:
if shape.has_text_frame:
yield shape
def iter_textframed_shapes(shapes):
"""Generate shape objects in *shapes* that can contain text.
Shape objects are generated in document order (z-order), bottom to top.
"""
for shape in shapes:
# ---recurse on group shapes---
if shape.shape_type == MSO_SHAPE_TYPE.GROUP:
group_shape = shape
for shape in iter_textable_shapes(group_shape.shapes):
yield shape
continue
# ---otherwise, treat shape as a "leaf" shape---
if shape.has_text_frame:
yield shape
prs = Presentation(path_to_my_prs)
for slide in prs.slides:
textable_shapes = list(iter_textframed_shapes(slide.shapes))
ordered_textable_shapes = sorted(
textable_shapes, key=lambda shape: (shape.top, shape.left)
)
for shape in ordered_textable_shapes:
print(shape.text)
但有时在PPT结尾的文本框首先被提取,有时在中间提取,等等。如何修复代码以获得正确顺序的文本(从左到右、从上到下)
目前没有回答
相关问题 更多 >
编程相关推荐