import json
import xlwt
api_excel = xlwt.Workbook(encoding='utf-8') # 创建一个文档
api_sheet = api_excel.add_sheet('swaggerAPI') # 添加一个sheet
json_file = open('swagger.json', encoding='utf-8') # 打开保存的swagger文本文档
api_data = json.load(json_file) # 将文档json内容转换为Python对象
api = api_data['paths'] # 取swagger文件内容中的path,文件中paths是键名
api_sheet.write(0, 0, "API路径")
api_sheet.write(0, 1, "方法")
api_sheet.write(0, 2, "模块")
api_sheet.write(0, 3, "描述")
i = 1
for path in api.keys(): # 循环取key
values = api[path] # 根据key获取值
print(values.keys()) #dict_keys(['post', 'put'])
for key in values.keys(): # 循环取key 存在一个路径对应多个方法情况
method = key # 获取请求方式,文件中请求方式是key
tags = values[method]['tags'][0] # 获取接口分类
if "summary" in values[method].keys():
summary = values[method]['summary'] #swagger描述信息
else:
summary = None
api_sheet.write(i, 0, path)
api_sheet.write(i, 1, method)
api_sheet.write(i, 2, tags)
api_sheet.write(i, 3, summary)
i+=1
api_excel.save('swaggerAPI.xls') # 保存文件
使用python解析swagger文档到excel
于 2022-10-27 15:13:56 首次发布