POSTMAN转EXCEL
[Python] 纯文本查看 复制代码 import json
import openpyxl
class postmantocase:
wb = openpyxl.Workbook()
ws = wb.active
row = 2
col = 1
def read_folder(self,path):
with open(path,'r',encoding='utf-8') as f:
load_dict = json.load(f)
return load_dict
def check(self,value):
return value if value else ''
def parseFolder(self,folder):
if folder.get('request'):
self.parseItem(folder)
self.col = 1
else:
if folder.get('name'):
self.ws.cell(self.row, 1).value = self.check(folder['name'])
self.row+=1
for item in folder['item']:
self.parseFolder(item)
def __init__(self):
self.ws.cell(1, 1).value='Folder'
self.ws.cell(1, 2).value='Name'
self.ws.cell(1, 3).value='Method'
self.ws.cell(1, 4).value='Path'
self.ws.cell(1, 5).value='Headers'
self.ws.cell(1, 6).value='Query'
self.ws.cell(1, 7).value='Body'
def appendForwardSlash(self,string):
if string[len(string) - 1] == '/':
return string
else:
return string + '/'
def parseItem(self,item):
queryRow = self.row
bodyRow = self.row
headerRow =self.row
self.ws.cell(self.row, self.col+1).value = self.check(item['name'])
self.ws.cell(self.row, self.col+2).value = self.check(item['request']['method'])
self.ws.cell(self.row, self.col+3).value = self.appendForwardSlash('/'.join(item['request']['url']['path'] if item['request']['url']['path'] else ['']))
if item['request'].get('header') and len(item['request']['header'])>0:
for h in item['request']['header']:
self.ws.cell(headerRow,self.col+4).value = json.dumps(h)
headerRow+=1
if item['request']['url'].get('query') and len(item['request']['url']['query'])>0:
for h in item['request']['url']['query']:
self.ws.cell(queryRow,self.col+5).value = json.dumps(h)
queryRow+=1
if item['request'].get('body') and len(item['request']['body']) > 0:
json_str = self.check(item['request']['body']['raw'])
self.ws.cell(bodyRow, self.col+6).value = json_str
self.row = max(headerRow, queryRow, bodyRow) + 1
def run(self,path):
self.parseFolder(self.read_folder(path))
self.wb.save('test.xlsx')
if __name__ == '__main__':
a = postmantocase()
path = r'C:\Users\Administrator\Desktop\postman_collection.json'
a.run(path)
|