export_reception_data.py
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import pandas as pd
import json
import os
def main():
print("开始读取本地JSON文件...")
try:
# 读取 JSON 文件
json_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 't3.json')
with open(json_file_path, 'r', encoding='utf-8') as f:
data = json.load(f)
results = data.get('data', [])
print(f"成功获取 {len(results)} 条数据,开始解析处理...")
# 战败客户名单
defeated_customers = [
'连志威', '张蔚然', '彭志峰', '咸宁市易达建材贸易有限公司王坤总',
'文龙', '周鑫', '刘琳','张春华'
]
# 处理并解析结果
processed_data_normal = []
processed_data_defeated = []
for row in results:
customer_name = row['customer_name']
reception_start_time = row['reception_start_time']
recording_url = row['recording_url']
detail_json_str = row['detail_json']
# 基础信息字典
record = {
'客户名称': customer_name,
'接待开始时间': reception_start_time,
'录音链接': recording_url,
}
# 解析 detail_json
if detail_json_str:
try:
# 如果 detail_json_str 是字符串,则解析,如果是列表则直接使用
detail_data = json.loads(detail_json_str) if isinstance(detail_json_str, str) else detail_json_str
if isinstance(detail_data, list):
# 将所有对话拼接成一个字符串,放在同一个单元格
dialogue_lines = []
for item in detail_data:
offset_ms = item.get('offset', 0)
# 将毫秒转换为 HH:MM:SS 格式
total_seconds = int(offset_ms) // 1000
hours = total_seconds // 3600
minutes = (total_seconds % 3600) // 60
seconds = total_seconds % 60
time_str = f"{hours:02d}:{minutes:02d}:{seconds:02d}"
role = item.get('role', '')
content = item.get('content', '')
# 格式化每一行的输出
line = f"[{time_str}] 角色{role}: {content}"
dialogue_lines.append(line)
# 用换行符连接所有的对话记录
record['对话记录'] = "\n".join(dialogue_lines)
else:
record['解析状态'] = "JSON格式不是列表"
except json.JSONDecodeError:
record['解析状态'] = "无法解析为JSON"
record['原始JSON'] = str(detail_json_str)
else:
record['解析状态'] = "为空"
# 分类存储
if customer_name in defeated_customers:
processed_data_defeated.append(record)
else:
processed_data_normal.append(record)
# 转换为 DataFrame
df_normal = pd.DataFrame(processed_data_normal)
df_defeated = pd.DataFrame(processed_data_defeated)
# 导出为 Excel
output_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'reception_data_export.xlsx')
# 写入 Excel 文件的不同 Sheet
with pd.ExcelWriter(output_file, engine='openpyxl') as writer:
if not df_normal.empty:
df_normal.to_excel(writer, sheet_name='成交记录', index=False)
if not df_defeated.empty:
df_defeated.to_excel(writer, sheet_name='战败记录', index=False)
print(f"数据已成功导出到: {output_file}")
except Exception as e:
print(f"发生错误: {e}")
if __name__ == "__main__":
main()