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?
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.
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.
We can use the pandas module read_excel() function to read the excel file data into a DataFrame object.
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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With