How to consume blob file?

Tony

I am having an issue, I am uploading xlsx files to google storage. When I want to reuse them later on I obtain a blob file.

After that I am loss on how to use the actual xlsx file.

from google.cloud import storage

import openpyxl

client = storage.Client()
new_bucket = client.get_bucket('bucket.appspot.com')

#get blob object:
o = new_bucket.get_blob('old_version.xlsx')

# <Blob: blobstorage.appspot.com, old_version.xlsx, 16372393787851916>

#download the object

bytes_version = o.download_as_bytes()

#load it to openpyxl library
wb = load_workbook(filename = bytes_version ,data_only=True)

InvalidFileException: openpyxl does not support b'.xmlpk\x05\x06\x00\x00\x00\x00:\x00:\x00n\x10\x00\x00\xa6\x06\x01\x00\x00\x00' file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm

End goal would be to download the file as object and read them with openpyxl library (it work with the original file but after the storage on cloud didn't find way to get my xlsx file).

Thank for the help !

edit: adding current code

John Hanley

Your code is reading the Cloud Storage blob into memory:

bytes_version = o.download_as_bytes()

And then trying to load the workbook from memory:

wb = load_workbook(filename = bytes_version ,data_only=True)

However, the load_workbook() method expects a filename or a file-like object. Using a byte string with the file contents is not supported.

openpyxl.reader.excel.load_workbook(filename, read_only=False, keep_vba=False, data_only=False, keep_links=True)

Parameters:

filename (string or a file-like object open in binary mode c.f., zipfile.ZipFile) – the path to open or a file-like object

Documentation

Solution:

Save the Cloud Storage blob to a local disk file first and then specify the file name in the call to load_workbook():

o.download_to_filename('/path/to/file')
wb = load_workbook(filename = '/path/to/file' ,data_only=True)

Note: Replace /path/to/file with a real path on your system and with the .xlsx file extension.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to host a json file on the internet to consume with fetch?

How to convert Blob to File in JavaScript

How to save the blob for media file?

How to upload file to blob container?

How to programmatically consume a file from a Rest API using Spring?

How to pass a file as SOAP request to Mule SOAP client to consume service

How to consume a wsdl file that doesn't exist on web in C#

How to consume static file hosted on Tomcat 9 Server in Docker Image

How to get a File() or Blob() from an URL in javascript?

how to resolve blob error while reading a file?

How to download a file as a blob/byte in Java/Spring

How to find the file a specfic blob is associated with?

How to create File object from Blob?

How to save .xlsx data to file as a blob

How to give a Blob uploaded as FormData a file name?

How to get a file or blob from an object URL?

How to copy an Azure File to an Azure Blob?

How to fix charset of csv file in Blob correctly?

how to retrieve blob file to image in php?

How to convert selected file input images into blob

Angularjs/Restangular, how to name file blob for download?

How to read data from blob file?

How to delete a file in Azure blob storage

How to attach blob file into HTML href="mailto:"

"blob:http: ... " what is it? and how to save it to a file?

How to save a file to a subfolder in an Azure blob container?

How to convert BLOB into PDF file in the Node environment?

How to email the of contents of a file from a Azure blob

How to convert the selected file to blob format?