【问题标题】:Openpyxl: Code fails to write data from one Excel spreadsheet to anotherOpenpyxl:代码无法将数据从一个 Excel 电子表格写入另一个
【发布时间】:2022-01-16 11:49:18
【问题描述】:

我正在尝试使用 Openpyxl 在一个 Excel 工作簿中搜索数据,并将其写入另一个(预先存在的)Excel 工作簿。目标是这样的:

  1. 在 Workbook1 中搜索包含单词“Sales”的行(有多个这样的行)
  2. 将这些行的 E 列中的数据(数值)复制到 Workbook2 中的特定单元格(第二个工作表,单元格 C3)。

我下面的代码似乎运行没有任何错误,但是,当我打开 Workbook 2 时,没有数据写入其中。有谁知道为什么/可以建议修复?

# importing openpyxl module
import openpyxl as xl
import sys

# opening the source excel file
filename ="C:\\Users\\hadam\\Documents\\Testing\\TB1.xlsx"
wb1 = xl.load_workbook(filename)
ws1 = wb1.worksheets[0]

# opening the destination excel file
filename1 = "C:\\Users\\hadam\\Documents\\Testing\\comp2.xlsx"
wb2 = xl.load_workbook(filename1)
ws2 = wb2.worksheets[1]

for sheet in wb1.worksheets:
    for row in sheet.iter_rows():
        for cell in row:
            try:
                if 'Sale' in cell.value:
                    # reading cell value from source excel file
                    c = ws1.cell(row=cell.row, column=4).value

                    # writing the read value to destination excel file
                    ws2.cell(row=2, column=2).value = c.value
            
            except (AttributeError, TypeError):
                continue

            # saving the destination excel file
            wb2.save(str(filename1))
                
sys.exit()

其他信息:

  • 具体来说,我正在搜索的文本字符串 ('Sales') 位于 Workbook1 的 A 列中。这不是完全匹配,但例如给定单元格包含“5301 销售 - 国内 - type4”。因此,我想将 E 列中包含 A 列中“销售额”的数值汇总到 Workbook2 中的单个单元格中。
  • 我对 Python/编码非常陌生。但是,我的环境似乎设置得很好,例如我已经测试了一个代码(从网络上的其他地方复制),它可以将一个电子表格中的所有数据写入另一个预先存在的电子表格)。我在 Python 3 模式和 openpyxl 模块中使用 Mu 编辑器。

【问题讨论】:

    标签: python excel cell openpyxl rows


    【解决方案1】:

    线

    c = ws1.cell(row=cell.row, column=4).value
    

    将 Col 4 中的值复制到变量“c”。 'column=4' 是电子表格中的 D 列,您希望它是 'column=5' 以从 E 列获取值。如果 D 列为空,则 c 每次都没有值。

    在您的代码中,c 等于 E 列中与您的搜索条件匹配的值,目前它将在循环的每次迭代中被覆盖。要对这些值求和,您必须使用 '+=' 为每次迭代将每个新值添加到 c 的现有值中。

    没有必要为每个循环更新第二本书单元格的值,您只需要写入结果总和,以便在总和完成后完成这些步骤 在循环完成时计算。

    请参阅下面修改您的代码部分的代码;

    初始化c

    c = 0
    

    更改行,以便 c 为每个循环匹配对 Col E 中的值求和

    c += ws1.cell(row=cell.row, column=5).value
    

    然后将单元格值更新并保存到与循环相同的缩进处的 excel book 中,以便在循环完成后执行。

    请注意,c 是一个 python 整数,它没有称为 value 的属性,它只是 'c'。使用 'c.value' 会给出一个空白值。此外,您要写入此值的单元格 C3 是 row/col 3

    ws2.cell(row=3, column=3).value = c
    

    最后将单元格保存到第二本excel书

    修改后的代码;

    ...
    c = 0
    for sheet in wb1.worksheets:
        for row in sheet.iter_rows():
            for cell in row:
                try:
                    if 'Sale' in cell.value:
                        # reading cell value from source excel file
                        c += ws1.cell(row=cell.row, column=5).value
    
                except (AttributeError, TypeError):
                    continue
    
    # writing the read value to destination excel file
    ws2.cell(row=3, column=3).value = c
    # saving the destination excel file
    wb2.save(str(filename1))
    ...
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2011-12-14
    • 2019-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-31
    相关资源
    最近更新 更多