Package: sheet-sql-report
sheet-sql-reportInspired by a need for this kind of inter-functionality I've seen in my own work, this is python package to help anyone to make use of their data stored in google sheets and SQL databases.Featuresconnect to google sheets or sql (tested on redshift, but should work for more) as datasourceget_gheet_data(workbook_name, sheet_name, cell=False)grab data from entire sheet or selected cellsql_query(query)run (no result) queries in a databaseget_sql_data(self, query)query the SQL database and return data as pandas dataframedata_to_sql(df, db_schema, db_table, delete_old = True)insert data into a sql databasedata_to_gsheet(self, df_data, workbook_name, sheet_name, starting_cell = 'A2')insert data into a google sheetRequirementsConnecting to Google SheetToSQLYou'll need to make sure to:Enable access to Google Sheets by enabling API access / generating a JSON key that you store in the project where you're working. Followthese instructionsto set up access and download the.jsoncredentialsMake sure that theclient_emailaddress (in the.jsonfile) has been granted edit access to the google sheets you want to access with your programWhen supplying the credentials it should be in format of a string with the path to the.jsoncredentials fileConnecting to SQL databasesYou can only connect to databases that you already have access to, obviously :D. So first need to make sure to get login details from the database administrator. To connect you need the details as a string the formatdialect+driver://username:password@host:port/database(driver not always needed can just supply aspostgresqlfor example)Installation and importTo install simply usepip install sheet-to-sqlThen to use in your project simply import usingimport sheet_sql_reportGetting StartedConfigure connection to datasourcesTo work with thesheet-sql_reportpackage you first need to create aSheetToSQLobject. You can initialise with connection info for Google and SQL - Both, either or neither; although the connection info is needed to run other methods.
TheSheetToSQL()object takes two optional arguments:google_cloud_credentialswhich is a string with a path to your gcloud JSON credentails filesql_connectiona string of your database connection info as described above (dialect+driver://username:password@host:port/database)Updating connection infoTo update the connection info, either if you didn't add at the start or want to switch databases/google logins you can use:update_google_details()update_sql_connection()Grab data from google Sheetget_gheet_data(workbook_name, sheet_name, cell=False)worksheet_name: the name of our Google Sheet that has the data we wantsheet_name: name of the sheet within the workbook we want to accesscell: (OPTIONAL) a sting with the cell to pull the data from e.g. 'A2' otherwise pulls whole sheetGrab data from sqlsql_query(query)query (string)a string containing the query to run on the database w/o returning data. Careful of certain characters that need to be different for Python e.g. % -> %%get_sql_data(query)query (string): a string containing the query to run on the database and return data. Careful of certain characters that need to be different for Python e.g. % -> %%add data to google sheetdata_to_gsheet(df_data, workbook_name, sheet_name, starting_cell = 'A2')df_data (dataframe): the new data to add to the google sheetworkbook_name (string): the name of the workbook to updatesheet_name (string): the name of the worksheet to updatestarting_cell (string): the cell location to input the new data. Defualt at A2add data to sqldata_to_sql(df, db_schema, db_table, delete_old = True)df (dataframe): data to feed to the databasedb_table (string): is name of the target table in the databasedb_schema (string): is name of the target schema in the databasedelete_old (bool): if True then will delete old data and replace with new, otherwise append