Skip to content Skip to sidebar Skip to footer

How Do I Read An Excel File Directly From Dropbox's Api Using Pandas.read_excel()?

I'm interested in comparing two versions of smallish Excel files stored in Dropbox as separate version. Using the Python SDK, specifically the files_download() method, I'm getting

Solution 1:

The following code will do what you want.

# Imports and initialization of variablesfrom contextlib import closing # this will correctly close the requestimport io
import dropbox
token = "YOURTOKEN"#get token on https://www.dropbox.com/developers/apps/
dbx = dropbox.Dropbox(token)
yourpath = "somefile.xlsx"# This approach is not limited to excel files# Relevant streamerdefstream_dropbox_file(path):
    _,res=dbx.files_download(path)
    with closing(res) as result:
        byte_data=result.content
        return io.BytesIO(byte_data)

# Usage
file_stream=stream_dropbox_file(yourpath)
pd.read_excel(file_stream)

The nice part of this approach is that using io.BytesIO converts the data into a general file-like object. Thus you can also use this to read things like csv's with pd.read_csv().

The code should also work for non-pandas io methods, such as loading images, but I haven't tested that explicitly.

Post a Comment for "How Do I Read An Excel File Directly From Dropbox's Api Using Pandas.read_excel()?"