Home > Blockchain >  How to update cells in a spreadsheet without the values on the list repeating on openpyxl
How to update cells in a spreadsheet without the values on the list repeating on openpyxl

Time:02-08

I'm new to programming so sorry the silly question.

I have been trying to develop a spreadsheet on excel that updates the duties of staff members from a certain list.

For example I have this code:

senior_duties = ['SOPs', 'EQA review', 'Doc review', 'NC review', 'Trend analysis']

senior_duties1 = random.choice(senior_duties)

#Staff member 1

sheet["D5"] = senior_duties1
sheet["D6"] = senior_duties1

#Staff member 2

sheet["E5"] = senior_duties1
sheet["E6"] = senior_duties1

I understand that this only prints random values from the list that some times repeat and duplicate between two staff members.

I want the code to print something new for each staff member from the list every time the script is run. I'm sure the solution involves a 'for' loop but I just can't seem to solve it.

Any help will be appreciated!

CodePudding user response:

To get a different outcome for each person, you'd need to call random_choice again:

senior_duties = ['SOPs', 'EQA review', 'Doc review', 'NC review', 'Trend analysis']

#Staff member 1
sheet["D5"] = random.choice(senior_duties)
sheet["D6"] = random.choice(senior_duties)

#Staff member 2
sheet["E5"] = random.choice(senior_duties)
sheet["E6"] = random.choice(senior_duties)

Or if it's the same choice for each staff member:

senior_duties = ['SOPs', 'EQA review', 'Doc review', 'NC review', 'Trend analysis']

#Staff member 1
duty_1 = random.choice(senior_duties)
sheet["D5"] = duty_1
sheet["D6"] = duty_1

#Staff member 2
duty_2 = random.choice(senior_duties)
sheet["E5"] = duty_2
sheet["E6"] = duty_2

CodePudding user response:

If you want to randomly assign the duties to each staff member, just shuffle the list and assign the duties from the list.

import random

senior_duties = ['SOPs', 'EQA review', 'Doc review', 'NC review', 'Trend analysis']

random.shuffle(senior_duties)

# Staff member 1

sheet["D5"] = senior_duties[0]
sheet["D6"] = senior_duties[0]

# Staff member 2

sheet["E5"] = senior_duties[1]
sheet["E6"] = senior_duties[1]

This will give you a random duty assignment every time you run the script, however if you want an assignment that has not been done yet, you will need to add further logic

  •  Tags:  
  • Related