Xlrd初使用

in Python with 0 comment

google老半天也没找到复制EXCEL中range的方法,老老实实用for循环写写吧
反正是给统计的,凑合用就行了。

from xlrd import *

wb = open_workbook('a.xlsx')
sh1 = wb.sheet_by_name('逐梦-箱子')
sh2 = wb.sheet_by_name('如梦-箱子')
sh3 = wb.sheet_by_name('若梦-箱子')
sh4 = wb.sheet_by_name('何梦-箱子')
fExcel = ['_1.txt', '_2.txt', '_3.txt', '_4.txt']


def sh2arr(sh):
    temp = []
    for row in range(100):
        values = []
        for col in range(7):
            if row == 0:
                pass
            elif (row > 0) and (col == 5):
                if sh.cell(row, col).value > 1:
                    values.append(sh.cell(row, col).value)
                else:
                    values.append(int(sh.cell(row, col).value))
            else:
                values.append(sh.cell(row, col).value)
        if not values:
            pass
        else:
            temp.append(values)
    return temp

Arr_1 = sh2arr(sh1)
Arr_2 = sh2arr(sh2)
Arr_3 = sh2arr(sh3)
Arr_4 = sh2arr(sh4)

with open(fExcel[0], 'w', encoding='utf-8') as FFF:
    for x in Arr_1:
        for xx in x:
            FFF.write(str(xx)+'    ')
        FFF.write('\n')

with open(fExcel[1], 'w', encoding='utf-8') as FFF:
    for x in Arr_2:
        for xx in x:
            FFF.write(str(xx)+'    ')
        FFF.write('\n')

with open(fExcel[2], 'w', encoding='utf-8') as FFF:
    for x in Arr_3:
        for xx in x:
            FFF.write(str(xx)+'    ')
        FFF.write('\n')

with open(fExcel[3], 'w', encoding='utf-8') as FFF:
    for x in Arr_4:
        for xx in x:
            FFF.write(str(xx)+'    ')
        FFF.write('\n')
Responses