Google Sheets to Postgres DB using gspread and env files.
Project description
g2pg
g2pg takes a Google Sheet, converts it to a DataFrame, which you can then manipulate as you need to.
This DataFrame can then be written to a PostgreSQL database table.
This makes use of a .env file, which after much trial and error I finally got to work with gspread.
Can install using pip install g2pg.
To use:
import g2pg
How does it work
g2pg uses the gspread package to extract data from the Google Sheet.
Follow these directions to get the json credentials file that can be used with gspread https://gspread.readthedocs.io/en/latest/oauth2.html#for-bots-using-service-account
Make sure you share any Gsheets with the email that is specified in your credentials file.
The json credentials need to be stored in a .env file. Don't upload the contents of your .env or json_credentials file to github unless you want everyone to know your secrets.
If using this package you need to have a .env file or enviroment variables set in the following way.
DB_USER= 'username'
DB_PW = 'super_secret_password'
DB_URL = 'db_address'
DB_NAME = 'db_name'
SHEET_TYPE= 'service_account'
SHEET_PROJECT_ID= 'api-project-XXX'
SHEET_PRIVATE_KEY_ID= '2cd … ba4'
SHEET_PRIVATE_KEY="-----BEGIN PRIVATE KEY-----\nNrDyLw … jINQh/9\n-----END PRIVATE KEY-----\n"
SHEET_CLIENT_EMAIL= 'yoursisdifferent@developer.gserviceaccount.com'
SHEET_CLIENT_ID= '1234567890'
SHEET_AUTH_URI= 'https://accounts.google.com/o/oauth2/auth'
SHEET_TOKEN_URI= 'https://oauth2.googleapis.com/token'
SHEET_AUTH_PROVIDER_X509_CERT_URL= 'https://www.googleapis.com/oauth2/v1/certs'
SHEET_CLIENT_X509_CERT_URL= 'https://www.googleapis.com/robot/v1/metadata/bla...bla..bla.iam.gserviceaccount.com'
There are 2 methods available:
def get_df_from_gsheet(gsheet_name,worksheet_name)This returns a datframe from the specified Google Sheet and worksheet.The worksheet name is optional and will default toSheet1. The dataframe will have all columns and rows removed, where there is no data, and the column names will be converted to a database friendly format.def df_to_db(df, table_name,schema, index_name)This writes the specifieddfto thetable_namein the DB that is specified in the.envfile.schemais optional and will default topublicin postgres if not specified.index_nameis the index of your df. If not specified, it will default to index (this is used as the primary key in your DB table).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file g2pg-1.3.0.tar.gz.
File metadata
- Download URL: g2pg-1.3.0.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
90f05e9cff7d1f22d2b7678a1c06818d4d9d1defcf97145502e4dcd5368efcda
|
|
| MD5 |
ecb3c04669804d8f0390f485bf539357
|
|
| BLAKE2b-256 |
4854378e03a5fe8bac695833184c203bb71458aa400039e39d80781da4f6f731
|
File details
Details for the file g2pg-1.3.0-py3-none-any.whl.
File metadata
- Download URL: g2pg-1.3.0-py3-none-any.whl
- Upload date:
- Size: 6.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
564b4540e6dc70c79a642219d6a1264f05dd1e22933e4728d616d857ef170df0
|
|
| MD5 |
0ec426cc73bb2570a5b0df77c720b1eb
|
|
| BLAKE2b-256 |
ecbaa15c701c27e0c540ff2c6b6f0e9c1860e242b63f03ff93c82bbb245f09c0
|