0

I'm attemping to read google spreadsheets with pandas , but i got a trouble.

Here is my code:

import pandas as pd
import win32com.client as win32

sheet_id = '1xkKegpIEZXDH1d2Os-6Kr7v4t46n5y7PQp3jDLskBVA'
df = pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")

print(df)

And here is my log:

  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 216, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 525, in open
    response = meth(req, response)
  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 634, in http_response
    response = self.parent.error(
  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 557, in error
    result = self._call_chain(*args)
  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 749, in http_error_302
 line 634, in http_response
    response = self.parent.error(
  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 563, in error
    return self._call_chain(*args)
  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 496, in _call_chain
    result = func(*args)
  File "C:\Users\dell\AppData\Local\Programs\Python\Python310\lib\urllib\request.py", line 643, in http_error_default
    raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 400: Bad Request
Jim G.
  • 15,141
  • 22
  • 103
  • 166
  • Looks like a duplicate of this [question](https://stackoverflow.com/questions/19611729/getting-google-spreadsheet-csv-into-a-pandas-dataframe). You should also probably review the [Python quickstart guide](https://developers.google.com/sheets/api/quickstart/python#step_1_install_the_google_client_library) that Google offers for the sheets API. – Cold Fish Sep 04 '22 at 04:22

1 Answers1

0

You're getting an HTTP Error:

HTTP Error 400: Bad Request

Are you sure that:

https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv

Where sheet_id = 1xkKegpIEZXDH1d2Os-6Kr7v4t46n5y7PQp3jDLskBVA

Exists?

And even if it does exist, I don't see any evidence of authentication. Did you forget to pass a key in the query string? Consider the code packed in this question. The supplied URL passes a key in the query string.

Please see more information about passing your key here.


Alternatively, instead of:

pd.read_csv(f"https://docs.google.com/spreadsheets/d/{sheet_id}/export?format=csv")

Why not download the spreadsheet to the local machine and read it there instead of over http?

Jim G.
  • 15,141
  • 22
  • 103
  • 166