Python, with its extensive libraries such as Pandas and OpenPyXL, provides robust tools for handling Excel files, enabling efficient data manipulation and analysis. This article discusses how to loop through an Excel sheet using Python, extract data, and format the output with a specific structure: title, content, and tags.
To start, ensure you have Pandas installed in your Python environment. If not, you can install it using pip:
bashCopy Codepip install pandas
Let’s assume we have an Excel file named data.xlsx
with multiple sheets, and each sheet contains data in columns titled “Title”, “Content”, and “Tags”. Our goal is to loop through each sheet, read the data, and print it in a formatted manner.
Here’s how you can achieve this:
pythonCopy Code
import pandas as pd
# Load the Excel file
file_path = 'data.xlsx'
xlsx = pd.ExcelFile(file_path)
# Loop through each sheet in the Excel file
for sheet_name in xlsx.sheet_names:
# Read the sheet into a DataFrame
df = pd.read_excel(xlsx, sheet_name=sheet_name)
# Iterate through each row in the DataFrame
for index, row in df.iterrows():
title = row['Title']
content = row['Content']
tags = row['Tags']
# Print the formatted output
print(f"[title] {title}")
print(f"
As I write this, the latest version of Python is 3.12.4