导出 Swagger 接口到 Excel

 

 

 

import json
import xlwt

api_excel = xlwt.Workbook(encoding='utf-8')  # 创建一个文档
api_sheet = api_excel.add_sheet('CRM接口')    # 添加一个sheet
json_file = open('D:/Test/api-docs.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 = []  # 创建接口分类空列表
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('D:\\Test\\CRM接口列表.xls')     # 保存文件

 

 

原文链接

将Swagger中所有接口地址用Python写入Excel中 - 童薰 - 博客园

 

posted @ 2024-12-26 20:46  小丸  阅读(681)  评论(0)    收藏  举报