feishu.py 3.2 KB

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