excel_util.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import pandas as pd
  2. import openpyxl as ox
  3. from itertools import groupby
  4. import os
  5. class ExcelUtil:
  6. # 当前项目路径
  7. dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + r'\resources'
  8. """
  9. 解析excel文件
  10. """
  11. def __init__(self, sheet_name=None, path=None):
  12. if path:
  13. self.path = path
  14. else:
  15. self.path = os.path.join(self.dir_path, 'mvp.xlsx')
  16. if sheet_name:
  17. self.sheet_name = sheet_name
  18. else:
  19. self.sheet_name = '硬标签+行为'
  20. def read_excel_by_pd(self):
  21. df = pd.read_excel(self.path)
  22. data = df.head()
  23. print('获取到的数据{}'.format(data))
  24. def read_excel_by_ox(self):
  25. work_book = ox.load_workbook(self.path, data_only=True)
  26. work_sheet = work_book.get_sheet_by_name(self.sheet_name)
  27. # print('max_row:{}, max_col:{}'.format(work_sheet.max_row, work_sheet.max_column))
  28. return work_sheet
  29. def init_crowd_info(self):
  30. """
  31. 整理不同人群包含的父选序号
  32. :return:
  33. """
  34. rows = [row for row in self.read_excel_by_ox().rows]
  35. crowd_a = []
  36. crowd_b = []
  37. crowd_c = []
  38. crowd_d = []
  39. crowd_e = []
  40. crowd_f = []
  41. for row in rows[2:]:
  42. option = row[4].value
  43. a = row[6].value
  44. if a is not None and a == 1 and option not in crowd_a:
  45. crowd_a.append(option)
  46. b = row[7].value
  47. if b is not None and b == 1 and option not in crowd_b:
  48. crowd_b.append(option)
  49. c = row[8].value
  50. if c is not None and c == 1 and option not in crowd_d:
  51. crowd_c.append(option)
  52. d = row[9].value
  53. if d is not None and d == 1 and option not in crowd_d:
  54. crowd_d.append(option)
  55. e = row[10].value
  56. if e is not None and e == 1 and option not in crowd_e:
  57. crowd_e.append(option)
  58. f = row[11].value
  59. if f is not None and f == 1 and option not in crowd_f:
  60. crowd_f.append(option)
  61. return {'A': crowd_a, 'B': crowd_b, 'C': crowd_c, 'D': crowd_d, 'E': crowd_e, 'F': crowd_f}
  62. def init_mvp_data(self):
  63. """
  64. 获取每个标签包括的父题父选项编号
  65. :return:
  66. """
  67. rows = [row for row in self.read_excel_by_ox().rows][24:]
  68. tag_name = None
  69. datas = []
  70. for row in rows:
  71. tag = row[1].value
  72. values = row[3].value
  73. corr = row[4].value
  74. if tag:
  75. tag_name = tag
  76. if values is not None:
  77. datas.append([tag_name, values, corr])
  78. result = {}
  79. for name, items in groupby(datas, key=lambda obj: obj[0]):
  80. orders = []
  81. for n in items:
  82. orders.append([n[1], n[2]])
  83. result[name] = orders
  84. return result
  85. if __name__ == '__main__':
  86. # eu = ExcelUtil()
  87. # results = eu.init_mvp_data()
  88. # for key in results.keys():
  89. # print(key)
  90. # print(' {}'.format(results[key]))
  91. # print('-'*40)
  92. import os
  93. dir_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  94. print(dir_path)