[尝试从.pptx文件的大目录中提取文本字段,以下脚本非常适合某些Powerpoint演示文稿:
from pptx import Presentation
import glob
f = open("Scraped PPTX Data.txt", "a", encoding='utf-8')
for eachfile in glob.glob("*.pptx"):
prs = Presentation(eachfile)
for slide in prs.slides:
for shape in slide.shapes:
if hasattr(shape, "text"):
f.write(shape.text)
f.close()
然而,在许多其他人(看来是非常大的人)上,我收到了这道巨大的错误墙:
File "C:\Users\GLD-POS3\Desktop\SIGNS\PPT_Scraper.py", line 9, in <module>
prs = Presentation(eachfile)
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\api.py", line 28, in Presentation
presentation_part = Package.open(pptx).main_document_part
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\opc\package.py", line 125, in open
pkg_reader = PackageReader.from_file(pkg_file)
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\opc\pkgreader.py", line 37, in from_file
phys_reader, pkg_srels, content_types
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\opc\pkgreader.py", line 70, in _load_serialized_parts
for partname, blob, srels in part_walker:
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\opc\pkgreader.py", line 106, in _walk_phys_parts
phys_reader, part_srels, visited_partnames
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\opc\pkgreader.py", line 106, in _walk_phys_parts
phys_reader, part_srels, visited_partnames
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\opc\pkgreader.py", line 103, in _walk_phys_parts
blob = phys_reader.blob_for(partname)
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\opc\phys_pkg.py", line 111, in blob_for
return self._zipf.read(pack_uri.membername)
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 1432, in read
return fp.read()
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 885, in read
buf += self._read1(self.MAX_N)
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 989, in _read1
self._update_crc(data)
File "C:\Users\GLD-POS3\AppData\Local\Programs\Python\Python37-32\lib\zipfile.py", line 917, in _update_crc
raise BadZipFile("Bad CRC-32 for file %r" % self.name)
zipfile.BadZipFile: Bad CRC-32 for file 'ppt/media/image170.jpeg'
文件'ppt / media / image170.jpeg'的CRC-32错误
这里要知道的是,pptx
文件基本上只是一个带有花哨名称的zip文件。尝试运行
python -m zipfile -l filename.pptx
那应该列出pptx文件的内容。通常,pptx文件包含一堆xml文件,一堆图像和其他媒体文件。从错误消息中,您可以看到为cyclic redundancy check计算的校验和(CRC =
image170.jpeg
)与zipfile中存储的值不匹配。[AFAICT,无法告诉
ZipFile
忽略CRC错误。事实是,提取文本时,您可能只需要读取zip文件内
ppt/slides/slideN.xml
文件夹中的XML文件。您完全不需要访问图像。尝试使用
zipfile.ZipFile
打开无效文件,然后从ppt/slides
中的XML文件中手动提取文本。