我有一个存储在 s3 中的 jpeg 图像。
我的方案是使用 Python 创建一个插入该图像的 excel 文件,并将 excel 文件加载到 s3 中,而不在本地存储 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)
输出:
关于python - 我们可以从 AWS S3 复制图像并将其写入 excel 文件(S3)而不使用 Python 在本地存储数据吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63968893/