コード例 #1
0
def format_output(val,eachformat, eachstyle):
    "Returns a excel cell with the data formated as specified"
    new_cell = WriteOnlyCell(xls_sheet, value = "init")
    new_cell.style = eachstyle
    if val==None:
        val="None"
    elif eachformat == None:
        pass
    elif eachformat == "OLE":
        val = ole_timestamp(val)
        new_cell.number_format = 'YYYY MMM DD'
    elif eachformat.startswith("OLE:"):
        val = ole_timestamp(val)
        val = val.strftime(eachformat[4:])
    elif eachformat=="FILE":
        val = file_timestamp(val)
        new_cell.number_format = 'YYYY MMM DD'
    elif eachformat.startswith("FILE:"):
        val = file_timestamp(val)
        val = val.strftime(eachformat[5:])
    elif eachformat.lower() == "lookup_id":
        val = id_table.get(val, "No match in srum lookup table for %s" % (val))
    elif eachformat.lower() == "lookup_luid":
        val = lookup_luid(val)
    elif eachformat.lower() == "lookup_sid":
        val = "%s (%s)" % (val, lookup_sid(val))
    elif eachformat.lower() == "seconds":
        val = val/86400.0
        new_cell.number_format = 'dd hh:mm:ss'
    elif eachformat.lower() == "md5":
        val = hashlib.md5(str(val)).hexdigest()
    elif eachformat.lower() == "sha1":
        val = hashlib.sha1(str(val)).hexdigest()
    elif eachformat.lower() == "sha256":
        val = hashlib.sha256(str(val)).hexdigest()
    elif eachformat.lower() == "base16":
        if type(val)=="<type 'int'>":
            val = hex(val)
        else:
            val = str(val).encode("hex")
    elif eachformat.lower() == "base2":
        if type(val)==int:
            val = bin(val)
        else:
            try:
                val = int(str(val),2)
            except :
                val = val
                new_cell.comment = Comment("Warning: Unable to convert value %s to binary." % (val),"srum_dump")
    elif eachformat.lower() == "interface_id" and options.reghive:
        val = interface_table.get(str(val),"")
    elif eachformat.lower() == "interface_id" and not options.reghive:
        val = val
        new_cell.comment = Comment("WARNING: Ignoring interface_id format command because the --REG_HIVE was not specified.", "srum_dump")
    else:
        val = val
        new_cell.comment =  Comment("WARNING: I'm not sure what to do with the format command %s.  It was ignored." % (eachformat), "srum_dump")  
    new_cell.value = val  
    return new_cell
コード例 #2
0
def save_xlsx_template(file_path, data, header=None, col_id=None):
    """将输入保存到 Excel 文件中。使用文件模板

    全部保存为文本。

    Args:
        file_path (str): xlsx 文件的路径
        data (list[list]): 要保存的数据,二维
        header (list): 第一行
        col_id (list[int]): data 中列号到 xlsx 中列号的映射
    """
    if len(data) <= 0:
        return save_xlsx(file_path, data, header, col_id)

    cd = os.path.dirname(os.path.abspath(__file__))
    num_col = len(data[0])
    if num_col == 9:
        shutil.copy(os.path.join(cd, 'data/prototype_list.xlsx'), file_path)
    elif num_col == 12:
        shutil.copy(os.path.join(cd, 'data/prototype_pair.xlsx'), file_path)
    elif num_col == 8:
        shutil.copy(os.path.join(cd, 'data/prototype_ui.xlsx'), file_path)
    else:
        return save_xlsx(file_path, data, header, col_id)

    if col_id is None:
        col_id = list(range(0, num_col))
    max_col_id = max(col_id)

    workbook = openpyxl.load_workbook(file_path)
    sheet = workbook.get_sheet_by_name('sheet 1')

    # 格式
    fonts = [sheet.cell(row=1, column=i + 1).font.copy() for i in col_id]
    fills = [sheet.cell(row=1, column=i + 1).fill.copy() for i in col_id]
    alignments = [
        sheet.cell(row=1, column=i + 1).alignment.copy() for i in col_id
    ]
    number_formats = [
        sheet.cell(row=1, column=i + 1).number_format for i in col_id
    ]

    # 写入内容
    row_id = 1
    for row in data:
        row_cells = [
            '',
        ] * (max_col_id + 1)
        for j in range(0, num_col):
            cell = WriteOnlyCell(sheet, value=str(row[j]))
            cell.font = fonts[j]
            cell.fill = fills[j]
            cell.alignment = alignments[j]
            cell.number_format = number_formats[j]
            row_cells[col_id[j]] = cell
        sheet.append(row_cells)
        row_id += 1

    workbook.save(file_path)
コード例 #3
0
            print "Skipping corrupt row in the %s table.  The last good row was %s." % (
                each_sheet, row_num)
            continue
        if ese_row == None:
            break
        #The row is retrieved now use the template to figure out which ones you want and format them
        xls_row = []
        row_num += 1
        for eachcolumn, eachformat, eachstyle in zip(ese_template_fields,
                                                     ese_template_formats,
                                                     ese_template_styles):
            if eachcolumn == "#XLS_COLUMN#":
                val = eachformat.replace("#ROW_NUM#", str(row_num))
                val = WriteOnlyCell(xls_sheet, value=val)
                val.style = eachstyle.style
                val.number_format = eachstyle.number_format
            else:
                val = ese_row.get(eachcolumn, "UNABLETORETRIEVECOLUMN")
                if val == "UNABLETORETRIEVECOLUMN":
                    val = "WARNING: Invalid Column Name " + eachcolumn + " - Try one of these:" + str(
                        ese_template_fields) + str(
                            eachcolumn in ese_template_fields)
                    val = WriteOnlyCell(xls_sheet, value=val)
                    val.style = eachstyle
                else:
                    val = format_output(val, eachformat, eachstyle.style)
            #print dir(new_cell.style.font)
            xls_row.append(val)
        xls_sheet.append(xls_row)

firstsheet = target_wb.get_sheet_by_name("Sheet")