逐行从csv输入文件填充Excel文件 [英] Populate an excel file from csv input file line by line

查看:128
本文介绍了逐行从csv输入文件填充Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的csv文件,其中包含多行:

I have a csv file like this with multiple lines:

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

另一方面,我有一个excel文件,我想在其中将某些特定单元格上的某些csv逐行值填充到该文件中,例如:

In the other hand I have an excel file where I want to fill it with some of the csv line by line values on certain specific cells, for example:

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文件的下一行并保存新文件.

When finish columns/values per line, save a file and continue with the next line of the csv file and a new file saved.

我尝试了类似的操作:

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")

没有成功.怎么办呢?有什么建议吗?

Without success. How can do this? Any suggestion?

谢谢

推荐答案

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

I don't know python but I believe the logic should be something like that:

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")

这篇关于逐行从csv输入文件填充Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆