Search code examples
python-3.xgoogle-sheetsgoogle-sheets-apipygsheets

How to have bolded headers, but non-bolded cells - filled with worksheet.append_table of pygsheets==2.0.1


The python3 (pygsheets 2.0.1) script below will bold all the cells starting at A2.
Is there an easy way (i.e., in one command) to ask for all these cells not to be bolded?

Code:

import boto3, botocore
import datetime
import json
import pygsheets

currentDT = str(datetime.datetime.now())

def create_spreadsheet(outh_file, spreadsheet_name = "jSonar AWS usage"):
    client = pygsheets.authorize(outh_file=outh_file, outh_nonlocal=True)
    spread_sheet = client.create(spreadsheet_name)
    return spread_sheet


def get_regions():
    region = "us-west-1"
    regions = dict()

    ec2 = boto3.client("ec2", region_name=region)
    ec2_responses = ec2.describe_regions()

    ssm_client = boto3.client('ssm', region_name=region)
    for resp in ec2_responses['Regions']:
        region_id = resp['RegionName']
        tmp = '/aws/service/global-infrastructure/regions/%s/longName' % region_id
        ssm_response = ssm_client.get_parameter(Name = tmp)
        region_name = ssm_response['Parameter']['Value'] 
        regions[region_id] = region_name
    return(regions)


def rds_worksheet_creation(spread_sheet, regions, spreadsheet_index):
    worksheet = spread_sheet.add_worksheet("RDS", rows=100, cols=26, src_tuple=None, src_worksheet=None, index=spreadsheet_index)
    worksheet.cell('A1').set_text_format('bold', True).value = 'DBInstanceIdentifier'
    worksheet.cell('B1').set_text_format('bold', True).value = 'MasterUsername'
    worksheet.cell('C1').set_text_format('bold', True).value = 'Region'
    worksheet.cell('D1').set_text_format('bold', False).value = 'Sent Query to (Name)'
    worksheet.cell('E1').set_text_format('bold', False).value = 'Sent Query to (email)'
    worksheet.cell('F1').set_text_format('bold', False).value = 'WorksheetCreated: %s' % currentDT
    cells_data = list()
    for region, region_h in sorted(regions.items()):
        client = boto3.client('rds', region_name=region)
        clnt = boto3.client('ssm', region_name=region)
        db_instances = client.describe_db_instances()
        for instance in db_instances['DBInstances']:
            MasterUsername = instance['MasterUsername']
            DBInstanceIdentifier = instance['DBInstanceIdentifier']
            cells_data.append([DBInstanceIdentifier, MasterUsername, region_h])
    worksheet.append_table(cells_data, start='A2')


if __name__ == "__main__":
    spread_sheet = create_spreadsheet(spreadsheet_name = "jSonar AWS usage",
                                                            outh_file = '/home/qa/.aws/client_secret.json')
    regions = get_regions()
    rds_worksheet_creation(spread_sheet, regions, 0)
    spread_sheet.share("[email protected]")

Output:

spreadsheet screenshot


Solution

  • If i understand correctly you want to un-bold multiple cells in single command.

    To set format to a range of cells create a Datarange and use apply_format.

    model_cell = Cell('A1')
    model_cell.set_text_format('bold', False)
    Datarange('A1','A10', worksheet=wks).apply_format(model_cell)
    

    docs