I am trying to make the first line bold of a google sheets sheet:
from oauth2client.service_account import ServiceAccountCredentials
import gspread
from df2gspread import df2gspread as d2g
GSHEETS_SCOPES = [
"https://www.googleapis.com/auth/spreadsheets",
"https://www.googleapis.com/auth/drive.file",
"https://www.googleapis.com/auth/drive"
]
# Establishing connection to google sheets
CLIENT_SECRET_GOOGLE_SHEETS = r"folder\client_secret_google_sheets.json"
creds = ServiceAccountCredentials.from_json_keyfile_name(CLIENT_SECRET_GOOGLE_SHEETS, GSHEETS_SCOPES)
client = gspread.authorize(creds)
sheet = client.open("my_sheet")
I am able to write a pandas dataframe to the sheet like so:
d2g.upload(df, sheet.id, 'test', clean=True, credentials=creds, col_names=True, row_names=False)
Then I am trying to change so that the first line would be bold, I've checked this answer and tried this:
DATA = {'requests': [
{'repeatCell': {
'range': {'endRowIndex': 1},
'cell': {'userEnteredFormat': {'textFormat': {'bold': True}}},
'fields': 'userEnteredFormat.textFormat.bold',
}}
]}
sheet.spreadsheets().batchUpdate(spreadsheetId=sheet.id, body=DATA).execute()
But I get :
AttributeError: 'Spreadsheet' object has no attribute 'spreadsheets'
Then I checked this answer and tried:
ws = sheet.worksheet('test')
DATA = {'requests': [
{'repeatCell': {
'range': {'endRowIndex': 1},
'cell': {'userEnteredFormat': {'textFormat': {'bold': True}}},
'fields': 'userEnteredFormat.textFormat.bold',
}}
]}
sheet.spreadsheets().batchUpdate(spreadsheetId=ws.id, body=DATA).execute()
But I am still getting the same error.
I was able to achieve it using:
ws = sheet.worksheet('test')
ws.format('1', {'textFormat': {'bold': True}})
For further formatting I used:
ws.format("1", {
"backgroundColor": {
"red": 1.0,
"green": 1.0,
"blue": 1.0
},
"horizontalAlignment": "CENTER",
"textFormat": {
"fontSize": 12,
"bold": True
}
})
This documentation is really helpful.