繁体   English   中英

尝试使用 python 和 openpyxl 在电子表格中查找非空单元格

[英]Trying to find non-empty cells in a spreadsheet usin python and openpyxl

我有一个 excel 电子表格,其中包含一些空单元格和一些非空单元格。

我想,对于一组行和列,打印出非空值,然后将它们复制到一个新的电子表格中,逐行逐行复制到另一列下方......

部分代码:

sheet = book.active
cells = sheet['A1': 'B6']
for c1 in cells:
    if c1.value:
       print(c1.value)

它返回'元组没有属性'值'。

这是为什么? 我怎样才能做到这一点?

您的问题是代码中的c1不是单元格,实际上它是一行。 您可以通过这种方式修复您的代码;

sheet=book.active
cells = sheet['A1':'B6']
for row in cells:
    for cell in row:
        if cell.value:
            print(cell.value)

暂无
暂无

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

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