openpyxl从特定的列/单元格开始写入

时间:2019-03-06 13:18:28

标签: python openpyxl xlsx

我有以下代码:

ws = wb.worksheets[1]
print(ws)
with open('out.txt', 'r+') as data:
    reader = csv.reader(data, delimiter='\t')
    for row in reader:
    print(row)
    ws.append(row)
wb.save('test.xlsx')

默认情况下,它会从A0开始写入xlsx文件

是否有更方便的方法开始添加数据(例如从C2开始)? 还是只有xxx.cell(row=xx , column=yy ).value=zz

i = 2
j = 3

with open('out.txt', 'r+') as data:
    reader = list(csv.reader(data, delimiter='\t'))
for row in reader:
    for element in row:
        ws.cell(row=i, column=j).value = element
        j += 1
    j = 3
    i += 1

1 个答案:

答案 0 :(得分:0)

只需用None s填充行

ws.append([]) # move to row 2
for row in reader:
   row = (None)*2 + row
   ws.append(row)