pgsheets : Manipulate Google Sheets Using Python
pgsheets is a Python3 library for interacting with Google Sheets. It makes use of Pandas DataFrames, 2-dimensional structures perfectly suited for data analysis and representing a spreadsheet.
This library can be integrated easily with your existing data to present dashboards, update documents, or provide quick data analysis.
Features
Get / Set all or part of a Google Sheet
Manage authorization with Google API
Retrieve/set formulas or values
Resize spreadsheets
Open up a wealth of Pandas data tools to use on Google Sheets
Installation
Simply install with pip:
$ pip install pgsheets
Usage
Setting up a Google Project
If you haven’t already you will need to create a project in Google’s Developer Console and get your Client ID and Client Secret.
Navigate to the Google Developer Console
Create a project (you will be redirected to the project page)
Click on APIs & Auth
Click on Consent screen and set a Product Name
Now click on credentials.
Click Create new Client ID and select Installed Application > Other
Getting a Token
Using your Google client id and client secret we can get a authorization URL to present to a user:
>>> from pgsheets import Token, Client
>>> c = Client(my_client_id, my_client_secret)
>>> c.getOauthUrl()
'https://accounts.google.com/o/oauth2/auth?...'
By visiting this URL a Google user can consent to your application viewing and modifying their Google sheets. After consenting to this an access code is returned, which we use to get a token:
>>> my_token = c.getRefreshToken(access_code)
>>> type(my_token)
str
>>> t = Token(c, my_token)
You need to save my_token for future use.
Editing a spreadsheet
Create a spreadsheet (and make sure you save it) and copy the url. Now we can access the Spreadsheet:
>>> import pandas as pd
>>> from pgsheets import Spreadsheet
>>> s = Spreadsheet(t, my_url)
>>> s
<Spreadsheet title='test' key='.....'>
>>> s.getTitle()
'test'
>>> s.getWorksheets()
[<Worksheet title='Sheet1' sheet_key='.....'>]
>>> w = s.getWorksheet('Sheet1')
>>> w.getTitle()
'Sheet1'
>>> w.setDataFrame(
pd.DataFrame([['money', 'interest', 'years', 'result'],
['1000', '0.015', '3', '=A2 * (1+B2) ^ C2']]),
copy_columns=False,
copy_index=False,
resize=True)
>>> w.asDataFrame()
interest years result
money
1000 0.015 3 =R[0]C[-3] * (1+R[0]C[-2]) ^ R[0]C[-1]
>>> w.asDataFrame(values=True)
interest years result
money
1000 0.015 3 1045.678375
>>> df = w.asDataFrame()
>>> df['checked'] = "TRUE"
>>> w.setDataFrame(df)
>>> w.asDataFrame()
interest years result checked
money
1000 0.015 3 =R[0]C[-3] * (1+R[0]C[-2]) ^ R[0]C[-1] TRUE
Limitations
The library has only been tested in Python3.4. It will almost certainly not work in Python2.
Currently the following cannot be done with pgsheets:
Create a spreadsheet
Add a workbook to a spreadsheet
Rename a spreadsheet or a workbook
Prevent certain values from changing slightly e.g. ‘True’ becomes ‘TRUE’
Finally the Google API has some limitations. Ideally this code should not cause any changes to a worksheet:
>>> w.setDataFrame(w.asDataFrame())
Unfortunately, there are certain edge cases. For example, with a Formula such as the following
={1, 2} |
which displays across two cells:
1 |
2 |
There is no clear way to know that the cell on the right wasn’t input as a ‘2’ by the user. Thus the above code would cause the following output:
={1, 2} |
2 |
which displays as:
#REF! |
2 |
Metadata
Release files for pgsheets 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pgsheets-0.0.1.tar.gz | 10.7 kB | Without provenance | Details for pgsheets-0.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pgsheets-0.0.1-py3-none-any.whl | Python 3 | none | any | Details for pgsheets-0.0.1-py3-none-any.whl |
Total release size: 22.1 kB
Release files / pgsheets-0.0.1.tar.gz
| Download URL | pgsheets-0.0.1.tar.gz |
|---|---|
| Size | 10.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b834a1b75098f376f7d8fe7a7659515f48d0575383a60fb4a82f69f328e7faa3
|
|
BLAKE2b-256 checksum How to use checksums |
10372f572a74456258db8d4df02fa47a39c9663c3c181e5ed1eed62509447066
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / pgsheets-0.0.1-py3-none-any.whl
| Download URL | pgsheets-0.0.1-py3-none-any.whl |
|---|---|
| Size | 11.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ebb25641ff29858a7eede80511e4ae1b26b7398f67d058593d4c4287126f0e61
|
|
BLAKE2b-256 checksum How to use checksums |
371829e5caf268a33d9ffcb531d401648e688d1932db42e9669ada461007a4c8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for pgsheets 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.