feishu.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. """
  2. feishu python方法
  3. """
  4. import requests
  5. def get_app_token():
  6. """
  7. 获取飞书api token
  8. :return:
  9. """
  10. url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal/"
  11. post_data = {
  12. "app_id": "cli_a51114cf8bf8d00c", # 这里账号密码是发布应用的后台账号及密码
  13. "app_secret": "cNoTAqMpsAm7mPBcpCAXFfvOzCNL27fe",
  14. }
  15. response = requests.request("POST", url=url, data=post_data)
  16. tenant_access_token = response.json()["tenant_access_token"]
  17. return tenant_access_token
  18. class Feishu(object):
  19. """
  20. feishu Python Object
  21. """
  22. def __init__(self, document_token):
  23. self.headers = {"Content-Type": "application/json"}
  24. self.document_token = document_token
  25. def prepend_value(self, sheet_id, ranges, values):
  26. """
  27. 在表的某一个sheet的ranges中插入数据,若该地方存在数据,会自动把已有的数据往下移动,再写如数据
  28. :param sheet_id: 飞书表的唯一ID
  29. :param ranges: 单元格位置的range, 从左上角到右下角, 两边都是闭区间
  30. :param values: 二维数组, 用于填充ranges的空格数组
  31. """
  32. insert_value_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{}/values_prepend".format(
  33. self.document_token
  34. )
  35. # print(get_app_token())
  36. headers = {
  37. "Authorization": "Bearer " + get_app_token(),
  38. "contentType": "application/json; charset=utf-8",
  39. }
  40. body = {
  41. "valueRange": {"range": "{}!{}".format(sheet_id, ranges), "values": values}
  42. }
  43. response = requests.request(
  44. "POST", url=insert_value_url, headers=headers, json=body
  45. )
  46. print(response.json())
  47. def insert_value(self, sheet_id, ranges, values):
  48. """
  49. 插入数据
  50. :param sheet_id:
  51. :param ranges:
  52. :param values:
  53. :return:
  54. """
  55. insert_value_url = (
  56. "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{}/values".format(
  57. self.document_token
  58. )
  59. )
  60. headers = {
  61. "Authorization": "Bearer " + get_app_token(),
  62. "contentType": "application/json; charset=utf-8",
  63. }
  64. body = {
  65. "valueRange": {"range": "{}!{}".format(sheet_id, ranges), "values": values}
  66. }
  67. response = requests.request(
  68. "PUT", url=insert_value_url, headers=headers, json=body
  69. )
  70. print(response.json())
  71. def search_value(self, sheet_id, ab):
  72. """
  73. 搜索
  74. :param sheet_id:
  75. :param ab:
  76. :return:
  77. """
  78. ranges = "{}!{}".format(sheet_id, ab)
  79. # print(ranges)
  80. search_url = "https://open.feishu.cn/open-apis/sheets/v2/spreadsheets/{}/values/{}".format(
  81. self.document_token, ranges
  82. )
  83. headers = {
  84. "Authorization": "Bearer " + get_app_token(),
  85. "contentType": "application/json; charset=utf-8",
  86. }
  87. response = requests.request("GET", url=search_url, headers=headers)
  88. return response.json()