Created
February 19, 2023 03:48
-
-
Save maintainer64/9dfc2475119d304724cbd558e1d6aa41 to your computer and use it in GitHub Desktop.
Export dataframe pandas object from Google Sheets by link to view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from io import BytesIO | |
import pandas as pd | |
from aiohttp import ClientSession | |
from asyncio import run | |
# https://docs.google.com/spreadsheets/d/1fzRAc7J38i2xLgq3dzJIgnNtcmM3JYbCI7LlmkvIfcc/edit | |
# |----Идентификатор-книги-(spreadsheet_id)----| | |
async def google_sheet_export_dataframe( | |
spreadsheet_id: str, | |
sheet_name: str | |
) -> pd.DataFrame: | |
""" | |
Получить dataframe объект из Google Sheets | |
:param spreadsheet_id: Идентификатор книги (получить из ссылки) | |
:param sheet_name: Наименование листа | |
:return: | |
""" | |
url = f"https://docs.google.com/spreadsheets/d/{spreadsheet_id}" \ | |
f"/gviz/tq?tqx=out:csv&sheet={sheet_name}" | |
async with ClientSession() as client: | |
response = await client.get(url=url, raise_for_status=True) | |
stream = BytesIO(await response.read()) | |
return pd.read_csv(stream) | |
async def main(): | |
pd = await google_sheet_export_dataframe( | |
"1fzRAc7J38i2xLgq3dzJIgnNtcmM3JYbCI7LlmkvIfcc", | |
"Question" | |
) | |
print(pd.head()) | |
run(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment