【问题标题】:How to pick specifics cells in a xlsx file?如何选择 xlsx 文件中的特定单元格?
【发布时间】:2021-08-30 09:28:12
【问题描述】:

我有一些看起来像这样的 excel 文件:

我以前从未在 Python 中读取过这样的文件,所以我有点迷茫。

所以当我导入其中一个时,它看起来像这样:

我无法使用它。

您知道正确导入这些数据的一些技巧吗?

我的目标是对所有文件求和,例如在 E16 中拥有一个最终文件 -> 文件 1 的 E16 + 文件 2 的 E16 +...+文件 n 的 E16。

对于每个包含数字的单元格,依此类推。

但我无法总结这个混乱......有什么想法可以更有效地做到这一点吗?

【问题讨论】:

    标签: python excel import


    【解决方案1】:

    我认为这可能会对您有所帮助:

    # import openpyxl module
    import openpyxl
     
    # Call a Workbook() function of openpyxl
    # to create a new blank Workbook object
    wb = openpyxl.Workbook()
     
    # Get workbook active sheet 
    # from the active attribute.
    sheet = wb.active
     
    # writing to the cell of an excel sheet
    sheet['A1'] = 200
    sheet['A2'] = 300
    sheet['A3'] = 400
    sheet['A4'] = 500
    sheet['A5'] = 600
     
    # The value in cell A7 is set to a formula
    # that sums the values in A1, A2, A3, A4, A5 .
    sheet['A7'] = '= SUM(A1:A5)'
     
    # save the file
    wb.save("sum.xlsx")
    

    【讨论】:

    • 我的值在不同的文件中,这也行吗?因为在这里,您似乎手动创建了您的数据,我的数据已经存在,在不同的文件中。
    • 是的,例如 sheet['A1'] = sheet['A1'] ].value 这里的值将是您的现有数据值
    • 是的,我明白了,我会尝试并与您保持联系,谢谢先生!
    • 如何告诉 sheet['A1'] = 文件 1 中第 15 行和第 3 列的值?当我们导入数据框时没有工作表所以..
    • 行 = 15 列 = 1 打印(sheet.cell(row, column).value)
    猜你喜欢
    • 2023-01-25
    • 2018-08-15
    • 2011-01-14
    • 2013-09-02
    • 2021-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多