I am attempting to copy a row with a certain value in it to a new sheet. In viewing the reference listed below I was able to identify how to copy with ws.append however that is not functioning properly in the script. What I would like to do is iterate over the wb and if it has a certain value, create another sheet and copy that row to said sheet. I would appreciate any assistance on this as I am not producing the expected results. The only thing I am doing are adding some elif conditions as well as a else but it is not iterating through the worksheet and copying over the intended results. It is copying one of the rows but not the intended row for each conditional and copying it to a new file with the same name as ws not the new sheet that is being created in the conditional.
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')
If I run the following
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')
It runs the query I need, in this case every row that has the word Info in it, but it does not create a new sheet and it doesn't append the values to the new sheet. It keeps the same sheet name as the original. Adding the elif or else statements dont function properly. When that is all run together. It just creates tons of sheets with the title Info and the row number in the original excel file. I have changed the value in the if statement to something other than info and it still outputs the rows that contain Info.
Reference: Openpyxl: How to copy a row after checking if a cell contains specific value
So through a discussion with a co-worker I was able to find out the that issue was creating the worksheets in the for loop created a worksheet for every cell that had the specified value. The following has been tested and works perfectly.