在每条记录后按行而不是按列插入数据和 1 个空白行

Insert data row-wise instead of column-wise and 1 blank row after each record

这是我的代码:

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

并且还在每行之间插入 1 个空行,如下所示:

      A
1     this
2
3     that
4
5     what
6
7     is
8
9     this

我正在尝试更改我的代码以满足要求。如果我找到解决方案,我也会 post 我的代码作为答案。

编辑: 好的,我已经通过修改 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)

你为什么要写这么复杂的东西?

for v in th_list:
   ws.append([v]) # pad as necessary, never encode
   ws.append() # blank row

我自己找到了两个问题的解决方案。这是:

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)