一次性遍历当前文件夹下的所有Excel文件并导出为Flutter的ListData格式文档。
一次性遍历当前文件夹下的所有Excel文件并导出为Flutter的ListData格式文档,所保存的txt文件的文件名也是以Excel的文件名来命名的。
#博客园商君治国安邦制作 import xlrd import glob filename_arr ={} j = 0 for files in glob.glob("*.xls"): filename_arr[j] = files j += 1 for key,value in filename_arr.items(): filename = value filefirstname = filename.split('.')[0] print(filename) print(filefirstname) def strs(row): global values2, values3, values4, values5, values6, values7, values8, values9, values10 values1 = "" Values2 = "" Values3 = "" Values4 = "" Values5 = "" Values6 = "" Values7 = "" Values8 = "" values0 = "" values9 = "" values10 = "" values = "" for i in range(len(row)): if i == 0: values1 = '"illsort": ' + "'" + values + str(row[i]) + "'" + "," + "\n" elif i == 1: values2 = '"illsortdetails": ' + "'" + values + str(row[i]) + "'" + "," + "\n" elif i == 2: values3 = '"title": ' + "'" + values + str(row[i]) + "'" + "," + "\n" elif i == 3: values4 = '"material": ' + "'" + values + str(row[i]) + "'" + "," + "\n" elif i == 4: values5 = '"model": ' + "'" + values + str(row[i]) + "'" + "," + "\n" elif i == 5: values6 = '"function": ' + "'" + values + str(row[i]) + "'" + "," + "\n" elif i == 6: values7 = '"qty": ' + "'" + values + str(row[i]) + "'" + "," + "\n" elif i == 7: values8 = '"attention": ' + "'" + values + str(row[i]) + "'" + "," + "\n" elif i == 8: values9 = '"spec": ' + "'" + values + str(row[i]) + "'" + "," + "\n" else : values10 = '"store": ' + "'" + values + str(row[i]) + "'" + "," + "\n" values = values1 + values2 + values3 + values4 + values5 + values6 + values7 + values8 + values9 + values10 return values filedata = xlrd.open_workbook(filename) sqlfile = open(filefirstname + ".txt", "a", encoding='utf-8') table = filedata.sheets()[0] print(filedata.sheets()[0]) nrows = table.nrows ncols = table.ncols colnames = table.row_values(0) print(nrows) print(ncols) print(colnames) for ronum in range(1, nrows): row = table.row_values(ronum) values = '{ ' + strs(row) + ' },' sqlfile.writelines(values + "\r") sqlfile.close() if __name__ == '__main__': print('Done') # See PyCharm help at https://www.jetbrains.com/help/pycharm/