如何使用 xlwt 和 xlrd 将每五行放入一个 sheet?
How to put every five rows into one sheet with xlwt and xlrd?
现在我只能将14行数据全部保存到Sheet1。
我想把每 5 行放在一个 sheet 中,例如:
现在我的代码是这样的:
import xlwt
import xlrd
from xlutils.copy import copy
myyear = '2018'
myweek = '10'
mycd='1001'
fileName = myyear + myweek + '.xls'
workbook = xlwt.Workbook(encoding='UTF-8')
worksheet = workbook.add_sheet('sheet1')
worksheet.write(0, 0, label='year')
worksheet.write(0, 1, label='week')
worksheet.write(0, 2, label='CD')
worksheet.write(0, 3, label='keyword')
worksheet.write(0, 4, label='importance')
savePath = r'D:/' + fileName
workbook.save(savePath)
rexcel = xlrd.open_workbook(savePath)
rows = rexcel.sheets()[0].nrows
excel = copy(rexcel)
worksheet = excel.get_sheet(0)
current_row = rows
for cmp_noun, value in [[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9],[10,10],[11,11],[12,12],[13,13],[14,14]]:
if current_row<=5:
worksheet.write(current_row, 0, label=myyear)
worksheet.write(current_row, 1, label=myweek)
worksheet.write(current_row, 2, label=mycd)
worksheet.write(current_row, 3, label=cmp_noun)
worksheet.write(current_row, 4, label=value)
current_row += 1
excel.save(savePath)
如何修改代码成为我想要的效果?
我知道怎么做了。
代码:
from itertools import zip_longest
import xlwt
def generate_data():
year = '2018'
week = '10'
cd='1001'
for i in range(1, 14):
cmp_noun = value = i
yield (year, week, cd, cmp_noun, value)
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def generate_workbook(header, data, n=5):
groups = grouper(data, n)
workbook = xlwt.Workbook(encoding='UTF-8')
for i, group in enumerate(groups, 1):
worksheet = workbook.add_sheet(f'sheet{i}')
group = filter(None, group)
populate_sheet(worksheet=worksheet, header=header, data=group)
return workbook
def populate_sheet(worksheet, header, data):
for i, column in enumerate(header):
worksheet.write(0, i, column)
for row_no, row in enumerate(data, 1):
for i, item in enumerate(row):
worksheet.write(row_no, i, label=item)
if __name__ == '__main__':
name = r'D:/' + '201810.xls'
data = generate_data()
header = ['year', 'week', 'CD', 'keyword', 'importance']
wb = generate_workbook(header=header, data=data, n = 5)
wb.save(name)
现在我只能将14行数据全部保存到Sheet1。
我想把每 5 行放在一个 sheet 中,例如:
现在我的代码是这样的:
import xlwt
import xlrd
from xlutils.copy import copy
myyear = '2018'
myweek = '10'
mycd='1001'
fileName = myyear + myweek + '.xls'
workbook = xlwt.Workbook(encoding='UTF-8')
worksheet = workbook.add_sheet('sheet1')
worksheet.write(0, 0, label='year')
worksheet.write(0, 1, label='week')
worksheet.write(0, 2, label='CD')
worksheet.write(0, 3, label='keyword')
worksheet.write(0, 4, label='importance')
savePath = r'D:/' + fileName
workbook.save(savePath)
rexcel = xlrd.open_workbook(savePath)
rows = rexcel.sheets()[0].nrows
excel = copy(rexcel)
worksheet = excel.get_sheet(0)
current_row = rows
for cmp_noun, value in [[1,1],[2,2],[3,3],[4,4],[5,5],[6,6],[7,7],[8,8],[9,9],[10,10],[11,11],[12,12],[13,13],[14,14]]:
if current_row<=5:
worksheet.write(current_row, 0, label=myyear)
worksheet.write(current_row, 1, label=myweek)
worksheet.write(current_row, 2, label=mycd)
worksheet.write(current_row, 3, label=cmp_noun)
worksheet.write(current_row, 4, label=value)
current_row += 1
excel.save(savePath)
如何修改代码成为我想要的效果?
我知道怎么做了。 代码:
from itertools import zip_longest
import xlwt
def generate_data():
year = '2018'
week = '10'
cd='1001'
for i in range(1, 14):
cmp_noun = value = i
yield (year, week, cd, cmp_noun, value)
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
def generate_workbook(header, data, n=5):
groups = grouper(data, n)
workbook = xlwt.Workbook(encoding='UTF-8')
for i, group in enumerate(groups, 1):
worksheet = workbook.add_sheet(f'sheet{i}')
group = filter(None, group)
populate_sheet(worksheet=worksheet, header=header, data=group)
return workbook
def populate_sheet(worksheet, header, data):
for i, column in enumerate(header):
worksheet.write(0, i, column)
for row_no, row in enumerate(data, 1):
for i, item in enumerate(row):
worksheet.write(row_no, i, label=item)
if __name__ == '__main__':
name = r'D:/' + '201810.xls'
data = generate_data()
header = ['year', 'week', 'CD', 'keyword', 'importance']
wb = generate_workbook(header=header, data=data, n = 5)
wb.save(name)