如何用Python读取Google Drive中Google Sheets的Excel数据?
Reading Google Sheets Data into Your Python Workflow
Since you’ve moved your Excel data to Google Sheets, here are two reliable methods to adapt your code to pull that cloud-based data—both will integrate seamlessly with your existing pandas/numpy workflow.
Method 1: Using gspread (Recommended for private sheets)
This method uses Google’s official Sheets API, so it’s ideal if you need to keep your sheet private and secure.
Step 1: Set up credentials
- Create a new project in your Google Cloud dashboard.
- Enable the Google Sheets API for this project.
- Generate a service account, then download the JSON credentials file associated with it.
- Share your target Google Sheet with the email address listed in the credentials JSON (grant "Viewer" access at minimum).
Step 2: Install required packages
Run these commands in your terminal:
pip install gspread pandas numpy
Step 3: Update your code to read the sheet
Replace your local Excel reading code with this snippet—your existing data processing logic will work unchanged after this:
import gspread import pandas as pd import numpy as np # Authenticate with your credentials file gc = gspread.service_account(filename='path/to/your/credentials.json') # Open the sheet (use the exact name of your Google Sheet) # For specific worksheets, use .worksheet("Worksheet Name") instead of .sheet1 sheet = gc.open('Your Google Sheet Name').sheet1 # Convert sheet data to a pandas DataFrame (matches your original df structure) df = pd.DataFrame(sheet.get_all_records()) # Your existing code starts here num = np.array([df['NUM']]) canal = np.array([df['CANAL']]) cluster = np.array([df['CLUSTER']]) region = np.array([df['REGION']]) # ... your后续数据处理代码 and visualization code remains exactly the same ...
Method 2: Using pandas with a public sheet link (Simpler for public data)
If you don’t mind making your sheet accessible to anyone with the link, this method skips API setup entirely.
Step 1: Make your sheet public
- Open your Google Sheet, click "Share" → "Get link" → set access to "Anyone with the link" (Viewer permission is sufficient).
- Copy the share URL, and extract the unique sheet ID (the long string between
/d/and/edit).
Step 2: Read directly with pandas
Replace your local pd.read_excel call with this:
import pandas as pd import numpy as np # Replace {SHEET_ID} with your actual sheet ID from the share URL sheet_export_url = "https://docs.google.com/spreadsheets/d/{SHEET_ID}/export?format=xlsx" df = pd.read_excel(sheet_export_url) # Your existing data processing code works exactly as before num = np.array([df['NUM']]) # ... rest of your code ...
Quick Tips
- Both methods produce a
dfDataFrame identical to the one from your local Excel file, so all your loops, dictionaries, and numpy operations will run without modifications. - For multi-worksheet sheets: In
gspread, usegc.open("Sheet Name").worksheet("Specific Tab Name"). In the pandas method, addsheet_name="Tab Name"to thepd.read_excelcall.
内容的提问来源于stack exchange,提问作者Joaquin




