如何使用 python 从所有 pptx 文件幻灯片中提取所有图像和文本?
How to extract ALL IMAGES and text from all pptx file slides using python?
我可以从 pptx 文件中读取图像,但不能读取所有图像。我无法提取带有标题或其他文本的幻灯片中显示的图像。这是我的代码,请帮助我。
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
import glob
import os
import codecs
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = '/usr/local/Cellar/tesseract/4.1.1/bin/tesseract'
from pytesseract import image_to_string
n=0
def write_image(shape):
global n
image = shape.image
# get image
image_bytes = image.blob
# assinging file name, e.g. 'image.jpg'
image_filename = fname[:-5]+'{:03d}.{}'.format(n, image.ext)
n += 1
print(image_filename)
os.chdir("directory_path/readpptx/images")
with open(image_filename, 'wb') as f:
f.write(image_bytes)
os.chdir("directory_path/readpptx")
def visitor(shape):
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
write_image(shape)
def iter_picture_shapes(prs1):
for slide in prs1.slides:
for shape in slide.shapes:
visitor(shape)
file = open("directory_path/MyFile.txt","a+")
for each_file in glob.glob("directory_path/*.pptx"):
fname = os.path.basename(each_file)
file.write("-------------------"+fname+"----------------------\n")
prs = Presentation(each_file)
print("---------------"+fname+"-------------------")
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
print(shape.text)
file.write(shape.text+"\n")
iter_picture_shapes(prs)
file.close()
以上代码能够从没有文本或标题的 pptx 幻灯片中提取图像,但无法从有文本或标题的幻灯片中提取图像。
也尝试遍历幻灯片母版和幻灯片版式。如果有 "background" 张图片,那就是它们所在的位置。同样的 for shape in slide.shapes:
机制适用于幻灯片母版和幻灯片布局;它们是多态 Slide
object 的变体,具有相同的 shape-access 语义。
我认为您的问题与幻灯片上的标题或文本没有严格关系。也许那些特定的幻灯片使用包含一些背景图像的布局。如果您打开幻灯片并单击图像,则 select 它(给它边界框)表示它是背景图像并且位于幻灯片版式或可能位于幻灯片母版上。这就是徽标通常如何显示在每张幻灯片上的方式。
如果其中有您感兴趣的文本 and/or 图片,您可能还需要考虑为每张幻灯片迭代备注幻灯片。在幻灯片中找到图片并不常见注释,但 PowerPoint 支持它。
另一种方法是遍历底层 .pptx
包(作为 Zip 存档)并以这种方式提取图像。
我可以从 pptx 文件中读取图像,但不能读取所有图像。我无法提取带有标题或其他文本的幻灯片中显示的图像。这是我的代码,请帮助我。
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE
import glob
import os
import codecs
from PIL import Image
import pytesseract
pytesseract.pytesseract.tesseract_cmd = '/usr/local/Cellar/tesseract/4.1.1/bin/tesseract'
from pytesseract import image_to_string
n=0
def write_image(shape):
global n
image = shape.image
# get image
image_bytes = image.blob
# assinging file name, e.g. 'image.jpg'
image_filename = fname[:-5]+'{:03d}.{}'.format(n, image.ext)
n += 1
print(image_filename)
os.chdir("directory_path/readpptx/images")
with open(image_filename, 'wb') as f:
f.write(image_bytes)
os.chdir("directory_path/readpptx")
def visitor(shape):
if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
write_image(shape)
def iter_picture_shapes(prs1):
for slide in prs1.slides:
for shape in slide.shapes:
visitor(shape)
file = open("directory_path/MyFile.txt","a+")
for each_file in glob.glob("directory_path/*.pptx"):
fname = os.path.basename(each_file)
file.write("-------------------"+fname+"----------------------\n")
prs = Presentation(each_file)
print("---------------"+fname+"-------------------")
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
print(shape.text)
file.write(shape.text+"\n")
iter_picture_shapes(prs)
file.close()
以上代码能够从没有文本或标题的 pptx 幻灯片中提取图像,但无法从有文本或标题的幻灯片中提取图像。
也尝试遍历幻灯片母版和幻灯片版式。如果有 "background" 张图片,那就是它们所在的位置。同样的 for shape in slide.shapes:
机制适用于幻灯片母版和幻灯片布局;它们是多态 Slide
object 的变体,具有相同的 shape-access 语义。
我认为您的问题与幻灯片上的标题或文本没有严格关系。也许那些特定的幻灯片使用包含一些背景图像的布局。如果您打开幻灯片并单击图像,则 select 它(给它边界框)表示它是背景图像并且位于幻灯片版式或可能位于幻灯片母版上。这就是徽标通常如何显示在每张幻灯片上的方式。
如果其中有您感兴趣的文本 and/or 图片,您可能还需要考虑为每张幻灯片迭代备注幻灯片。在幻灯片中找到图片并不常见注释,但 PowerPoint 支持它。
另一种方法是遍历底层 .pptx
包(作为 Zip 存档)并以这种方式提取图像。