Python 教學 | pygsheets 自動寫入資料到 Google Sheet
上次教過大家用 python 去讀寫 excel,這次要分享用 python 串接 Google Sheet,對 Google Sheet 做讀寫。
文章會分為三大部分:
- 啟用 Google Sheet API
- python 連接 Google Sheet
- pygsheets 資料讀寫
啟用 Google Sheet API
step 1. 建立 GCP 專案
- 前往 GCP 網頁
- 點選 [選取專案] > [新增專案]
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 填寫專案資訊,點選 [建立]
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
step 2. 啟用 API
- 前往 Google Sheet API,先點選 [選取專案] 選剛剛建立的專案,再點選 [啟用]
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 點選 [憑證] > [建立憑證] > [服務帳戶],填寫相關資料後點選 [完成]
我們會獲得一個服務帳戶,之後要和這個帳戶共享 google sheet。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- 點選剛剛建立的帳戶 > [金鑰] > [新增金鑰] > [建立新的金鑰] > [json] > [建立]
這時候就會下載一個 json 檔案,這個檔案就是連接 Google Sheet 的密碼,記得妥善保管,不要外流。
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
python 連接 Google Sheet
step 1. 建立 Google Sheet
- 到 Google Sheet 建立一個試算表
- 點選 [共用],輸入剛剛服務帳戶的 email
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
step 2. 建立 python 檔
- 安裝套件,終端機輸入
pip install pygsheets
- Python 程式碼
連接 Google Sheet 會需要兩項資訊,一個是剛剛下載的金鑰,另一個是 Google Sheet 的網址(直接複製網址列的網址就是了)
import pygsheets
key = "xxx.json"
gc = pygsheets.authorize(service_file = key)
url = "https://docs.google.com/spreadsheets/xxx"
sheet = gc.open_by_url(url)
pygsheets 資料讀寫
- 開啟工作表
work_sheet = sheet.worksheet_by_title("工作表1")
- 讀取 Google Sheet
取得每一小格(cell)有兩種方法,第一種是利用字串 "A1",第二種是利用 tuple 給行列的值
print(work_sheet.cell("A1").value)
print(work_sheet.cell((1,2)).value)
- 寫入 Google Sheet
cell 的取得方式和剛剛一樣,再利用 = 把值寫入即可
work_sheet.cell("A1").value = "apple"
work_sheet.cell("A1").value = "banana"
這篇文章就先寫到這裡啦,之後再出一篇文章教大家更多的讀寫技巧,歡迎大家先到我的部落格逛逛~