2021-07-27

select multiple columns using openpyxl

I am new to python and trying to read excel file using openpyxl.

My Excel file has 20 columns and I need to only extract the 5 columns .

My Excel file

      CustNumber CustName Serial Number CreateDate ModifiedDate Organization CustAddress Phone
         1       XYZ       1010         01-01-2021   01-10-2021    test1     101 parklane  234
         2       ABC       1012         01-01-2021   01-10-2021    test2     102 texchlane 234
         3       CDF        1010         01-01-2021   01-10-2021   test1     101 parklane  234
         4       ASC       1012         01-01-2021   01-10-2021    test2     102 texchlane 234

Output

   CustNumber CustName              CreateDate ModifiedDate Organization    CustAddress 
         1       XYZ                01-01-2021   01-10-2021    test1     101 parklane  
         3       CDF                01-01-2021   01-10-2021    test1     101 parklane  

I need to select some columns from the excel file and filter the records in the excel file where Organization = test1.

I want to do it in openpyxl not in pandas.I am able to read one column but not sure how can I read multiple columns and then filter the file to extract only test1 records.

My code

  import openpyxl
  book = openpyxl.load_workbook('Book1.xlsx')
  sheet = book['SSH_CERT']
  column_name = 'Description'
  for column_cell in sheet.iter_cols(1, sheet.max_column): 
  if column_cell[0].value == column_name:    
   j = 0
    for data in column_cell[1:]:   
        print(data.value)
    break
     
     

Thanks



from Recent Questions - Stack Overflow https://ift.tt/36ZzlMT
https://ift.tt/eA8V8J

No comments:

Post a Comment