pythonbits

Excel Data Sorting Using Python

Dec 30th, 2022
1,115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.53 KB | Source Code | 0 0
  1. # created by python bits.Check out my channel: https://www.youtube.com/@python-bits
  2.  
  3. import openpyxl
  4. from datetime import datetime
  5.  
  6. wb = openpyxl.load_workbook('Sample_data.xlsx')
  7. ws = wb.active
  8.  
  9. excel_data = []
  10. for row in ws.iter_rows(min_row=2, values_only=True):
  11.     excel_data.append(row)
  12.  
  13. #print(excel_data[0:5])
  14.  
  15. excel_data.sort(key=lambda row: datetime.strptime(row[2], '%m/%d/%Y'))
  16. print(excel_data[0:5])
  17.  
  18.  
  19. ws.delete_rows(2, ws.max_row-1)
  20.  
  21. for row in excel_data:
  22.     ws.append(row)
  23.  
  24. wb.save('final_output.xlsx')
Advertisement
Add Comment
Please, Sign In to add comment