Package: gspread2
Gspread2A wrapper aroundgspreadfor easier usage.
Intended to provide features and syntax similar toOpenPyXL.DISCLAIMER: This library is still under development!Until v1.0.0 is released, assume everything is subject to change.FeaturesCell Formatting such as Fonts, Colors and BordersOpenPyXL functions such asiter_rows()anditer_cols()Values are automatically applied to the sheet when updatedSee Issue #1Roadmap/TODODocumentation (WIP)FormulasFilters and Pivot TablesInstallationRequirements:Python3.6+Install via Pip$ pip install gspread2Basic UsageGetting StartedCreate API credentialsBefore using this library, you must log into Google Developers page and set up a Service Account,
allowing read/write access to your Google Sheets.Head toGoogle Developers Consoleand create a new project (or select the one you have.)Navigate to "API & Services", "Credentials".Click on "CREATE CREDENTIALS", "Service account" and follow through the prompts.
On the last page, create a JSON key and save it locally. You will need to import this into the library to authenticate
to the API.Once you hit "Done", you will see the email address under "Service Accounts", make note of that email.On your Google Sheet, hit "Share" and add the email above.You should now have the credentials and permissions to view and edit your Google Sheet.Load WorkbookTo access a Workbook, you'll need the Google Sheet URL and the credentials file as shown above.
The following code example will return a Workbook object:importgspread2URL='https://docs.google.com/spreadsheets/d/spreadsheetID'CREDENTIALS='path/to/json.file'workbook=gspread2.load_workbook(URL,CREDENTIALS)You can also import the Workbook class and initialise it with the same parameters:fromgspread2.modelsimportWorkbookURL='https://docs.google.com/spreadsheets/d/spreadsheetID'CREDENTIALS='path/to/json.file'workbook=Workbook(URL,CREDENTIALS)Load WorksheetOnce you have a Workbook loaded, you can access worksheets in a number of ways:workbook=gspread2.load_workbook(URL,CREDENTIALS)worksheet=workbook['Sheet 1']ORworkbook=gspread2.load_workbook(URL,CREDENTIALS)worksheet=workbook.get_sheet_by_name('Sheet 1')To get the first sheet (usually the active one):workbook=gspread2.load_workbook(URL,CREDENTIALS)worksheet=workbook.activeSelect CellYou can select cells individually or iterate through columns and rows (other gspread functions are still available such
asworksheet.range())Worksheet.cell()Select an individual cell in the worksheetcell=worksheet.cell(1,2)# 1st row, 2nd columnORcell=worksheet.cell('B1')Worksheet.iter_rows()Returns a list of lists of cells for each row. This function is the same as found on OpenPyXL.Arguments are as follows:start_row (default: First row)end_row (default: Last row)start_col (default: First column)end_col (default: Last column)worksheet.iter_rows(2,4,3,5)The example above will return:[[Cell(C2),Cell(D2),Cell(E2)],[Cell(C3),Cell(D3),Cell(E3)],[Cell(C4),Cell(D4),Cell(E4)],]Worksheet.iter_cols()Returns a list of lists of cells for each column. This function is the same as found on OpenPyXL.Arguments are as follows:start_row (default: First row)end_row (default: Last row)start_col (default: First column)end_col (default: Last column)worksheet.iter_cols(2,4,3,5)The example above will return:[[Cell(C2),Cell(C3),Cell(C4)],[Cell(D2),Cell(D3),Cell(D4)],[Cell(E2),Cell(E3),Cell(E4)],]Edit CellsCell ValuesOnce you have retrieved your desired cells as shown above, you'll want to update the value.
On the originalgspreadlibrary, you have to keep track of all the cells you modified and pass them on toworksheet.update_cells().Ingspread2you do not have to pass on the cells to the function, the library will know what you modified.cell=worksheet.cell(1,1)cell.value='New Value'worksheet.update_cells()Cell Fontsgspreaddoes not provide any formatting features. To apply formatting to a cell, you must initialise a Font instance
(gspread2.styles.Font)fromgspread2.stylesimportFontcell=worksheet.cell(1,1)new_font=Font(name='Arial',size=12,bold=True,italic=True,strikethrough=True,underline=True,color='#FF000000',)cell.font=new_fontworksheet.update_cells()All arguments forFontare optional and default toNone.Arguments withNoneas a value will be untouched on update.Cell FillTo apply a background color to a cell, you must initialisegspread2.styles.colors.Colorand set it tocell.fillfromgspread2.styles.colorsimportColorcell=worksheet.cell(1,1)bg_colorColor('#FF000000')cell.fill=bg_colorworksheet.update_cells()