在无服务器平台上使用 Python 从 .ppt 文件中提取文本

问题描述 投票:0回答:1

我正在尝试在无服务器平台上使用 python 从 .ppt 和 .pptx 文件中提取文本。目前 .pptx 文件是使用

python-pptx
处理的,但是此软件包不支持 .ppt 文件。我知道通常您可以使用
win32com
打开 PowerPoint 应用程序来转换它们,但我在无服务器平台上没有此功能。我可以使用任何非 API 解决方案从文件中提取文本吗?

python powerpoint python-pptx
1个回答
0
投票

我设法找到了解决方案,它当然不是最佳的,但完成了工作。希望它可以帮助任何遇到同样问题的人。

import olefile
import re

ppt_file_path = 'path/to/file.ppt'
with olefile.OleFileIO(ppt_file_path) as ole:
  bin_data = ole.openstream("PowerPoint Document").read()

def remove_non_printable_characters(input_string):
    printable_regex = re.compile('[^\x20-\x7E]')
    cleaned_string = printable_regex.sub('', input_string)
    return(cleaned_string)
text_data = bin_data.decode('utf-8', errors='replace')
all_text = re.findall('\x00\x00[a-zA-Z0-9].*?\x00\x00', text_data)
all_text = [x.replace('\x00\x00', '') for x in all_text if x != '\x00\x00\x00\x00']
all_text = [x for x in all_text if len(x) <= len(remove_non_printable_characters(x))]
© www.soinside.com 2019 - 2024. All rights reserved.