-1

I want to print the data in my excel file, segmenting it by cities names. For example I want to get all the columns for paris and put it in a new sheet, the columns for london and put it in another sheet etc. So far, i could only open the file and print the cities names. What is the method I should follow? So far I have:

# -*- coding: utf-8 -*-


import openpyxl as px
workbook = px.load_workbook('digital_trust.xlsx')
worksheet_names = workbook.get_sheet_names()

for worksheet_name in worksheet_names:
    worksheet = workbook.get_sheet_by_name(worksheet_name)

    for row in worksheet.iter_rows():
        ville = row[3].value        
        print ville
oezlem
  • 237
  • 2
  • 12

1 Answers1

0

I agree with @diek its kinda impossible to help if we don't know how your worksheet is setup.

But assuming your values per cities is in the worksheet range row[4]:row[??] you could create a dictionary of cities seen and their values.

cities_values = {}
for row in worksheet.iter_rows():
  if row[3].value in cities_values:
    cities_values[row[3].value].extend( row[4:??] ) #add upper limit
  else: 
    cities_values[row[3].value] = row[4:??] #add upper limit

#create worksheets based off keys
for k in cities_values:
  workbook.create_sheet(k) 
  workbook[k]["A1"] = cities_values[k] #store data in first cell
  workbook.save("digital_trust.xlsx")
Tony
  • 1,318
  • 1
  • 14
  • 36