Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Pandas write XLSX to S3

11 Jun 2020 » programming

https://stackoverflow.com/questions/53565895/xlsx-pandas-write-to-s3-with-tabs

import io
import boto3
import xlsxwriter
import pandas as pd

bucket = 'your-s3-bucketname'
filepath = 'path/to/your/file.format'
df = pd.DataFrame({'Data': [10, 20, 30, 20, 15, 30, 45]})

with io.BytesIO() as output:
    with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
        df.to_excel(writer, 'sheet_name')
    data = output.getvalue()
s3 = boto3.resource('s3')
s3.Bucket(bucket).put_object(Key=filepath, Body=data)