script_runner.py
4.15 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Python脚本执行控制台工具
提供菜单界面,方便选择和执行项目中的Python脚本
"""
import os
import sys
import subprocess
from pathlib import Path
# 脚本配置
SCRIPTS = [
{
'id': '1',
'name': '生成2025年12月客户开单数据Excel',
'file': 'generate_november_customer_excel.py',
'description': '生成2025年12月客户开单数据Excel文件,包含客户档案号、姓名、注册时间、来源、开单业绩、时间、归属门店等字段'
},
{
'id': '2',
'name': '导出所有用户剩余权益数据',
'file': 'export_all_member_remaining_rights.py',
'description': '导出所有用户的剩余权益数据Excel文件,包含客户档案号、姓名、手机号、注册时间、归属门店、来源、品项名称、单价、总购买次数、已消费次数、已退卡次数、已扣除次数、剩余次数、剩余价值等字段'
},
{
'id': '3',
'name': '测试天王组业绩完成度接口',
'file': 'test_tianwang_api.py',
'description': '测试天王组业绩完成度接口,查询2025年1月和当前月份的数据'
}
]
def clear_screen():
"""清屏"""
os.system('clear' if os.name != 'nt' else 'cls')
def print_header():
"""打印标题"""
print("=" * 70)
print(" " * 20 + "Python脚本执行工具")
print("=" * 70)
print()
def print_menu():
"""打印菜单"""
print_header()
print("可用脚本列表:")
print("-" * 70)
for script in SCRIPTS:
print(f" [{script['id']}] {script['name']}")
print(f" {script['description']}")
print()
print("-" * 70)
print(" [0] 退出")
print("=" * 70)
print()
def get_script_by_id(script_id):
"""根据ID获取脚本配置"""
for script in SCRIPTS:
if script['id'] == script_id:
return script
return None
def execute_script(script):
"""执行脚本"""
script_path = Path(__file__).parent / script['file']
if not script_path.exists():
print(f"\n❌ 错误: 脚本文件不存在: {script_path}")
input("\n按回车键继续...")
return
print(f"\n{'=' * 70}")
print(f"正在执行: {script['name']}")
print(f"脚本文件: {script['file']}")
print(f"{'=' * 70}\n")
try:
# 使用subprocess执行脚本,保持输出实时显示
process = subprocess.Popen(
[sys.executable, str(script_path)],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
universal_newlines=True,
bufsize=1
)
# 实时输出
for line in process.stdout:
print(line, end='')
process.wait()
if process.returncode == 0:
print(f"\n{'=' * 70}")
print(f"✅ 脚本执行成功: {script['name']}")
print(f"{'=' * 70}\n")
else:
print(f"\n{'=' * 70}")
print(f"❌ 脚本执行失败: {script['name']} (退出码: {process.returncode})")
print(f"{'=' * 70}\n")
except Exception as e:
print(f"\n❌ 执行脚本时发生错误: {e}\n")
input("按回车键继续...")
def main():
"""主函数"""
while True:
clear_screen()
print_menu()
try:
choice = input("请选择要执行的脚本 (输入数字): ").strip()
if choice == '0':
print("\n感谢使用,再见!\n")
break
script = get_script_by_id(choice)
if script:
clear_screen()
execute_script(script)
else:
print(f"\n❌ 无效的选择: {choice}")
input("按回车键继续...")
except KeyboardInterrupt:
print("\n\n程序已中断,再见!\n")
break
except Exception as e:
print(f"\n❌ 发生错误: {e}")
input("按回车键继续...")
if __name__ == '__main__':
main()