python openpyxl

auto generate result exel

from openpyxl import Workbook

def openFile(filename):
    newlines = []
    with open(filename) as file:
        lines = file.readlines()
        count = 0
        for line in lines:
            if count <= 4:
                count += 1
                continue
            elif count >= 48:
                break
            else:
                newline = line.rstrip('\n').split('\t')
                newlines.append(newline)
                count += 1
    return newlines;

# nums--total of machines,sn--index of machine(start 1),newlines--currnte file information
def writeExcel(nums, sn, newlines,ws):
    start = sn + 1
    i = 1

    for element in newlines:
        ws.cell(row=i,column=1,value=element[0])
        ws.cell(row=i, column=start, value=element[1])
        ws.cell(row=i, column=start + nums + 2, value=element[2])
        ws.cell(row=i, column=start + (nums + 2) * 2, value=element[3])
        i += 1
    return;

wb = Workbook()
nums=2
probeList=["F2_5c","F4_12L","G1_4P"]
Listfiles = ["myperformanceF2_5C.txt", "oldperformanceF2_5C.txt","myperformanceF4_12L.txt", "oldperformanceF4_12L.txt","myperformanceG1_4P.txt","oldperformanceG1_4P.txt"]

openList = []
for i in Listfiles:
    openList.append(openFile(i))

# j--index of current machine
j = 1
#probe index
probeIndex,product=1,0
for probe in probeList:
    ws=wb.create_sheet(probe)
    for i in range(1,nums+1):
        writeExcel(nums,i,openList[product],ws)
        product+=1
#write title


wb.save("sample.xlsx")

你可能感兴趣的:(python openpyxl)