繁体   English   中英

我们可以从 AWS S3 复制图像并将其写入 excel 文件 (S3) 而不使用 Python 将数据存储在本地吗?

[英]Can we copy image from AWS S3 and write it into excel file (S3) without storing the data locally using Python?

我有一个存储在 s3 中的 jpeg 图像。 我的方案是创建一个插入该图像的 excel 文件,并将该 excel 文件加载到 s3 中,而不是使用 Python 在本地存储该 excel 文件。

您可以使用 xlsxwriter 包来完成。

import xlsxwriter
import pandas as pd
import boto3
from io import BytesIO

boto_object = boto3.resource('s3',region_name='us-west-2', aws_access_key_id=AWS_SERVER_PUBLIC_KEY, aws_secret_access_key=AWS_SERVER_SECRET_KEY)

bucket_object = boto_object.Bucket(bucket_name=XXXX)

img_path = "images/data/sample_image.jpeg" # can be PNG, JPG or BMP

with BytesIO() as output:
    with pd.ExcelWriter(output, engine='xlsxwriter') as writer:

        pandas_dataframe.to_excel(writer, sheet_name='sample_sheetname') #creating a sheet first
        image_obj = bucket_object.Object(img_path)
        response = image_obj.get()
        image_data = BytesIO(response['Body'].read())
        writer.sheets['sample_sheetname'].insert_image('A1',img_path, options = {'x_scale': 0.5,
                                                                   'y_scale': 0.5,
                                                                   'x_offset': 80,
                                                                   'y_offset': 10,'image_data':image_data}) #adding data to existing writer object
    data = output.getvalue()

bucket_object.put_object(Key='<path>/sample_output.xlsx', Body=data)

输出: 输出图像

暂无
暂无

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

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