Skip to main content
Question

Reading Box files in Python through API without downloading

  • May 22, 2025
  • 1 reply
  • 61 views

Forum|alt.badge.img

I have created a Box App and enabled the necessary permissions for reading and writing files in folders. Is it possible to read the content of .doc/.docx files using Box API without downloading the files using API?

The Box API gives me the content encoded in bytes (via .content()). How can I decode the content to a string? I tried things like .decode(utf-8) but that didn't work, any suggestions?

Any help is very much appreciated!

1 reply

Forum|alt.badge.img

It might be a little late for you but I am facing the same problems.

Here's a raw script of possible solution using Pillow and IO modules from Python. Hope it helps!

    from boxsdk import Client, OAuth2
    import io
    
    # Configure OAuth2 (replace with your credentials)
    oauth = OAuth2(
        client_id='YOUR_CLIENT_ID',
        client_secret='YOUR_CLIENT_SECRET',
        access_token='YOUR_ACCESS_TOKEN',
    )
    client = Client(oauth)
    
    file_id = 'YOUR_FILE_ID'
    
    # Download the file content
    try:
        file_content = client.file(file_id).content()
        print(f"File {file_id} downloaded successfully.")
    except Exception as e:
         print(f"Error downloading file: {e}")
         exit()

    from PIL import Image
   
    # Open the image using Pillow
    try:
        image = Image.open(io.BytesIO(file_content))
        print("Image opened successfully using Pillow.")
    except Exception as e:
        print(f"Error opening image with Pillow: {e}")
        exit()