简体   繁体   English

如何使用 AWS Lambda Python 读取 AWS S3 存储的 word 文档(.doc 和 .docx)文件内容?

[英]How to read AWS S3 stored word document (.doc and .docx) file content using AWS Lambda Python?

My scenario, I am trying to implement read AWS Stored S3 word document (.doc and .docx) file content from Aws Lambda by using python.我的场景,我正在尝试使用 python 从 Aws Lambda 实现读取 AWS Stored S3 word 文档(.doc 和 .docx)文件内容。 Below code I am using, My problem is I can able to get the file name but I can't able to read content.下面是我使用的代码,我的问题是我可以获取文件名,但无法读取内容。

def lambda_handler(event, context):

    file_contents = s3.Object(‘Bucketname’, 'sample.docx').get()['Body'].read().decode("unicode-escape")

    return {
         'File Name' : obj.key,
         ‘Content’ : file_contents
            }

Response: { "errorMessage": "'unicodeescape' codec can't decode bytes in position 25818-25819: truncated \\xXX escape", "errorType": "UnicodeDecodeError", "stackTrace": [ [ "/var/task/lambda_function.py", 76, "lambda_handler", "file_contents = s3.Object('Bucketname', 'sample.docx').get()['Body'].read().decode(\\"unicode-escape\\")" ] ] }响应:{“errorMessage”:“‘unicodeescape’编解码器无法解码位置 25818-25819 中的字节:截断的 \\xXX 转义”,“errorType”:“UnicodeDecodeError”,“stackTrace”:[ [“/var/task/lambda_function .py", 76, "lambda_handler", "file_contents = s3.Object('Bucketname', 'sample.docx').get()['Body'].read().decode(\\"unicode-escape\\" )" ] ] }

.docx 和 .doc 文件是二进制文件,所以简单的解码是行不通的,也许docx2txt可能会在这里有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM