5

I'm trying to use Python to download an Excel file to my local drive from Box.

Using the boxsdk I was able to authenticate via OAuth2 and successfully get the file id on Box.

However when I use the client.file(file_id).content() function, it just returns a string, and if I use client.file(file_id).get() then it just gives me a boxsdk.object.file.File.

Does anybody know how to write either of these to an Excel file on the local machine? Or a better method of using Python to download an excel file from Box.

(I discovered that boxsdk.object.file.File has an option download_to(writeable_stream here but I have no idea how to use that to create an Excel file and my searches haven't been helpful).

Clerin
  • 116
  • 1
  • 4
  • You are most of the way there. What's left should be easier than what you've already done. If I'm reading the Box docs right, you just need to open a new, writable file in binary mode, and use that file (the Python file object, not the name of the file) as the parameter to `download_to()`. Check out Python's `open()` built-in function. – John Y Jul 25 '16 at 22:56
  • Thanks John Y, that worked! I used the open() function with the arguments 'wb' (for mode) and 1 (for buffer). As long as I had already created a blank excel file in the local drive then it wrote the downloaded data into the excel sheet (including separating it into different tabs). Excel wasn't happy when I tried to open the excel file manually, but as long as I used the close() function first then it was possible to open the excel tabs as dataframes in Python using pandas. Thanks again! – Clerin Jul 26 '16 at 10:34

3 Answers3

1

It is correct that the documentation and source for download_to can be found here and here. I learned about it from this answer myself. You just need to do the following

path = 'anyFileName.xlsx'
with open(path, 'wb') as f:
    client.file(file_id).download_to(f)
tommy.carstensen
  • 8,962
  • 15
  • 65
  • 108
1

This is what I use to read my excel files from box and you can check the content character type and use that when decoding.

from boxsdk import JWTAuth
from boxsdk import Client
import io
import pandas as pd
import chardet # for checking char type

# Configure JWT auth object
sdk = JWTAuth.from_settings_file('config/box_config.json')
# Get auth client
client = Client(sdk)

s = client.file(file.id).content()
print(chardet.detect(open(s, 'rb').read())['encoding']) # gives char type
outputDF = pd.read_excel(io.StringIO(s.decode('utf-8')))
0

You could use python csv library, along with dialect='excel' flag. It works really nice for exporting data to Microsoft Excel. The main idea is to use csv.writer inside a loop for writing each line. Try this and if you can't, post the code here.