How do I access (read, write) Google Sheets spreadsheets with Python?

前端 未结 8 819
盖世英雄少女心
盖世英雄少女心 2020-11-29 15:41

I am wondering if you can point me to an example of reading/writing to/from a google doc/spreadsheet using python.

I did look at google docs API here https://develo

8条回答
  •  粉色の甜心
    2020-11-29 16:00

    You could have a look at Sheetfu. The following is an example from the README. It gives a super easy syntax to interact with spreadsheets as if it was a database table.

    from sheetfu import Table
    
    spreadsheet = SpreadsheetApp('path/to/secret.json').open_by_id('')
    data_range = spreadsheet.get_sheet_by_name('people').get_data_range()
    
    table = Table(data_range, backgrounds=True)
    
    for item in table:
        if item.get_field_value('name') == 'foo':
            item.set_field_value('surname', 'bar')              # this set the surname field value
        age = item.get_field_value('age')
        item.set_field_value('age', age + 1)
        item.set_field_background('age', '#ff0000')             # this set the field 'age' to red color
    
    # Every set functions are batched for speed performance.
    # To send the batch update of every set requests you made,
    # you need to commit the table object as follow.
    table.commit()
    

    Disclaimer: I'm the author of this library.

提交回复
热议问题