Openpyxl Filter and Sort Data




Filter Data

  • The auto_filter attribute is employed to line filtering and sorting conditions.
 Openpyxl Filter

Openpyxl Filter

Sample Code

from openpyxl import Workbook  
	
wb = Workbook()  
sheet = wb.active

sheet['A3'] = 40  
sheet['B3'] = 26  

row_count = [  
    (93,45),  
    (23,54),  
    (80,43),  
    (21,12),  
    (63,29),  
    (34,15),  
    (80,68),  
    (20,41)  
]  

for row in row_count:  
sheet.append(row)  

print(sheet.dimensions)  

for a1,a2 in sheet[sheet.dimensions]:  
print(a1.value, a2.value)  

sheet.auto_filter.add_sort_condition('B2:B8')  
sheet.auto_filter.add_filter_column(1, ['40', '26'])  

wb.save('dimension_1.xlsx')  

Output


Merge Data

  • We can merge the cell using the merge_cells() method. once we merge the cells, the top-left one is removed from the worksheet
  • The openpyxl also provides the unmerged_cells() method to unmerge the cell.
 Openpyxl Merger

Openpyxl Merge

Sample Code

from openpyxl.styles import Alignment  

wb = Workbook()  
sheet = wb.active

sheet.merge_cells('A1:B2')  

cell = sheet.cell(row=1, column=1)  
cell.value = 'Wikitechy'  
cell.alignment = Alignment(horizontal='center', vertical='center')  

wb.save('merging.xlsx')  

Output

Openpyxl Merge Output


Related Searches to Openpyxl Filter and Sort Data

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×