这是我的代码:
wb = Workbook()
dest_filename = 'book.xlsx'
th_list = ["this", "that", "what", "is", "this"]
ws1 = wb.active
ws1.title = 'what'
for row in range(1, 2):
for col in range(1, len(th_list)):
_ = ws1.cell(column=col, row=row, value="{0}".format(th_list[col].encode('utf-8')))
wb.save(filename = dest_filename)
运行py文件后,我以这种方式获取数据:
A B C D E
1 this that what is this
虽然我想要这样的数据:
A
1 this
2 that
3 what
4 is
5 this
并在每行之间插入一个空行,如下所示:
A
1 this
2
3 that
4
5 what
6
7 is
8
9 this
我正在尝试更改我的代码以满足要求。如果我找到解决方案,我也会将我的代码发布为答案。
编辑:好的我已经成功地将数据从行方式转换为列式,并修改了for循环。但仍然无法在每条记录后添加空行。这是代码:
wb = Workbook()
dest_filename = 'book.xlsx'
th_list = ["this", "that", "what", "is", "this"]
ws1 = wb.active
ws1.title = 'what'
for col in range(1, 2):
for row in range(1, len(th_list)+1):
_ = ws1.cell(column=col, row=row, value="{0}".format(th_list[row-1].encode('utf-8')))
wb.save(filename = dest_filename)
答案 0 :(得分:0)
你为什么写一些如此复杂的东西?
for v in th_list:
ws.append([v]) # pad as necessary, never encode
ws.append() # blank row
答案 1 :(得分:0)
我自己找到了解决这两个问题的方法。这是:
wb = Workbook()
dest_filename = 'book.xlsx'
th_list = ["this", "that", "what", "is", "this"]
ws1 = wb.active
ws1.title = 'what'
for col in range(1, 2):
for row in range(1, len(th_list)+1):
_ = ws1.cell(column=col, row=(row*2)-1, value="{0}".format(th_list[row-1].encode('utf-8')))
wb.save(filename = dest_filename)