Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: How to read excel file from Requests response?

I am downloading an excel file as a stream using the requests library.

r = requests.get(my_url, stream=True)

I want to read the data in this excel file, for that I can am trying to use pandas. But I am not sure how to read the file from the response I get. What can I do?

like image 407
hkjhadj1 Avatar asked Sep 28 '19 15:09

hkjhadj1


People also ask

How do you read data from an Excel file using Python?

Reading an Excel FileThe read_excel function of the pandas library is used read the content of an Excel file into the python environment as a pandas DataFrame. The function can read the files from the OS by using proper path to the file. By default, the function will read Sheet1.

How do I read an XLSX file from Excel in Python?

The read_excel() function of pandas is used for reading the xlsx file. This function has used in the script to read the sales. xlsx file. The DataFrame() function has used here to read the content of the xlsx file in the data frame and store the values in the variable named data.

Which method is used to read data from Excel files in pandas?

We can use the pandas module read_excel() function to read the excel file data into a DataFrame object.

Can Python pandas read Excel?

The Quick Answer: Use Pandas read_excel to Read Excel Files To read Excel files in Python's Pandas, use the read_excel() function. You can specify the path to the file and a sheet name to read, as shown below: What is this?


1 Answers

You can use a url in pandas directly to read the excel file without using requests.

import pandas as pd

df = pd.read_excel(my_url)

If it is necessary to retreive the data via requests, then this answer from here (How to download a Excel file from behind a paywall into a pandas dataframe?) may suffice:

Simply wrap the file contents in a BytesIO:

import pandas as pd
import io

with io.BytesIO(r.content) as fh:
    df = pd.io.excel.read_excel(fh, sheetname=0)
like image 131
linamnt Avatar answered Sep 19 '22 16:09

linamnt