Python脚本将swagger转化为excel接口清单

之前找的一篇脚本,怕遗失,特此保留。

1.先将swagger.json文件保存为txt文件。
.Net Core版本:在这里插入图片描述
.Net Framework版本:在这里插入图片描述

2.执行脚本将文件转化为Excel文档接口清单。

import json
import xlwt

api_excel = xlwt.Workbook(encoding='utf-8')  # 创建一个文档
api_sheet = api_excel.add_sheet('CRM接口')    # 添加一个sheet
json_file = open('C:/Users/DELL/Desktop/swagger-ams.txt', encoding='utf-8')  # 打开保存的swagger文本文档
api_data = json.load(json_file)     # 将文档json内容转换为Python对象
api = api_data['paths']  # 取swagger文件内容中的path,文件中path是键名
path_list = []  # 创建接口地址空列表
method_list = []  # 创建请求方式空列表
tags_list = []  # 创建接口分类空列表S
summary_list = []  # 创建接口描述空列表
for path in api.keys():  # 循环取key
    values = api[path]  # 根据key获取值
    method = tuple(values.keys())[0]  # 获取请求方式,文件中请求方式是key
    path_list.append(path)  # 将path写入接口地址列表中
    method_list.append(method)  # 将method写入请求方式列表中
    if method == 'get':  # key为get时从get里面取分类和描述,key为post时从post里面取分类和描述
        tags = values['get']['tags'][0]  # 获取接口分类
        summary = values['get']['summary']  # 获取接口描述
        tags_list.append(tags)  # 将接口分类写入列表中
        summary_list.append(summary)  # 将接口描述写入列表中
    if method == 'post':
        tags = values['post']['tags'][0]
        summary = values['post']['summary']
        tags_list.append(tags)
        summary_list.append(summary)
for i in range(len(path_list)):  # 将接口path循环写入第一列
    api_sheet.write(i, 0, path_list[i])
for j in range(len(method_list)):  # 将请求方式循环写入第二列
    api_sheet.write(j, 1, method_list[j])
for m in range(len(tags_list)):  # 将接口分类循环写入第三列
    api_sheet.write(m, 2, tags_list[m])
for n in range(len(summary_list)):  # 将接口描述循环写入第四列
    api_sheet.write(n, 3, summary_list[n])
api_excel.save('C:\\Users\\DELL\\Desktop\\接口地址.xls')     # 保存文件
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值