Feishu Development Study Notes (3) - Using python to develop and debug cloud documents and spreadsheets

Feishu Development Study Notes (3) - Using python to develop and debug cloud documents and spreadsheets

1. Establish Python Feishu development environment

First, enter the API debugging platform under the open platform
Feishu open platform:https://open.feishu.cn/app? lang=zh-CN
Take obtaining the file list under "My Space" as an example, and establish a Python Feishu development environment by obtaining the sample code provided by the Feishu API debugging console.
Insert image description here
Below you can switch to the developed example code:
Insert image description here

import requests

url = "https://open.feishu.cn/open-apis/drive/v1/files?direction=DESC&order_by=EditedTime"
payload = ''

headers = {
    
    
  'Authorization': 'Bearer u-fMw0FpV'
}

response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)

Note: In the code here, there is password information in the headers, namely ‘Authorization’: ‘Bearer u-fMw0FpV’
This is generated by the system and is required information.
There are three methods: python SDK, Python-Requests, and Python-http.client, which use three different python libraries for development. In theory, the SDK should be the most powerful, but in practice it is not. unknown.
Requests is a commonly used library. Let’s use the sample code of this library here.
Copy this code into Python and development can begin.
Insert image description here
OK, the operation was successful, a good start.
Insert image description here

2. Design of Python Feishu development architecture

2.1 Auxiliary tools to prepare json

json is the format for returning data strings. It can be easily read through json class tools and imported into json tools.

import json

json_Data=json.loads(response.text)
# 打印返回体信息
print(json_Data)
# 打印电子表格信息
print(json_Data['data']['files'][1])

returned messages

{
    
    'created_time': '16991126', 'modified_time': '16991138', 'name': '测试表格', 'owner_id': 'ou_3bd053e263b2734a2cc', 'parent_token': 'nodcnEByngMucMb', 'token': 'SVT0sVnhh', 'type': 'sheet', 'url': 'https://test-cptojg6atdfe.feishu.cn/sheets/SVT0sRnhh'}

2.2 Use Feishu API to query worksheets in a spreadsheet

The same method as the previous article, use Feishu API "Get Worksheet" to obtain the key information of the worksheet in the spreadsheet
Insert image description here

#API范例
#url = "https://open.feishu.cn/open-apis/sheets/v3/spreadsheets/SVTnhh/sheets/query"
user_access_token="u-dYWo5tGit1Bkg4P9h0FoV"

url="https://open.feishu.cn/open-apis/sheets/v3/spreadsheets/" + sheet_token + "/sheets/query"
Authorization_value="Bearer "+user_access_token
payload = ''
headers = {
    
    'Authorization': Authorization_value}

response = requests.request("GET", url, headers=headers, data=payload)
json_Data=json.loads(response.text)
# 打印返回体信息
print(json_Data)
# 将工作表id用数组存起来
sheet_ids= [ sheet_info['sheet_id']  for sheet_info in json_Data['data']['sheets']]  
sheet_titles= [ sheet_info['title']  for sheet_info in json_Data['data']['sheets']]  
print(sheet_ids)
print(sheet_titles)

sheet_ids gets the worksheet id in the spreadsheet, and sheet_titles gets the worksheet title in the spreadsheet.

2.3 Use Feishu API to query cell information in a worksheet

Feishu server-side API is an interface used to read the value of a single range of the table based on spreadsheetToken and range. The return data is limited to 10M
Insert image description here
Since the API debugging platform is not supported, only Write query strings following other API formats

#API范例
# 官方API格式
#url = https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/:spreadsheetToken/values/:range
# 示例API格式
#url ='https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/shtcngNygNfuqhxTBf588jwgWbJ/values/Q7PlXT!A3:D200?valueRenderOption=ToString&dateTimeRenderOption=FormattedString' 
#headers= 'Authorization: Bearer t-ce3540c5f02ac074d64fa90fa49621c0'

user_access_token="u-dYWo5tGKmTTfL3m290w0l0Mw0FoV"
sheet0_id=sheet_ids[0]
sheet0_range="A1:B2"
url="https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" + sheet_token + "/values/"+sheet0_id+"!"+sheet0_range+'?valueRenderOption=ToString&dateTimeRenderOption=FormattedString' 
Authorization_value="Bearer "+user_access_token
payload = ''
headers = {
    
    'Authorization': Authorization_value}

response = requests.request("GET", url, headers=headers, data=payload)
json_Data=json.loads(response.text)
# 打印返回体信息
# print(json_Data)
sheet0_values= json_Data['data']['valueRange']['values']
print(sheet0_values)

returned messages

[['A1', '测试1'], ['A2', '测试2']]

Insert image description here
As you can see, the read was successful

2.4 Use Feishu API to write information to a worksheet cell

After the above reading operation is successful, writing should be similar and not difficult.
Below is the API for writing data. It does not support the debugging toolbench, but there is also reference code.
Insert image description here
spreadsheetToken and range are two required parameters
Insert image description here
The following is the code for modifying cell values ​​in Python after research:

#API范例
# 官方API格式
#url = https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/:spreadsheetToken/values
# 示例API格式
#url = PUT 'https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/shtcnWbJ/values'
# headers= {'Authorization: Bearer t-ce3540c5f0c074530';'Content-Type: application/json'}
# valueRange={"range": "Q7PlXT!A1:B2","values": [[ "Hello", 1],[ "World", 1]]}

sheet0_modi_range=sheet0_id+"!"+"A3:B4"
#print(sheet0_modi_range)
url="https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/" + sheet_token + "/values" 
#print(url)
Authorization_value="Bearer "+user_access_token
#print(Authorization_value)
headers = {
    
    'Authorization': Authorization_value,'Content-Type':"application/json; charset=utf-8"}
pay_load = json.dumps({
    
    'valueRange':{
    
    'range': sheet0_modi_range,'values': [["Hello",11],["World",12]]}})
#print(pay_load)
response = requests.request("PUT", url, headers=headers, data=pay_load)
json_Data=json.loads(response.text)

# 打印返回体信息
print(json_Data)

Return results

{
    
    'code': 0, 'data': {
    
    'revision': 10, 'spreadsheetToken': 'SVT0sanhh', 'updatedCells': 4, 'updatedColumns': 2, 'updatedRange': '885d89!A3:B4', 'updatedRows': 2}, 'msg': 'success'}

You can see that msg is success, and the cell value is modified successfully
This API is a bit difficult, because its type is not the general "GET" or "POST", but "PUT"
The key point is that the pay_load string cannot be a null value like other cases, but the modified cell address and value must be written in advance
Make a json object of valueRange in advance. The content is the range address, type string and values. The type is list
This json object needs to be passed in parameters and needs to be converted using json.dumps(). json string, this is the most critical step. Because I didn’t understand the knowledge in advance,
pondered for an hour. Finally, it was important to understand why the parameter type was wrong and the modification was successful.
In the picture below, the modified results are displayed.
Insert image description here
At this point, using the python operation API to obtain the worksheet in the spreadsheet and modifying the value of a certain cell has been successful.

Guess you like

Origin blog.csdn.net/qq_43662503/article/details/134226709