使用python读写excel 生成json文件

配合运维上传发布脚本,需要生成一个json文件给后台,他们获取对应的发布信息和脚本

  1. 将发布信息填写进表格《生产发布记录.xlsx》
    2.执行getRelease.bat
    3.上传release_note.json到git根目录

写了一个简单的python脚本,在本地用bat文件进行调用执行
1.bat脚本代码如下:

cd C:\Users\yangmx\Desktop\发布记录
python getExcelData.py

python代码:

import openpyxl
import json

#Add by MaxYang  at 2018-11-06 16:22
#This scripts is for generate release json file 
#1.add your info to excle sheet 
#2.run getRelease.bat to call this file
#3.upload the release_note.json to git server

json_all  =[]
def read_07_Excel(file_path):
    wb = openpyxl.load_workbook(file_path)  #打开文件
    # sheet = wb.get_sheet_by_name("测试表格2007")
    sheet = wb["release"]  #通过sheet名称锁定表格
    # new_list =[]
    for row in sheet.rows:  #循环所有的行
        list_release = []
        for cell in row:#循环行中所有的单元格
            print(cell.value, "\t", end="")  #获取单元格的值
            list_release.append(cell.value)
        print()
        # print(list_release)
        if list_release[0]!="发布原因":
            process_to_json(list_release)

        # new_list.append(list_release)
        # list_all =[]
        # list_all.append(list_release)
     # print(new_list)


def process_to_json(json_list):
    list_json = json_list
    release_dirt = {'发布原因':'test','发布人':'test','发布工程名称':'test','发布版本':'test','发布类型':'test'}
    release_dirt['发布原因'] = list_json[0]
    release_dirt['发布人'] = list_json[1]
    release_dirt['发布工程名称'] = list_json[2]
    release_dirt['发布版本'] = list_json[3]
    release_dirt['发布类型'] = list_json[4]
    print(release_dirt)
    # json_str = json.dumps(release_dirt)
    json_all.append(release_dirt)
    # f = open('release_notes.json', 'w')
    # f.writelines(json_str)
    # f.close()


def process_json():
    json_dirt = {"发布列表": json_all}
    json_str = json.dumps(json_dirt)
    f = open('release_note.json', 'w')
    f.write(json_str)
    f.close()


# def process_list_json():
#     json_dirt = {"发布列表":json_all}
#     json_str = json.dumps(json_dirt)
#     f = open('others.txt', 'w')
#     f.write(json_str)
#     f.close()


# file_07_excel="C:/Users/yangmx/Desktop/发布记录/生产发布记录.xlsx"
#设置表格路径,默认是执行文件的当前目录
file_07_excel="生产发布记录.xlsx"
read_07_Excel(file_07_excel)
# print(json_all)
process_json()

你可能感兴趣的:(使用python读写excel 生成json文件)