In Excel, names are often written in various formats, but sometimes it’s necessary to standardize them. One common standard is to capitalize only the first letter of each name and make the rest lowercase. This ensures consistency and readability in datasets.
Python provides tools like the openpyxl library to automate Excel tasks. With Python, you can write a script that reads a list of names from an Excel file, converts them to the desired format, and saves the modified names back to the Excel file. This automation process can save time and reduce errors when working with large datasets.
In this tutorial, we’ll use Python to automate the task of capitalizing the first letter of each name and making the rest lowercase in an Excel file. We’ll use the openpyxl library to read the names from the Excel file, modify them, and save the modified names back to the Excel file.
By the end of this tutorial, you’ll have a Python script that can easily handle this task, making it easier to maintain consistency in your Excel datasets.
Original Excel(names.xlsx)

from openpyxl import load_workbook
# Load the Excel workbook
workbook = load_workbook('names.xlsx')
sheet = workbook.active
# Track the current row number
current_row = 2
# Iterate over the names in the sheet and capitalize the first letter
for row in sheet.iter_rows(min_row=2, min_col=1, max_col=1, values_only=True):
name = row[0]
if name:
# Lowercase all letters and then capitalize the first letter
modified_name = name.lower().capitalize()
sheet.cell(row=current_row, column=2, value=modified_name)
current_row += 1 # Move to the next row
# Save the modified workbook
workbook.save('names_modified.xlsx')
Output:
Explanation of the code above:
This code uses the openpyxl library in Python to load an Excel workbook (names.xlsx), iterate over the names in the first column of the active sheet, and modify each name to have the first letter capitalized and the rest in lowercase. It then saves the modified names to a new Excel workbook (names_modified.xlsx).
- Load the Excel Workbook: The code starts by importing the
load_workbookfunction from theopenpyxllibrary and loading thenames.xlsxworkbook into a variable calledworkbook. - Get the Active Sheet: It then gets the active sheet of the workbook and stores it in a variable called
sheet. - Track the Current Row Number: The code initializes a variable called
current_rowto 2. This variable will be used to keep track of the current row number while iterating over the names in the Excel sheet. - Iterate Over the Names: The code uses a
forloop and theiter_rowsmethod of the sheet object to iterate over the rows of the first column (min_col=1, max_col=1) starting from the second row (min_row=2). Thevalues_only=Trueargument ensures that only the cell values are returned, not the cell objects. - Modify the Name: For each row, the code retrieves the name from the first column (row[0]) and checks if it is not empty (
if name:). It then converts the name to lowercase and capitalizes the first letter using thecapitalize()method. - Update the Cell: The code updates the cell in the second column (column=2) of the current row (
current_row) with the modified name. - Move to the Next Row: After updating the cell, the code increments the
current_rowvariable to move to the next row. - Save the Modified Workbook: Finally, the code saves the modified workbook to a new Excel file called
names_modified.xlsx.
In conclusion, using Python for Excel automation tasks, such as standardizing names by capitalizing the first letter and making the rest lowercase, can greatly improve efficiency and accuracy. By leveraging libraries like openpyxl, you can quickly process large datasets, ensuring consistency and readability in your Excel files. This approach not only saves time but also reduces the likelihood of manual errors, making it a valuable tool for data management tasks.





Leave a Reply