吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1759|回复: 6
收起左侧

[Python 原创] POSTMAN转EXCEL

  [复制链接]
oscouregeo 发表于 2022-12-5 15:40
[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)

免费评分

参与人数 1吾爱币 +7 热心值 +1 收起 理由
wushaominkk + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

lfordch 发表于 2022-12-5 18:05
感谢分享,收藏学习了
yzjtxwd 发表于 2022-12-5 22:23
smile4009 发表于 2022-12-6 00:43
a2523188267 发表于 2022-12-6 09:49

感谢分享
小坏丶 发表于 2022-12-6 14:55
这个转excel后是啥样的
 楼主| oscouregeo 发表于 2022-12-6 16:22
小坏丶 发表于 2022-12-6 14:55
这个转excel后是啥样的

Folder        Name        Method        Path          Headers        Query        Body               
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-25 02:59

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表