最近在做安全测试时,需要对项目工程类的文件进行检查,所以根据项情况 ,整理代码搜索关键词,并对代码进行搜索,检查是否符合安全要求,并将查询出的结果写入到excel表格中,以方便查看结果。
对excle操作之前选用xlwt库进行处理,但有以下xlwt缺点:
版本
只能处理Excel97-2003或Excel 97之前版本的xls格式
存储数据过大
存储数据过大时,会报错Exception: String longer than 32767 characters
因查询数据较大,因此使用的是XlsxWrite库进行( https://github.com/jmcnamara/XlsxWriter ),以此来弥补以上缺点:
代码如下:
# -*- coding: utf-8 -*-
import os
def findAllFiles(path):
find_files = []
for root, dirs, files in os.walk(path): #
for file in files:
pathWithFile = os.path.join(root,file)
if os.path.splitext(pathWithFile)[1] in [".java",".xml",".iml",".bat"]:
find_files.append(pathWithFile)
else:
pass
return find_files
# read file
def readfile (readfilepath):
newStr = []
keywords = ["qq","mail","tel","phone","sex","key","password","passwd",
"pwd","username","TGC","token","TOKEN","GPS","gps","admin",
"test","Test","log.","system.println.","SSL","AES","sha1",
"md5","MD5","Base64","base64","crash","MSG","msg"]
with open(readfilepath,'r',encoding='utf-8') as f:
#enumerate与for 联用,多用于计数
for num,lines in enumerate(f):
for i in range(len(keywords)):
if keywords[i] in lines:
strName = readfilepath
if strName:
getLines = lines.strip()
strName = readfilepath.strip()
newStr.append(strName)
newStr.append(num)
newStr.append(keywords[i])
newStr.append(getLines)
#return strName,kw,strCon #默认返回的是tuple,相当于返回类型为:return (strName,newStr)
return newStr
def perLinesResult(srcFilePath):
newcontent = []
needReadFiles = findAllFiles(srcFilePath)
for i in range(len(needReadFiles)):
getContent = readfile(needReadFiles[i]) # 对返回的元组值,分别定义参数进行接收,并分别对接收后的值进行处理
if getContent: #判断getContent如果存在值,则为真
newcontent.extend(getContent)
return newcontent
import xlsxwriter
def write_excel(code_path,result_path):
workbook = xlsxwriter.Workbook(result_path)
worksheet = workbook.add_worksheet("test")
#表头样式
formatHeader = workbook.add_format({
'bold': True, # 字体加粗
'border': 1, # 单元格边框宽度
'align': 'center',
'valign': 'vcenter',
'fg_color': '#F4B084',
})
#单元格样式
wsformat = workbook.add_format({
"font_size":10,
"top":1,
"left":1,
"right":1,
"bottom":1,
})
worheader = [u"文件路径",u"行号",u"关键词", u"查询的内容"]
for i in range(len(worheader)):
worksheet.write(0,i,worheader[i],formatHeader)
content = perLinesResult(code_path)
column = 0
row = 1
for j in range(len(content)):
worksheet.write(row,column,content[j],wsformat)
column = column + 1
if column % 4 == 0:
row = row + 1
column = 0
workbook.close()
if __name__ == '__main__':
#src_File_Path = "D:/Code/iOS/IOS"
Code_src_File_Path = "D:/Code/Android"
result_file_path = "F:/Leanring_Files/xxx.xlsx"
write_excel(Code_src_File_Path,result_file_path)