6626070
2997924

PL03-Topic02, gspread

Back to the previous page
List of posts to read before reading this article


Contents


Installation

For linux

$ pip install gspread




For windows

pip install gspread
pip install --upgrade oauth2client

Google Cloud Platform(GCP), API & Services, google driver API and google sheets API




Version Control






Google sheets

Connection to sheets

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = [
'https://spreadsheets.google.com/feeds',
'https://www.googleapis.com/auth/drive',
]

json_file_name = ''
credentials = ServiceAccountCredentials.from_json_keyfile_name(json_file_name, scope)
gc = gspread.authorize(credentials)

spreadsheet_url = ''

# 스프레스시트 문서 가져오기 
doc = gc.open_by_url(spreadsheet_url)
# 시트 선택하기
worksheet = doc.worksheet('')




Read

Cell

cell_data = worksheet.acell('B1').value
print(cell_data)




Row

row_data = worksheet.row_values(1)
print(row_data)




Column

column_data = worksheet.col_values(1)
print(column_data)




Range

# 범위(셀 위치 리스트) 가져오기
range_list = worksheet.range('A1:D2')
print(range_list)

# 범위에서 각 셀 값 가져오기
for cell in range_list:
    print(cell.value)




Write

Cell

worksheet.update_acell('B1', 'b1 updated')




Row

worksheet.insert_row(['new1', 'new2', 'new3', 'new4'], 4)    # specific row
worksheet.append_row(['new1', 'new2', 'new3', 'new4'])       # last row





title3


List of posts followed by this article


Reference


OUTPUT
<details markdown="1">
<summary class='jb-small' style="color:red">OUTPUT</summary>
<hr class='division3_1'>
<hr class='division3_1'>
</details>