interate through a column while skipping a header

1,614 views
Skip to first unread message

Brice Drilling

unread,
Sep 29, 2015, 2:30:58 PM9/29/15
to openpyxl-users
I am needing to iterate (loop) through a column of cells but would like to skip the first row.  to try and accomplish this I have the code snippit below...but with some errors:
 

jobs_found
= 0
if (customer is not None) and (office is not None) and (rig is not None):
   
for sheet in sheet_list:
        wb2
= openpyxl.load_workbook(sheet)
        wb2_sheet1
= wb2.get_sheet_by_name(wb2.sheetnames[2])
        print(sheet, 'started at: ', time.strftime("%c"))
       
for hs in wb2_sheet1.iter_rows('A2:A' + str(wb2_sheet1.get_highest_row())):
           
for cell in hs:
                wb1cell
= 0
               
if (str.lower(cell.internal_value) == str.lower(office)) and (str.lower(cell.offset(row = 0, column = 2).internal_value) == str.lower(customer)) and (str.lower(cell.offset(row = 0, column = 7).internal_value) =                                      = str.lower(rig)):
                    wb1cell
+= 1
                    for ncolumn in range(1,32):
                        wb1_sheet1
.cell(row=wb1cell, column = ncolumn).value = cell.offset(row=0, column=ncolumn)
                       
print(wb1_sheet1.cell(row=wb1cell, column = ncolumn).value)

               
break

 
while this accomplishes what I am trying to on the looping through a column while skipping the header, I feel like it could be simplified.  I have used the column[1] call before and just manually removed the first row and add it back later.
 
I am getting an error after the for ncolumn (to copy the cells to the new sheet) I get a "<Cell Hole Section Data - August.B2> to Excel" ValueError.
 
 
version 2.2.6

Charlie Clark

unread,
Sep 29, 2015, 2:37:23 PM9/29/15
to openpyx...@googlegroups.com
Am .09.2015, 20:30 Uhr, schrieb Brice Drilling <bigre...@gmail.com>:

> for hs in wb2_sheet1.iter_rows('A2:A' +
> str(wb2_sheet1.get_highest_row())):

Why do people insist on trying to calculate column labels themselves? What
docs are people looking at?

Use "A1" notation for adhoc, interactive queries or where a range string
is required. Use row=x, col=y notation for programmatic access.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226
Message has been deleted

Brice Drilling

unread,
Sep 29, 2015, 2:48:16 PM9/29/15
to openpyxl-users
So stick to columns and remove the header lol ha
 
thanks
Auto Generated Inline Image 1
Auto Generated Inline Image 2
Reply all
Reply to author
Forward
0 new messages