【问题标题】:Populate an excel file from csv input file line by line从csv输入文件逐行填充一个excel文件
【发布时间】:2020-05-23 02:51:48
【问题描述】:

我有一个这样的多行 csv 文件:

column1,column2,column3,column4,colunm5,column6
valueA,valueB,valueC,valueD,valueE,valueD
valueA,valueB,valueC,valueD,valueE,valueD
...

另一方面,我有一个 excel 文件,我想用某些特定单元格上的一些 csv 值逐行填充它,例如:

column1 and valueA into the excel file in the cell D7.
column2 and valueB into the excel file in the cell E8.
column3 and valueC into the excel file in the cell F8.
column4 and valueC into the excel file in the cell G8.
column5 and valueC into the excel file in the cell H8.
column6 and valueC into the excel file in the cell I8.

当完成每行的列/值时,保存一个文件并继续 csv 文件的下一行并保存一个新文件。

我尝试过类似的东西:

from openpyxl import load_workbook
import csv

wb = load_workbook('templatefile.xlsx')
ws = wb.get_active_sheet()
with open('source.csv') as fin:
    reader = csv.reader(fin, delimiter=',')
        row = row[0].split()
        ws.cell(row=index, column=1).value = row[2]
        ws.cell(row=index, column=2).value = row[3]
# save the file
wb.save("out1.xlsx")

没有成功。怎么能做到这一点?有什么建议吗?

谢谢,

【问题讨论】:

  • 索引在哪里?您需要在循环内声明索引和增加。
  • 索引 = 1;行=索引++
  • 谢谢。你能给我看一个实现示例吗?

标签: python python-3.x list openpyxl


【解决方案1】:

我不懂python,但我相信逻辑应该是这样的:

wb = load_workbook('templatefile.xlsx')
ws = wb.get_active_sheet()
i = 1
with open('source.csv') as fin:
    reader = csv.reader(fin, delimiter=',')
        row = row[0].split()
        ws.cell(row[i], column=1).value = row[2]
        ws.cell(row=[i], column=2).value = row[3]
        i = i+1
# save the file
wb.save("out1.xlsx")

【讨论】:

  • 还不行,不过,谢谢你的帮助。
猜你喜欢
  • 2019-08-16
  • 2019-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-12
  • 2019-07-23
  • 2013-05-12
  • 2013-11-25
相关资源
最近更新 更多