23

I managed to read data from a Google Sheet file using this method:

# ACCES GOOGLE SHEET
googleSheetId = 'myGoogleSheetId'
workSheetName = 'mySheetName'
URL = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format(
    googleSheetId,
    workSheetName
)
df = pd.read_csv(URL)

However, after generating a pd.DataFrame that fetches info from the web using selenium, I need to append that data to the Google Sheet.

Question: Do you know a way to export that DataFrame to Google Sheets?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
  • How do I get the 'your_google_API_credentials.json'? – Lucy_in_the_sky_with_diamonds Sep 25 '20 at 22:44
  • you could also do a `worksheet.clear()` to clear the contents then apply this `worksheet.update([dataframe.columns.values.tolist()] + dataframe.values.tolist())` or the one you posted as a solution to fully update whatever is your `dataframe` **value** but may I ask is there a way to not to a `.clear()`? for like update as it what is the value of the prefered dataframe to be uploaded? – Ice Bear Jan 16 '21 at 14:36
  • Is there a way to access sheets with their names? – Hamza Jul 06 '21 at 18:06
  • Here it is, Hamza: `# Initialize Google Sheets google_sheet_id = Sheet_ID google_sheet_name = 'Sheet_Name' URL = 'https://docs.google.com/spreadsheets/d/{0}/gviz/tq?tqx=out:csv&sheet={1}'.format( google_sheet_id, google_sheet_name ) df = pd.read_csv(URL)` – Mihnea-Octavian Manolache Jul 08 '21 at 03:53

3 Answers3

16

Yes, there is a module called "gspread". Just install it with pip and import it into your script.

Here you can find the documentation: https://gspread.readthedocs.io/en/latest/

In particular their section on Examples of gspread with pandas.

worksheet.update([dataframe.columns.values.tolist()] + dataframe.values.tolist())
Josh Peak
  • 5,898
  • 4
  • 40
  • 52
Daniel
  • 391
  • 1
  • 12
  • 1
    In the documentation it links to another package [`gspread-dataframe`](https://github.com/robin900/gspread-dataframe) and it's as simple as `set_with_dataframe(worksheet, df)` – Alaa M. Jan 17 '23 at 19:23
10

This might be a little late answer to the original author but will be of a help to others. Following is a utility function which can help write any python pandas dataframe to gsheet.

import pygsheets
def write_to_gsheet(service_file_path, spreadsheet_id, sheet_name, data_df):
    """
    this function takes data_df and writes it under spreadsheet_id
    and sheet_name using your credentials under service_file_path
    """
    gc = pygsheets.authorize(service_file=service_file_path)
    sh = gc.open_by_key(spreadsheet_id)
    try:
        sh.add_worksheet(sheet_name)
    except:
        pass
    wks_write = sh.worksheet_by_title(sheet_name)
    wks_write.clear('A1',None,'*')
    wks_write.set_dataframe(data_df, (1,1), encoding='utf-8', fit=True)
    wks_write.frozen_rows = 1

Steps to get service_file_path, spreadsheet_id, sheet_name:

  1. Click Sheets API | Google Developers
  2. Create new project under Dashboard (provide relevant project name and other required information)
  3. Go to Credentials
  4. Click on “Create Credentials” and Choose “Service Account”. Fill in all required information viz. Service account name, id, description et. al.
  5. Go to Step 2 and 3 and Click on “Done”
  6. Click on your service account and Go to “Keys”
  7. Click on “Add Key”, Choose “Create New Key” and Select “Json”. Your Service Json File will be downloaded. Put this under your repo folder and path to this file is your service_file_path.
  8. In that Json, “client_email” key can be found.
  9. Create a new google spreadsheet. Note the url of the spreadsheet.
  10. Provide an Editor access to the spreadsheet to "client_email" (step 8) and Keep this service json file while running your python code.
  11. Note: add json file to .gitignore without fail.
  12. From url (e.g. https://docs.google.com/spreadsheets/d/1E5gTTkuLTs4rhkZAB8vvGMx7MH008HjW7YOjIOvKYJ1/) extract part between /d/ and / (e.g. 1E5gTTkuLTs4rhkZAB8vvGMx7MH008HjW7YOjIOvKYJ1 in this case) which is your spreadsheet_id.
  13. sheet_name is the name of the tab in google spreadsheet. By default it is "Sheet1" (unless you have modified it.
desaiankitb
  • 992
  • 10
  • 17
0

Google Sheets has a nice api you can use from python (see the docs here), which allows you to append single rows or entire batch updates to a Sheet.

Another way of doing it without that API would be to export the data to a csv file using the python csv library, and then you can easily import that csv file into a Google Sheet.

Luke Storry
  • 6,032
  • 1
  • 9
  • 22