ExcelWrite-自动生成中文表格

python脚本:读取txt文件内测试指标,自动保存成excel文件

 

 

class excelProcess:
        def __init__(self, sheetName):
        self.sheetName = sheetName
        self.wbk = xlwt.Workbook()
        self.sheet = self.wbk.add_sheet(self.sheetName, cell_overwrite_ok=True)
        self.rows = 0
        self.style = xlwt.easyxf('font: bold 1')
        alignment = xlwt.Alignment() # Create Alignment
        alignment.horz = alignment.HORZ_CENTER # May be: HORZ_GENERAL, HORZ_LEFT, HORZ_CENTER, HORZ_RIGHT, HORZ_FILLED, HORZ_JUSTIFIED, HORZ_CENTER_ACROSS_SEL, HORZ_DISTRIBUTED
        alignment.vert = alignment.VERT_CENTER # May be: VERT_TOP, VERT_CENTER, VERT_BOTTOM, VERT_JUSTIFIED, VERT_DISTRIBUTED
        self.style_alignment = xlwt.XFStyle()
        self.style_alignment.alignment = alignment
        self.style.alignment = alignment

    def WriteLog(self, message, date):
        fileName = os.path.join(os.getcwd(), date + '.txt')
        with open(fileName, 'a') as f:
            f.write(message)

    def WriteSheetRow(self, rowValueList, isBold):
        i = 0
        # index = int(rowValueList[0])+1
        for svalue in rowValueList:
            if isBold:
                self.sheet.write(self.rows, i, svalue, self.style)
            else:
                self.sheet.write(self.rows, i, svalue, self.style_alignment)
            i += 1
        self.rows +=1

    def Merge_Title(self):
        #合并标题单元格
        self.sheet.write_merge(0,0,1,7,eval_TITLE, self.style)
        self.sheet.write_merge(1,1,1,2,detection_TITLE[1], self.style)
        self.sheet.write_merge(1,1,3,4,detection_TITLE[2], self.style)
        self.sheet.write_merge(1,1,5,6,detection_TITLE[3], self.style)
        self.sheet.write_merge(0,2,0,0,ROW_TITLE[0], self.style)
        self.sheet.write_merge(1,2,7,7,ROW_TITLE[7], self.style)

    def save_Excel(self, fileN):
        # fileName = os.path.join(fileN)
        self.wbk.save(fileN)

def save_excel(file_txt,json_classes,save_filename):
    excel = excelProcess(save_filename[:-4])
    excel.WriteSheetRow(eval_TITLE,True)
    excel.WriteSheetRow(detection_TITLE, True)
    excel.WriteSheetRow(ROW_TITLE, True)
    excel.Merge_Title()
    json_class_name = json_classes.split(',')
    for index,line in enumerate(file_txt):
        save_msg = line.split(' ')
        save_msg.insert(0,plate_map[json_class_name[index]])
        excel.WriteSheetRow(save_msg,False)
        excel.save_Excel(save_filename)


    '''
    Function  : 自动化转换脚本
    == ========================================================================
    == Description: 将mean_index.txt文件内测试指标保存成excel文件,输出类别名json_classes根据需要自行修改。
    == use: 在voc_eval_dlpr_fmc.py文件中引用本模块,文件末添加如下代码:
    # import ExcelWrite as EW
    # with open('mean_index.txt', 'r') as fff:
    #     EW.save_excel(fff,json_classes,'mean_index_Result.xls')
    == Created: zhiming_liu 2019-10-23 18:26:24 
    == ========================================================================
    '''
    json_classes = 'traffic_EMBASSY_rect,traffic_GANGAO_rect,traffic_MILITARY_rect,traffic_NEWE_rect,traffic_NORMAL_rect,traffic_POLICE_rect,traffic_WJ_rect,traffic_YELLOW_rect,traffic_DOUBLEYELLOW_rect,traffic_AGRI_rect'
    with open('mean_index.txt', 'r') as fff:
        save_excel(fff,json_classes,'mean_index_Result.xls')
 

你可能感兴趣的:(开发)