Python3 openpyxl将包含特定值的行中的数据复制到现有工作簿中的新工作表

时间:2017-12-21 05:00:50

标签: python-3.x openpyxl

我正在尝试将具有特定值的行复制到新工作表中。在查看下面列出的参考资料时,我能够确定如何使用ws.append进行复制,但是在脚本中无法正常运行。我想要做的是迭代wb,如果它有一定值,则创建另一个工作表并将该行复制到所述工作表。由于我没有产生预期的结果,我将不胜感激。我唯一要做的就是添加一些elif条件以及其他条件,但它不会遍历工作表并复制预期的结果。它正在为每个条件复制其中一行而不是目标行,并将其复制到一个与ws不同的新文件中,而不是在条件中创建的新工作表。

from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.utils import range_boundaries
from sys import argv
script, inpath, outpath = argv

min_col, min_row, max_col, max_row = range_boundaries("A:M")
wb = load_workbook(inpath)
print("File Loaded")
ws = wb.get_sheet_by_name("value")

check = 0 # == A

for row in ws.iter_rows():
    if row[check].value == 'value':
        try:
            ws1 = wb.create_sheet("value", 1)
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
            ws1.append((cell.value for cell in row[min_col-1:max_col]))
        except:
            print("words")
    elif row[check].value == 'value':
        try:
            ws2 = wb.create_sheet("value", 2)
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
            ws2.append((cell.value for cell in row[min_col-1:max_col]))
        except:
            print("moar words")

    elif row[check].value == 'value':
        try:
            ws3 = wb.create_sheet("value", 3)
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
            ws3.append((cell.value for cell in row[min_col-1:max_col]))
        except:
            print("words")
    else:
        try:
            ws4 = wb.create_sheet("value", 4)
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
            ws4.append((cell.value for cell in row[min_col-1:max_col]))
        except:
            print("words")


wb.save(outpath + '.xlsx')

如果我运行以下

for row in ws.iter_rows():
    if row[check].value == 'Info':

    try:
        #ws1.cell(row=1, column=1).value = ws1.cell(row=1, column=1).value
        ws1 = wb.create_sheet("Info", 1)
        ws1 = wb.active
    # Copy Row Values
    # We deal with Tuple Index 0 Based, so min_col must have to be -1
        ws1.append((cell.value for cell in row[min_col-1:max_col]))
    except:
        print("Words")



wb.save(outpath + '.xlsx')

它运行我需要的查询,在这种情况下,每行中都包含Info,但它不会创建新工作表,也不会将值附加到新工作表。它与原始页面保持相同的工作表名称。添加elif或else语句不能正常运行。当这一切都在一起运行。它只是创建了大量的工作表,标题信息和原始excel文件中的行号。我已将if语句中的值更改为除info之外的其他值,它仍然输出包含Info的行。

参考:Openpyxl: How to copy a row after checking if a cell contains specific value

1 个答案:

答案 0 :(得分:1)

因此,通过与同事的讨论,我能够发现问题是在for循环中创建工作表,为每个具有指定值的单元格创建了工作表。以下内容已经过测试并且运行良好。

from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.utils import range_boundaries
from sys import argv
script, inpath, outpath = argv

min_col, min_row, max_col, max_row = range_boundaries("A:M")
wb = load_workbook(inpath)
print("File Loaded")
ws = wb.get_sheet_by_name("Report")

interested_column = 6 # == G

ws1 = wb.create_sheet("Value", 1)
ws2 = wb.create_sheet("Value", 2)
ws3 = wb.create_sheet("Value", 3)
ws4 = wb.create_sheet("Value", 4)

for row in ws.iter_rows():
    if row[interested_column].value == 'Value':
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
        ws1.append((cell.value for cell in row[min_col-1:max_col]))
    elif row[interested_column].value == 'Value':
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
        ws2.append((cell.value for cell in row[min_col-1:max_col]))
    elif row[interested_column].value == 'Value':
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
        ws3.append((cell.value for cell in row[min_col-1:max_col]))
    else:
        # Copy Row Values
        # We deal with Tuple Index 0 Based, so min_col must have to be -1
        ws4.append((cell.value for cell in row[min_col-1:max_col]))

wb.save(outpath + '.xlsx')