我尝试从 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'”。这不应该起作用吗?我怎样才能解决这个问题?我还尝试首先在 body 上使用 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))
ref:
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.
期刊开发 https://www.journaldev.com/19178/python-io-bytesio-stringio
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)