I remember an instance from my office, there was a problem that I saw. Every time a new employee joined the team, the office manager had to come up with a unique password for them to use. This was a tedious task, and sometimes the passwords weren’t very secure.
That day, I decided to try using Python to automate this procedure and help my office manager. I stood up to the manager and told him about what magic Python can do. With Python’s help, the office manager learned how to automate the process of generating passwords for employees in Excel. They no longer had to spend hours thinking up passwords or spend time and money on some automated software – Python could do it for them!
Using Python’s magic, I created a script for the office manager that read the list of employee names from an Excel file. Then, like a wizard casting a spell, Python conjured up random passwords for each employee, making sure they were strong and secure.
With a wave of Python’s wand, the passwords were added to the Excel file next to each employee’s name. Then office manager saved the file and opened it, marveling at how quickly and effortlessly Python had solved their problem.
From that day forward, generating employee passwords was no longer a daunting task. Thanks to Python’s automation magic, the office manager could focus on more important matters, like brewing the perfect cup of coffee for the team. And they all lived happily ever after, with secure passwords and efficient workflows, thanks to Python’s enchanting powers.
Now let me take you all through the Python script and how it becomes a magic spell
Step 1: We will first take the predefined names of some employees and then generate random passwords for them in Excel automatically.
import openpyxl
import random
import string
# Create a workbook and select the active sheet
workbook = openpyxl.Workbook()
sheet = workbook.active
# Indian employee names
employee_names = [
"Ramesh Kumar",
"Priya Patel",
"Amit Singh",
"Divya Sharma",
"Rajesh Gupta",
"Anjali Verma",
"Sanjay Tiwari",
"Neha Mishra",
"Vivek Malhotra",
"Pooja Choudhary"
]
# Fill employee names in column A with 'Employee' as header
sheet['A1'] = 'Employee'
sheet['B1'] = 'Passwords'
for index, name in enumerate(employee_names, start=2):
sheet.cell(row=index, column=1).value = name
# Function to generate passwords for each employee
def generate_passwords():
for row in range(2, sheet.max_row + 1):
password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
sheet.cell(row=row, column=2).value = password
# Call the function to generate passwords
generate_passwords()
# Save the workbook
workbook.save("employee_names.xlsx")
# Open the Excel file for editing
import os
os.system("start employee_names.xlsx")
Output:
Step 2: Now that you have seen how can you generate a random password for a predefined employee name. Next let’s generate 100 employee names automatically with Python in the Excel sheet and then after that add random passwords to them, by just reading the Excel sheet which has 100 employee names.
Code to Generate 100 Random Employee Names
Note: You can read your own Excel sheet if you have one, instead of making a Excel sheet with employee’s name
import openpyxl
import random
# Indian first names and last names
first_names = ["Ramesh", "Priya", "Amit", "Divya", "Rajesh", "Anjali", "Sanjay", "Neha", "Vivek", "Pooja"]
last_names = ["Kumar", "Patel", "Singh", "Sharma", "Gupta", "Verma", "Tiwari", "Mishra", "Malhotra", "Choudhary"]
# Create a workbook and select the active sheet
workbook = openpyxl.Workbook()
sheet = workbook.active
# Set the column header
sheet['A1'] = 'Employee'
# Generate and store 100 random Indian employee names
for i in range(2, 102): # Start from row 2, end at row 101
first_name = random.choice(first_names)
last_name = random.choice(last_names)
full_name = f"{first_name} {last_name}"
sheet[f'A{i}'] = full_name
# Save the workbook
workbook.save("Indian_employee_names.xlsx")
Output:
Now let’s go for the code to generate a random password for the above Excel sheet created with 100 random employee names by reading the Excel sheet name
import openpyxl
import random
import string
# Load the existing workbook
workbook = openpyxl.load_workbook("Indian_employee_names.xlsx")
sheet = workbook.active
# Function to generate passwords for each employee
def generate_passwords():
for row in range(2, sheet.max_row + 1):
password = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
sheet.cell(row=row, column=2).value = password
# Call the function to generate passwords
generate_passwords()
# Save the workbook
workbook.save("employee_names_with_passwords.xlsx")
# Open the Excel file for editing
import os
os.system("start employee_names_with_passwords.xlsx")
Output:
Explanation of the code above:
import openpyxl: This line imports theopenpyxllibrary, which is a Python library for reading and writing Excel files.import random: This line imports therandommodule, which provides functions for generating random numbers and selecting random elements from sequences.import string: This line imports thestringmodule, which contains a collection of string constants and utility functions.workbook = openpyxl.load_workbook("Indian_employee_names.xlsx"): This line loads an existing Excel workbook named “Indian_employee_names.xlsx” into memory and assigns it to the variableworkbook. Theload_workbookfunction is provided by theopenpyxllibrary and is used to open an existing Excel file.sheet = workbook.active: This line selects the active sheet (i.e., the sheet currently visible or selected) from the loaded workbook and assigns it to the variablesheet. Theactiveattribute of theworkbookobject returns a reference to the active sheet.def generate_passwords():: This line defines a function namedgenerate_passwords. This function will generate passwords for each employee in the Excel sheet.for row in range(2, sheet.max_row + 1):: This line starts a loop that iterates over each row in the Excel sheet, starting from the second row (since the first row typically contains headers) up to the last row (max_rowattribute returns the number of rows in the sheet).password = ''.join(random.choices(string.ascii_letters + string.digits, k=8)): This line generates a random password consisting of 8 characters. It uses therandom.choicesfunction to select characters randomly from a combination of uppercase letters, lowercase letters, and digits. Thestring.ascii_lettersconstant represents all uppercase and lowercase letters, andstring.digitsrepresents all digits. Thejoinfunction concatenates these characters into a single string.sheet.cell(row=row, column=2).value = password: This line assigns the generated password to the cell in the second column (column B) of the current row in the Excel sheet.generate_passwords(): This line calls thegenerate_passwordsfunction, which generates passwords for each employee in the Excel sheet.workbook.save("employee_names_with_passwords.xlsx"): This line saves the modified workbook (with passwords added) to a new Excel file named “employee_names_with_passwords.xlsx”. Thesavemethod of theworkbookobject is used to save the changes.import os: This line imports theosmodule, which provides a way to interact with the operating system.os.system("start employee_names_with_passwords.xlsx"): This line uses theos.systemfunction to execute a command in the operating system. Here, it opens the Excel file “employee_names_with_passwords.xlsx” for editing. Thestartcommand is used on Windows to open a file with its default application.
In conclusion, this Python script demonstrates a streamlined approach to generating random passwords for employees stored in an Excel spreadsheet. By leveraging the openpyxl library for Excel manipulation and Python’s built-in modules random and string for password generation, the process becomes automated and efficient.
The script first loads an existing Excel workbook containing employee information. It then iterates over each row, generating a random password for each employee using a combination of uppercase letters, lowercase letters, and digits. These passwords are then inserted into the second column of the Excel sheet.
Once all passwords have been generated and added to the Excel file, the modified workbook is saved under a new name. Lastly, the script opens the Excel file, allowing users to review the updated employee data with the newly generated passwords.
By automating the password generation process, this Python script saves time and ensures the security of employee accounts by creating strong, random passwords. It serves as an example of how Python automation can streamline tasks and enhance productivity in various workplace scenarios.





Leave a Reply