Patrick Ng
Patrick Ng

Reputation: 76

Download xlsx file with Python

Want to download to local directory. This code works for csv but not xlsx. It writes a file but cannot be opened as Excel.

Any help will be appreciated.

url = 'https://some_url'
resp = requests.get(url)
open('some_filename.xlsx', 'wb').write(resp.content)

Upvotes: 2

Views: 1791

Answers (2)

MariCruzR
MariCruzR

Reputation: 157

You could create a dataframe from the resp data and then use pd.to_excel() function to obtain the xlsx file. This is a tested solution, and it worked for me.

import requests
import pandas as pd
import io

url='https://www.google.com' #as an example
urlData = requests.get(url).content  #Get the content from the url
dataframe = pd.read_csv(io.StringIO(urlData.decode('latin-1'))) 
filename="data.xlsx"
dataframe.to_excel(filename)

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71580

In pandas you could just do:

import pandas as pd
url = 'https://some_url'
df = pd.read_csv(url)

Upvotes: 0

Related Questions