- Saved searches
- Use saved searches to filter your results more quickly
- License
- yoavaviram/python-google-spreadsheet
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Python Google Sheets API: Insert and Update Row
- Managing Google sheet from Python
- Connecting to google drive
- Opening the file and adding data
- Information about the google sheet
- Delete value using clear
- Using Google sheets data as option of Combobox
- Data Transfer from Google sheets to MySQL and vice versa
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
A simple Python wrapper for the Google Spreadsheet API
License
yoavaviram/python-google-spreadsheet
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
A simple Python wrapper for the Google Spreadsheeta API.
- An object oriented interface for Worksheets
- Supports List Feed view of spreadsheet rows, represented as dictionaries
- Compatible with Google App Engine
Before you get started, make sure you have:
List Spreadsheets and Worksheets:
>>> from google_spreadsheet.api import SpreadsheetAPI >>> api = SpreadsheetAPI(GOOGLE_SPREADSHEET_USER, GOOGLE_SPREADSHEET_PASSWORD, GOOGLE_SPREADSHEET_SOURCE) >>> spreadsheets = api.list_spreadsheets() >>> spreadsheets [('MyFirstSpreadsheet', 'tkZQWzwHEjKTWFFCAgw'), ('MySecondSpreadsheet', 't5I-ZPGdXjTrjMefHcg'), ('MyThirdSpreadsheet', 't0heCWhzCmm9Y-GTTM_Q')] >>> worksheets = api.list_worksheets(spreadsheets[0][1]) >>> worksheets [('MyFirstWorksheet', 'od7'), ('MySecondWorksheet', 'od6'), ('MyThirdWorksheet', 'od4')]
Please note that in order to work with a Google Spreadsheet it must be accessible to the user who’s login credentials are provided. The GOOGLE_SPREADSHEET_SOURCE argument is used by Google to identify your application and track API calls.
>>> sheet = spreadsheet.get_worksheet('tkZQWzwHEjKTWFFCAgw', 'od7') >>> rows = sheet.get_rows() >>> len(rows) 18 >>> row_to_update = rows[0] >>> row_to_update['name'] = 'New Name' >>> sheet.update_row(row_to_update) >>> row_to_insert = rows[0] >>> row_to_insert['name'] = 'Another Name' >>> row = sheet.insert_row(row_to_insert) >>> sheet.delete_row(row) >>> sheet.delete_all_rows()
>>> sheet = spreadsheet.get_worksheet('tkZQWzwHEjKTWFFCAgw', 'od7') >>> rows = sheet.get_rows(query='name = "Joe" and height < 175')
>>> sheet = spreadsheet.get_worksheet('tkZQWzwHEjKTWFFCAgw', 'od7') >>> filtered_rows = sheet.get_rows( filter_func=lambda row: row['status'] == "READY")
>>> sheet = spreadsheet.get_worksheet('tkZQWzwHEjKTWFFCAgw', 'od7') >>> rows = sheet.get_rows(order_by='column:age', reverse='true')
For more information about these calls, please consult the Google Spreadsheets API Developer Guide.
To run the test suite please follow these steps:
- Make sure Nose is installed: ( pip install nose )
- Create a local file named: test_settings.py with the following variables set to the relevant values: GOOGLE_SPREADSHEET_USER , GOOGLE_SPREADSHEET_PASSWORD , GOOGLE_SPREADSHEET_SOURCE , GOOGLE_SPREADSHEET_KEY , GOOGLE_WORKSHEET_KEY , COLUMN_NAME , COLUMN_UNIQUE_VALUE
- Run nosetests
Copyright © 2012 Yoav Aviram
About
A simple Python wrapper for the Google Spreadsheet API
Python Google Sheets API: Insert and Update Row
This artile focus on insert a new row at the top (row 2).
For append row at the bottom, refer to Python Google Sheets API: Append New Row .
credentials = . service = build('sheets', 'v4', credentials=credentials)sheet = service.spreadsheets() SPREADSHEET_ID = . SHEET_NAME = . SHEET_ID = . # Insert New Empty Row at A2body = 'requests': [ 'insertDimension': 'range': 'sheetId': SHEET_ID, 'dimension': 'ROWS', 'startIndex': 1, 'endIndex': 2 >, 'inheritFromBefore': True > > ]> # Using sheet.values().batchUpdate will trigger: Invalid JSON payload received. Unknown name "requests": Cannot find field.result = sheet.batchUpdate(spreadsheetId=SPREADSHEET_ID, body=body).execute() body = 'value_input_option': 'USER_ENTERED', # RAW 'data': [ 'range': f"' '!A2:B2", 'values': [["1", "New Row"]]> ]> result = sheet.values().batchUpdate(spreadsheetId=SPREADSHEET_ID, body=body).execute()
❤️ Is this article helpful?
Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.
Do send some 💖 to @d_luaz or share this article.
A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz
Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.
Managing Google sheet from Python
After logging in to your google account, visit this URL
https://console.cloud.google.com/apis/
GO to APIs overview
(Create a new project if not created.)
Click Credentials ( at left side menu )
Create credentials > Service Account
Service Account Details ( enter details )
Select the service account just created.
Visit Keys tab at top
Add Key > Create a New Key ( download the JSON file Save it in your computer and note the path. )
Service account email address (select service account details for edit ) to be given share access to the directory or to the file or directory to work with. Don't forget to enable Google Drive API for your project.
Connecting to google drive
import pygsheets path='G:\\My drive\\testing\\google-sheet\\creds1.json' gc = pygsheets.authorize(service_account_file=path)
Opening the file and adding data
sh = gc.open('pythontest') # Open the google sheet phthontest wks = sh[0] # select the first sheet wks.update_col(2,['ab','cd','ef']) # add list to 2nd column
Information about the google sheet
sh = gc.open('pythontest') # Open the google sheet phthontest print(sh.id) # id of the sheet print(sh.title) # pythontext output as title print(sh.url) # address of the file to open in browser print(sh.updated) # last updated date and time of the file
wk1 = sh[0] # select the first worksheet print('Rows: ',wk1.rows) # Rows : 1000 print('Columns: ',wk1.cols) # Columns: 26
print(wk1.get_value('B2')) # data at cell B2 print(wk1.cell((2,2)).value) # data at row_number=2 col_number=2 ( B2)
print(wk1.get_all_values(),include_tailing_empty=False)
wk1.update_value('A1',5) # Store 5 at A1 wk1.update_value('A2',10) # Store 10 at A2 wk1.update_value('A3','=A1+A2',True) # Add data at A1 and A2 and store at A3
The third parameter parse if False then the data is typed and not evaluated and placed.
Update the parse to True again and change the value at A1. The SUM will be reflected at A3.
wk1.update_value('A1',20) # Update 20 at A1
Delete value using clear
wk1.clear('B3','C5') # clear data from grid B3 to C5
wk1.clear('*','*') # clear all cells
Getting all row values and all column values as List. If we make the include_trailing_empty=True then all blanks after the last value will be included.
print(wk1.get_row(2,include_tailing_empty=False)) #['12', '34', 'ab', 'cd', '55', 'cv'] print(wk1.get_col(3,include_tailing_empty=False)) #['', 'ab', 'Al', '45']
Inserting rows and columns
#insert after row 2 , one row with these values wk1.insert_rows(row =2, number = 1, values =['BB', 40,'CC',60,'DD']) #insert after col 3 ,one column with the values wk1.insert_cols(col =3, number = 1, values =['','kk','tt',34, 40])
Using Google sheets data as option of Combobox
We can collect data from google sheet and add as options for a Combobox by using pygsheets library and google drive API.
Options of Combobox from Google sheet data »
Data Transfer from Google sheets to MySQL and vice versa
By using Pandas DataFrame we can transfer data from MySQL database table to Google sheets and from Google sheets to MySQL.
Data Transfer between Google sheet and MySQL »
Data Transfer between Google sheet and SQLite »
plus2net.com
Python programming Basics ⇩