Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import gspread
- from oauth2client.service_account import ServiceAccountCredentials
- import csv
- import os
- from pathlib import Path
- from datetime import datetime
- # ===== CONFIGURATION =====
- CREDENTIALS_FILE = "credentials.json"
- SPREADSHEET_ID = "Put in here your spreadsheet ID"
- GAME_FOLDER = r"Put in here the output folder"
- CSV_NAME = "name of the output file.csv"
- # =========================
- def main():
- try:
- # 1. Authentication
- scope = ["https://www.googleapis.com/auth/spreadsheets"]
- creds = ServiceAccountCredentials.from_json_keyfile_name(CREDENTIALS_FILE, scope)
- client = gspread.authorize(creds)
- print("✅ Successfully authenticated with Google Sheets API")
- # 2. Access spreadsheet
- spreadsheet = client.open_by_key(SPREADSHEET_ID)
- worksheet = spreadsheet.get_worksheet(0)
- print(f"📄 Accessing: '{spreadsheet.title}' > '{worksheet.title}'")
- # 3. Prepare output
- output_path = Path(GAME_FOLDER) / CSV_NAME
- os.makedirs(GAME_FOLDER, exist_ok=True)
- # 4. Export with proper CSV formatting
- with open(output_path, 'w', newline='', encoding='utf-8') as f:
- writer = csv.writer(f,
- delimiter=',',
- quotechar='"',
- quoting=csv.QUOTE_MINIMAL)
- writer.writerows(worksheet.get_all_values())
- # Success message
- current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
- row_count = len(worksheet.get_all_values())
- print(f"💾 Exported {row_count} rows to: {output_path}")
- print(f"⏱️ {current_time} | Format: Standard CSV (quoted fields)")
- except Exception as e:
- print(f"❌ Error: {str(e)}")
- print("\nTROUBLESHOOTING:")
- print(f"1. Check sharing permissions for: {creds.service_account_email}")
- print("2. Verify worksheet contains data")
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement