-
Notifications
You must be signed in to change notification settings - Fork 1
/
everythingToExcel-nosocket.py
188 lines (175 loc) · 6.88 KB
/
everythingToExcel-nosocket.py
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#-*- coding: UTF-8 -*-
# 不进行socket连接
import argparse
import time
import json
import traceback
import xlwt
import os
from urllib import parse
if os.name == "nt":
os.system("")
SET_DEAULT = "\033[0m"
SPECIAL_PURPLE = "\033[0;35m[*] "
INFO_BLUE = "\033[0;34m[i] "
TIME_GREEN = "\033[7;32m[+] "
ERROR_RED = "\033[0;31m[!] "
def EholeJsonData():
global args
result = []
with open(args.inputFile, encoding="utf8") as f:
origin = f.readlines()
for line in origin:
line_result = []
line_json = json.loads(line.strip())
line_json['ip'] = "pass"
for item in args.header:
# 判断是否含key,没有的话放空字符串
if line_json[item] == "None" or line_json[item] == None:
line_result.append("")
else:
line_result.append(str(line_json[item]))
result.append(line_result)
return result
def YassoJsonData():
global args
result = []
with open(args.inputFile, encoding="utf8") as f:
origin = f.readlines()
origin = origin[0]
line_json = json.loads(origin.strip())
for line_item in line_json:
line_result = []
for header_item in args.header:
# 判断是否含key,没有的话放空字符串
if line_item[header_item] == "None" or line_item[header_item] == None:
line_result.append("")
else:
line_result.append(str(line_item[header_item]))
result.append(line_result)
return result
def txtData():
global args
result = []
with open(args.inputFile, encoding="utf8") as f:
origin = f.readlines()
for line in origin:
result.append(line.strip().split(args.split))
return result
def csvData():
global args
result = []
with open(args.inputFile, encoding="utf8") as f:
origin = f.readlines()
for line in origin:
result.append(line.strip().split(args.split))
return result
# 4种输入的建表函数
def saveSingleSheet(data):
global args
# xlwt-workbook 建立
workbook = xlwt.Workbook(encoding='utf-8')
# xlwt-sheet 建立
sheet = workbook.add_sheet('GoodLuck')
# 若数据为空,报错
if data == None or data == []:
raise Exception("输入文件的数据不能为空")
# 写入表头(如果表头存在)
if args.header:
for header_j in range(len(args.header)):
sheet.write(0, header_j, args.header[header_j])
# 最大列数
cols_count = 0
for i in data:
cols_count = len(i) if len(i) > cols_count else cols_count
# 记录每列最大宽度
if args.header:
col_list = [len(args.header[x].encode('gb18030')) for x in range(len(args.header))]
else:
col_list = [0 for x in range(cols_count)]
# 数据写入Excel
for i in range(len(data)):
for j in range(len(data[i])):
# 写入
sheet.write(i + 1, j, data[i][j])
# 找更大的值
col_list[j] = len(data[i][j].encode('gb18030')) if len(data[i][j].encode('gb18030')) > col_list[j] else \
col_list[j]
for i in range(0, len(col_list)):
# 256*字符数得到excel列宽
sheet.col(i).width = 255 * (col_list[i] + 2)
# 保存xls
workbook.save(args.outputFile)
def main():
# 程序的特殊信息
print(SPECIAL_PURPLE + 'everythingToExcel')
print(SPECIAL_PURPLE + '把多种格式的数据转为Excel')
print(SPECIAL_PURPLE + 'Write by zh9ng9 <\033[5;35mhttps://github.com/Zh9ng9/everythingToExcel\033[0;35m> (202207)')
print(SPECIAL_PURPLE + '''Example:\n python3 everythingToExcel.py -iF ./input.json -oF output.xls\n python3 everythingToExcel.py -t txt -iF ./input.txt -H "序号|姓名|xxx|yyy|zzz" -oF output.xls''')
print(SET_DEAULT)
# 参数配置
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--type', required=False, type=str, default='EholeJson',
choices=['EholeJson', 'FscanTxt', 'YassoJson', 'txt', 'csv'],
help='string in {EholeJson,FscanTxt,YassoJson,txt,csv} 文件类型(default:EholeJson)')
parser.add_argument('-s', '--split', required=False, type=str, default=",", help='char 分隔符 (default:\',\')')
parser.add_argument('-H', '--header', required=False, type=str, default=None,
help='string excel表头,以\'|\'分割 (default:空)')
parser.add_argument('-iF', '--inputFile', required=True, type=str, default=None, help='string 输入的文件名')
parser.add_argument('-oF', '--outputFile', required=True, type=str, default=None,
help='string 输出的文件名(后缀如果不是xls会自动添加xls)')
# args作为全局变量
global args, data
args = parser.parse_args()
# 开始时间
start = time.time()
# 打印输出获取到的参数
print(INFO_BLUE + "文件类型:" + str(args.type))
print(INFO_BLUE + "分隔符:" + str(args.split))
print(INFO_BLUE + "excel表头:" + str(args.header))
print(INFO_BLUE + "输入的文件名:" + str(args.inputFile))
if not args.outputFile.endswith(".xls"):
args.outputFile = args.outputFile + ".xls"
print(INFO_BLUE + "输出的文件名:" + str(args.outputFile))
print(SET_DEAULT)
# 打印输出开始运行提示
print(TIME_GREEN + "程序开始运行..." + SET_DEAULT)
print()
try:
# 表头的修改
if args.header:
args.header = args.header.split('|')
# 根据不同的type从不同函数获取数据
if args.type == "txt":
data = txtData()
saveSingleSheet(data)
elif args.type == "csv":
data = csvData()
saveSingleSheet(data)
elif args.type == "EholeJson":
# EholeJson的表头
header = ["url", "ip","cms", "server", "statuscode", "length", "title"]
# EholeJson的表头写入header参数
args.header = header
data = EholeJsonData()
saveSingleSheet(data)
elif args.type == "YassoJson":
# Yasoo的表头
header = ["HostName", "Ports", "WeakPass", "Web"]
# Yasso的表头写入header参数
args.header = header
data = YassoJsonData()
saveSingleSheet(data)
elif args.type == "FscanTxt":
# 输出文件名
args.outputFile = f"fscanAuxResult_***.xlsx"
# Yasoo的表头
os.system('python fscanAux.py ' + args.inputFile)
except Exception as e:
print(ERROR_RED + "程序出现错误,已停止")
print(traceback.format_exc())
end = time.time()
print(TIME_GREEN + "已将\"%s\"文件转换为Excel文件\"%s\"" %(args.inputFile, args.outputFile) + SET_DEAULT)
print(TIME_GREEN + "程序用时: " + str(end - start) + SET_DEAULT)
if __name__ == '__main__':
main()