Google Sheets provides a convenient way to share data publicly, and sometimes you may want to download this data in CSV format for processing in other applications. In this article, I’ll show you how to construct the URL to download a public Google Spreadsheet as a CSV file, and provide a Python script to automate the process.
Understanding the Google Sheets CSV Export URL
When you have a publicly accessible Google Sheet, you can export it as a CSV by modifying its sharing URL. Here’s the pattern:
Original sharing URL:
|
|
CSV export URL:
|
|
Getting the File ID
The File ID is the long string of characters between /spreadsheets/d/ and /edit in your sheet’s URL. For example:
- URL:
https://docs.google.com/spreadsheets/d/abc123_def456/edit#gid=0 - File ID:
abc123_def456
Python Script to Download CSV
Here’s a Python script that demonstrates how to download a public Google Sheet as a CSV file:
|
|
How to Use the Script
-
Install required package:
1pip install requests -
Run the script:
1python download_sheet.py abc123_def456 my_data.csv
Important Notes
- The Google Sheet must be publicly accessible (set to “Anyone with the link can view”)
- This method works without authentication for publicly available sheets
- For private sheets, you would need to use the Google Sheets API with OAuth 2.0
- The script uses the
requestslibrary, which is a popular HTTP library for Python
Alternative: Using wget or curl
You can also download the CSV directly from the command line:
|
|
Conclusion
Downloading public Google Sheets as CSV files is straightforward once you know the URL pattern. The Python script provided makes it easy to automate this process in your applications. Remember to always respect data ownership and usage terms when working with publicly available data.
If you encounter issues, ensure that:
- The sheet is publicly accessible
- You’re using the correct file ID