简体   繁体   English

使用 openpyxl 将数据附加到新的 Excel 工作表中

[英]Append data into new excel sheet with openpyxl

I have bunch of excel workbooks and I would like to get cell values from them and write to a new sheet.我有一堆 excel 工作簿,我想从中获取单元格值并写入新工作表。

My code is not appending new data.It is just overwriting cells with values from last workbook.我的代码没有追加新数据。它只是用上一个工作簿中的值覆盖单元格。

(I've changed the pasted code.It was pasted incorrect.) (我已经更改了粘贴的代码。粘贴不正确。)

Here is my code这是我的代码

from openpyxl import load_workbook

booklist = ["17_02.xlsx", "17_03.xlsx",
        "17_04.xlsx", "17_05.xlsx",
        "17_06.xlsx", "17_08.xlsx",
        "17_09.xlsx", "17_10.xlsx"]


for wb in booklist:
    book = load_workbook(filename =wb,data_only=True)
    report = load_workbook(filename="dest.xlsx", data_only=True)
    print(book)
    sheet = book['Sheet']
    reportsheet=report['First']
    row_count=sheet.max_row
    column_count=sheet.max_column
    for r in range(1,row_count+1):
        for c in range(1,column_count+1):
           source=sheet.cell(row=r, column=c)
           dest = reportsheet.cell(row=r, column=c)
           dest.value = source.value
           sheet.title = 'First'
           book.save("dest.xlsx")

Edit:编辑:

After the mickNeill's answer I changed the code and it worked for appending.But now there is another problem.在米克尼尔的回答之后,我更改了代码,它可以用于追加。但现在还有另一个问题。 If I run the code (after clearing the cells) second time or more it's appending the data to the rows after the cleared cells.如果我第二次或更多次运行代码(在清除单元格之后),它会将数据附加到清除单元格之后的行。

First run: Data appended to A1:A20 Clear the cells,save and close the workbook.第一次运行:数据附加到 A1:A20 清除单元格,保存并关闭工作簿。

Second run: Data appended to A21:A20 instead of A1:A20 (cleared cells)第二次运行:数据附加到 A21:A20 而不是 A1:A20(清除单元格)

Every time I run the code value of the reportRow continues to increase (1,20,40 ...) and appending data to higher number of rows.每次运行时,reportRow 的代码值都会继续增加(1,20,40 ...)并将数据附加到更多行。

from openpyxl import load_workbook


booklist = ["17_02.xlsx", "17_03.xlsx",
    "17_04.xlsx", "17_05.xlsx",
    "17_06.xlsx", "17_08.xlsx",
    "17_09.xlsx", "17_10.xlsx"]


for wb in booklist:
    book = load_workbook(filename =wb,data_only=True)
    report = load_workbook(filename="dest.xlsx", data_only=True)
    print(book)
    sheet = book['Sheet']
    reportsheet=report['First']
    row_count=sheet.max_row
    reportRow = reportsheet.max_row
    column_count=sheet.max_column
    for r in range(1,row_count+2):
        for c in range(1,column_count+1):
            source=sheet.cell(row=r, column=c)
            dest = reportsheet.cell(row=reportRow, column=c)
            dest.value = source.value
        reportRow += 1
    report.save("dest.xlsx")

Try this: Editied, you are saving the wrong book, last line试试这个:编辑,你保存错了书,最后一行

from openpyxl import load_workbook

booklist = ["Book5.xlsx", "Book6.xlsx","Book7.xlsx"]

report = load_workbook(filename="dest.xlsx", data_only=True)
for wb in booklist:
    book = load_workbook(filename =wb,data_only=True)

    #print(book)
    sheet = book['Sheet1']
    reportsheet=report['First']
    row_count=sheet.max_row
    reportRow = reportsheet.max_row + 1
    print reportRow
    column_count=sheet.max_column
    for r in range(1,row_count+1):
        for c in range(1,column_count+1):
            print reportRow
            source=sheet.cell(row=r, column=c)
            dest = reportsheet.cell(row=reportRow, column=c)
            dest.value = source.value
            sheet.title = 'First' 
        reportRow += 1
report.save("dest.xlsx")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM