[資料庫筆記] Python 串接 GoogleSheet 新增、讀取、更新和刪除

python_pygsheet

此次整理了 pygsheets – Google Spreadsheets Python API 的官方文件,了解如何用 Python 串接 Google Sheet 來當作資料庫使用,從 Pygsheets 安裝、Python連結 Google Sheet API、到基礎資料庫的 CRUD 應用,本篇描述 Python 做 GoogleSheet 的新增欄位 Create、讀取資料 Read、更新數據 Update 和刪除Delete,提供大家參考。

ㄧ. Python 連結 Google Sheet API

1. 首先建立 Google Cloud Platform 帳號,選擇 Google Sheet API 啟用

GoogleSheet_API
GoogleSheet_API

2. 點選建立憑證 > 服務帳戶,並將金鑰 json 檔儲存於桌面。

建立憑證 GCP

3. 在 Google Sheet 裡面,授權給剛剛在 GCP 申請的服務帳號 ID

googlesheet
googlesheet

以上就完成基礎設定了~

二. Python 新增 GoogleSheet 資料

安裝 pygsheets 套件:

初始設定 import 這次會使用到的 pygsheets 外,gc 這邊是告訴 Python 我們的授權金鑰 json 放置的位子。

1.利用 Python 開啟 GoogleSheet

2.查看此 GoogleSheet 內 Sheet 清單

3.選取要 Sheet 清單

三. Python 讀取 GoogleSheet 資料

Python 做 GoogleSheet 讀取資料的部分,除了匯出成 CSV 處理外,也可以單獨用 cell 讀取 value 或是欄位。

四. Python 更新 GoogleSheet 資料

Python 做 GoogleSheet 的修改資料,利用 update_cell 語法可以修改單獨欄位的值,update_cells 可以修改多個欄位。

 五. Python 刪除 GoogleSheet 資料

Python 做 GoogleSheet 利用 clear 可清除所有值,del_worksheet 可刪除Sheet。

 六. Python 串接 GoogleSheet 的其他應用

set_dataframe :可以匯入 Dataframe 資料
share :可以授權權限
remove_permissions: 移除權限語法

延伸閱讀:

那 [資料庫筆記] Python 串接 GoogleSheet 新增、讀取、更新和刪除 就到這邊感謝收看,如文章內容有誤請不吝指正!