Recently, while handling a bunch of data analysis tasks, I was struggling with Excel files every day. I wrote a lot of pandas code and had to remember all those strange function usages. By chance, I discovered the SWD library for Python, and I was amazed after trying it out. Not only can it handle 90% of daily data processing tasks, but the code is also simple enough for my mom to understand.
Originally, processing an Excel file required writing several lines of code just to read it, taking into account various data type conversions. After using SWD, I could accomplish it in one line of code, and it handled the format conversion for me. The processed data is directly in Python dictionary format, eliminating the need to adapt to a new data structure.
Installation and Basic Usage
Installing this library is super easy; just one line of code:
pip install swd
It’s also very easy to use; take a look at the code below:
from swd import Excel
# Read Excel file
excel = Excel('sales_data.xlsx')
# Get all sheet names
sheets = excel.sheet_names
print(sheets) # ['Sales Data', 'Customer Info']
# Read data from a specific sheet
data = excel.read('Sales Data')
Tip: This library defaults to reading Excel data in dictionary format, which is more intuitive than pandas. You don’t have to memorize so many complex methods; you can directly use basic Python dictionary operations to process data.
To be honest, at first, I was skeptical, thinking, can such a simple library handle complex Excel operations? But after trying it, I was pleasantly surprised. The hundreds of lines of code I wrote with pandas can now be done in dozens of lines. There’s no need to check those complicated API documents; basic Python knowledge is sufficient.
Data Processing Black Technology
Take a recent sales data analysis I worked on as an example. I needed to count the performance of each salesperson and analyze their customer distribution. Writing it in pandas was quite troublesome, but with SWD, it was much simpler:
from swd import Excel
excel = Excel('sales.xlsx')
data = excel.read('Sales Data')
# Group by salesperson name
sales_total = {}
for row in data:
name = row['Salesperson']
amount = row['Sales Amount']
sales_total[name] = sales_total.get(name, 0) + amount
print(sales_total)
This code impressed the data analysts. There was no need to memorize operations like groupby or pivot; it was all done using basic Python dictionaries and loops. The code was also very easy to maintain, and new colleagues could understand it at a glance.
Data Export Functionality
After processing the data, I needed to export it, and this feature has been well-received. When I used other libraries to export Excel files, I often encountered formatting issues. The files exported by SWD not only maintain the format but also automatically handle Chinese character encoding issues:
from swd import Excel
# Create a new Excel file
excel = Excel()
# Write data
data = [
{'name': 'Zhang San', 'sales': 1000},
{'name': 'Li Si', 'sales': 2000}
]
excel.write(data, sheet_name='Sales Statistics')
# Save the file
excel.save('Statistics Results.xlsx')
Tip: Remember to check the data types during export; make sure numbers don’t accidentally turn into strings, or it will cause issues during statistics.
The export function also supports setting cell formats, font colors, background colors, etc., fully meeting the needs for creating reports. When I created financial reports, I used this feature, and my boss said it looked better than the built-in reports in Excel.
Advanced Features
What surprised me the most was SWD‘s ability to handle Excel formulas. Other Python libraries can usually only read the calculated results of formulas, but SWD can directly read and calculate formulas:
from swd import Excel
excel = Excel('financial_report.xlsx')
# Get cell formula
formula = excel.get_formula('A1')
print(formula) # '=SUM(B1:B10)'
# Calculate formula result
result = excel.calculate_formula('=SUM(B1:B10)')
print(result)
This feature is particularly useful for handling financial reports. I once had a requirement to batch modify formulas in hundreds of Excel files; other libraries couldn’t manage it, but SWD finished it in no time.
Batch Processing Files
If you want to process a bunch of Excel files, you can write it like this:
from swd import Excel
import os
# Process all Excel files in the folder
for file in os.listdir('data_folder'):
if file.endswith('.xlsx'):
excel = Excel(f'data_folder/{file}')
data = excel.read('Sheet1')
# Process data...
Note: When processing multiple files, remember to add a try-except block so that if there’s an issue with one file, the entire program doesn’t crash.
In the past week, I used SWD to process hundreds of sales reports, including data cleaning, format unification, and generating analysis reports. If I had used pandas, it would have taken several days. With SWD, I finished it in one day, and I even helped my colleagues with their Excel files.
The developers of this library mentioned that they also came from a data analysis background and deeply understand the pain points of data processing, which is why they developed this library. After using it, I found this to be true; many features are designed to meet practical work needs. Moreover, they are continuously updating it, often adding new features.
If I had known about this library earlier, I could have written so much less code. But it’s not too late to know now; in the future, when faced with data processing tasks, using SWD is definitely the way to go.
[Follow me, and I’ll help you easily learn Python]