read_file.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import openpyxl
  2. from utils.openai_api import get_tokens_length
  3. def read_xlsx_file(data_source, file_name):
  4. workbook = openpyxl.load_workbook(file_name)
  5. sheet = workbook.active
  6. data = []
  7. if data_source == "七麦":
  8. for row in sheet.iter_rows(min_row=2, values_only=True):
  9. data.append({
  10. '发表时间': row[1],
  11. '作者': row[2],
  12. '评级': row[3],
  13. '标题': row[4],
  14. '内容': row[5]
  15. })
  16. return data[2:]
  17. else:
  18. for row in sheet.iter_rows(min_row=1, values_only=True):
  19. data.append({
  20. '发表时间': row[0],
  21. '作者': row[1],
  22. '评级': row[2],
  23. '内容': row[3]
  24. })
  25. return data[2:]
  26. def process_data(data_source,
  27. max_length,
  28. data,
  29. message,
  30. length,
  31. count_now=0,
  32. reply=""):
  33. temp = []
  34. result_message = ""
  35. data = data[count_now:]
  36. while len(data) > 0:
  37. temp_message = message
  38. ele = data.pop(0)
  39. if data_source == "七麦":
  40. temp_string = "({}, {})".format(ele["标题"], ele["内容"])
  41. else:
  42. temp_string = "{}".format(ele["内容"])
  43. temp.append(temp_string)
  44. temp_message = temp_message.format(
  45. reply_before=reply,
  46. comment_num=count_now,
  47. comment_num_start=count_now + 1,
  48. comment_num_end=count_now + len(temp),
  49. all_num=length,
  50. data_string="[{}]".format(", ".join(temp)))
  51. if len(temp_message) > max_length:
  52. break
  53. result_message = temp_message
  54. return result_message, len(temp)