import gspread
from oauth2client.service_account import ServiceAccountCredentials
# Define the scope and credentials to access Google Sheets API
scope = ['https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
# Authenticate and connect to the Google Sheets API
client = gspread.authorize(credentials)
# Open the Google Sheets document by its title
document_title = 'Your Document Title'
sheet = client.open(document_title).sheet1
# Range to count non-blank cells within (example: A1:C10)
range_to_count = 'A1:C10'
# Get all values within the range
all_values = sheet.range(range_to_count)
# Count the number of non-blank cells
non_blank_count = sum(1 for cell in all_values if cell.value != '')
print("Number of non-blank cells:", non_blank_count)