从s3读取.pptx文件



我尝试从Amazon S3打开.pptx并使用python-pptx库读取它。这是代码:

from pptx import Presentation
import boto3
s3 = boto3.resource('s3')
obj=s3.Object('bucket','key')
body = obj.get()['Body']
prs=Presentation((body))

它给出"AttributeError: 'StreamingBody'对象没有属性'seek'"这难道不行吗?我该如何解决这个问题?我还尝试在身体上首先使用read()。有没有不下载文件的解决方案?

要从S3加载文件,您应该下载(或使用流策略)并使用io.BytesIO转换您的数据,因为pptx.Presentation可以处理

import io
import boto3
from pptx import Presentation
s3 = boto3.client('s3')
s3_response_object = s3.get_object(Bucket='bucket', Key='file.pptx')
object_content = s3_response_object['Body'].read()
prs = Presentation(io.BytesIO(object_content))

裁判:

Just like what we do with variables, data can be kept as bytes in an in-memory buffer when we use the io module’s Byte IO operations.journaldev

最新更新