【发布时间】:2021-05-20 02:59:04
【问题描述】:
我遇到了很多不同的错误,我什至不知道应该提及哪个,但这与凭据无关,因为我已经可以上传文件并且可以读取 txt 文件。现在我想读一个docx。
我在我的 index.html 中创建了一个表单,其中只有一个文本区域来写入文件的确切名称和一个提交输入,该输入将打开一个新窗口,其中包含我的 AWS S3 存储桶中我的 docx 文件的段落数。
我得到的错误是:
AttributeError:“StreamingBody”对象没有“seek”属性
我的代码如下所示:
path = "s3://***bucket/"
bucket_name = "***bucket"
@app.route('/resultfiles', methods=["POST"])
def getdata():
thefilename = request.form['file_name']
if '.docx' in thefilename:
object_key = thefilename
file_object = client.get_object(Bucket=bucket_name, Key=object_key)
body = file_object['Body']
doc = docx.Document(body)
docx_paras = len(doc.paragraphs)
return render_template('resultfiles.html', docx_paras=docx_paras)
【问题讨论】:
-
你可能只需要在你的 body 上调用
read()方法:body = file_object['Body'].read()然后你会得到一个二进制对象,它可能会或可能不会被 docx 库使用。 -
我尝试了 read() 但它不起作用,我也尝试了 read().decode('utf-8') 但仍然没有。你没有别的了吗? .
标签: python amazon-web-services flask amazon-s3 python-docx