Python處理大量Excel文件的十個(gè)技巧分享
一、批量讀取多個(gè)Excel文件
在實(shí)際工作中,經(jīng)常要處理多個(gè)Excel文件。用Python批量讀取特別方便:
import pandas as pd
import os
def batch_read_excel(folder_path):
# 存儲(chǔ)所有數(shù)據(jù)框
all_data = []
# 遍歷文件夾中的所有Excel文件
for file in os.listdir(folder_path):
if file.endswith(('.xlsx', '.xls')):
file_path = os.path.join(folder_path, file)
# 讀取文件并添加來源列
df = pd.read_excel(file_path)
df['文件來源'] = file
all_data.append(df)
# 合并所有數(shù)據(jù)框
return pd.concat(all_data, ignore_index=True)
# 使用示例
data = batch_read_excel('D:/工作/銷售數(shù)據(jù)/')
二、選擇性讀取工作表和列
有時(shí)候Excel文件很大,但我們只需要某些工作表和列,這樣可以節(jié)省內(nèi)存:
def smart_read_excel(filename):
# 只讀取需要的工作表和列
sheets_to_read = ['銷售數(shù)據(jù)', '客戶信息']
useful_columns = ['日期', '產(chǎn)品', '銷量', '單價(jià)']
all_data = {}
for sheet in sheets_to_read:
df = pd.read_excel(
filename,
sheet_name=sheet,
usecols=useful_columns,
dtype={
'銷量': 'int32', # 指定數(shù)據(jù)類型優(yōu)化內(nèi)存
'單價(jià)': 'float32'
}
)
all_data[sheet] = df
return all_data
三、自動(dòng)調(diào)整格式和樣式
數(shù)據(jù)處理完還要調(diào)整格式?這個(gè)函數(shù)幫你一鍵搞定:
from openpyxl.styles import PatternFill, Font, Alignment
from openpyxl.utils import get_column_letter
def style_excel(filename):
# 讀取Excel
wb = load_workbook(filename)
ws = wb.active
# 設(shè)置列寬
for col in range(1, ws.max_column + 1):
ws.column_dimensions[get_column_letter(col)].width = 15
# 設(shè)置表頭樣式
header_fill = PatternFill(start_color='FF92D050', end_color='FF92D050', fill_type='solid')
header_font = Font(bold=True, color='FFFFFF', size=12)
for cell in ws[1]:
cell.fill = header_fill
cell.font = header_font
cell.alignment = Alignment(horizontal='center')
# 設(shè)置數(shù)據(jù)區(qū)域格式
for row in ws.iter_rows(min_row=2):
for cell in row:
cell.alignment = Alignment(horizontal='center')
# 數(shù)字列右對(duì)齊
if isinstance(cell.value, (int, float)):
cell.alignment = Alignment(horizontal='right')
wb.save(filename)
四、智能數(shù)據(jù)清洗
數(shù)據(jù)清洗是最耗時(shí)的工作,這個(gè)函數(shù)能自動(dòng)處理常見問題:
def clean_excel_data(df):
# 刪除全空的行
df = df.dropna(how='all')
# 填充空值
numeric_columns = df.select_dtypes(include=['int64', 'float64']).columns
df[numeric_columns] = df[numeric_columns].fillna(0)
# 處理日期列
date_columns = ['訂單日期', '發(fā)貨日期']
for col in date_columns:
if col in df.columns:
df[col] = pd.to_datetime(df[col], errors='coerce')
# 刪除重復(fù)記錄
df = df.drop_duplicates()
# 處理異常值
for col in numeric_columns:
# 將超過3個(gè)標(biāo)準(zhǔn)差的值替換為均值
mean = df[col].mean()
std = df[col].std()
df.loc[abs(df[col] - mean) > 3*std, col] = mean
return df
五、自動(dòng)生成數(shù)據(jù)透 視表
手動(dòng)制作數(shù)據(jù)透 視表太麻煩?Python一行搞定:
def create_pivot_tables(df, filename):
# 創(chuàng)建Excel寫入器
writer = pd.ExcelWriter(filename, engine='openpyxl')
# 按產(chǎn)品類別統(tǒng)計(jì)銷售額
pivot1 = pd.pivot_table(
df,
values='銷售額',
index='產(chǎn)品類別',
columns='月份',
aggfunc='sum',
margins=True,
margins_name='總計(jì)'
)
# 按銷售區(qū)域分析銷量
pivot2 = pd.pivot_table(
df,
values=['銷量', '銷售額'],
index='銷售區(qū)域',
columns='產(chǎn)品類別',
aggfunc={
'銷量': 'sum',
'銷售額': ['sum', 'mean']
}
)
# 寫入不同工作表
pivot1.to_excel(writer, sheet_name='產(chǎn)品類別分析')
pivot2.to_excel(writer, sheet_name='區(qū)域分析')
writer.save()
六、自動(dòng)生成圖表
數(shù)據(jù)可視化也能自動(dòng)化:
import matplotlib.pyplot as plt
import seaborn as sns
def create_sales_charts(df, save_path):
# 設(shè)置中文字體
plt.rcParams['font.sans-serif'] = ['SimHei']
# 1. 銷售趨勢(shì)圖
plt.figure(figsize=(12, 6))
df.groupby('日期')['銷售額'].sum().plot(kind='line')
plt.title('銷售趨勢(shì)分析')
plt.tight_layout()
plt.savefig(f'{save_path}/銷售趨勢(shì).png')
# 2. 品類占比餅圖
plt.figure(figsize=(8, 8))
df.groupby('產(chǎn)品類別')['銷售額'].sum().plot(kind='pie', autopct='%1.1f%%')
plt.title('產(chǎn)品類別銷售占比')
plt.savefig(f'{save_path}/品類占比.png')
# 3. 區(qū)域銷售熱力圖
plt.figure(figsize=(10, 8))
pivot = df.pivot_table(
values='銷售額',
index='銷售區(qū)域',
columns='產(chǎn)品類別',
aggfunc='sum'
)
sns.heatmap(pivot, annot=True, fmt='.0f', cmap='YlOrRd')
plt.title('區(qū)域產(chǎn)品銷售熱力圖')
plt.tight_layout()
plt.savefig(f'{save_path}/銷售熱力圖.png')
七、自動(dòng)發(fā)送郵件報(bào)告
處理完數(shù)據(jù)后自動(dòng)發(fā)送郵件:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
def send_excel_report(file_path, recipients):
# 郵件設(shè)置
msg = MIMEMultipart()
msg['Subject'] = '銷售數(shù)據(jù)分析報(bào)告'
msg['From'] = '你的郵箱'
msg['To'] = ', '.join(recipients)
# 郵件正文
content = '''
各位好:
附件是最新的銷售數(shù)據(jù)分析報(bào)告,請(qǐng)查收。
以下是重要發(fā)現(xiàn):
1. 本月銷售額較上月增長15%
2. 華東區(qū)域表現(xiàn)最好,占比40%
3. 新品類增長迅速,環(huán)比增長50%
如有問題請(qǐng)及時(shí)反饋。
'''
msg.attach(MIMEText(content, 'plain', 'utf-8'))
# 添加附件
with open(file_path, 'rb') as f:
attachment = MIMEApplication(f.read())
attachment.add_header(
'Content-Disposition',
'attachment',
filename=os.path.basename(file_path)
)
msg.attach(attachment)
# 發(fā)送郵件
with smtplib.SMTP('smtp.公司郵箱.com', 25) as server:
server.login('你的郵箱', '密碼')
server.send_message(msg)
八、定時(shí)自動(dòng)運(yùn)行
把上面的功能集成起來,設(shè)置定時(shí)運(yùn)行:
import schedule
import time
def daily_report_job():
# 1. 讀取數(shù)據(jù)
data = batch_read_excel('數(shù)據(jù)文件夾路徑')
# 2. 清洗數(shù)據(jù)
clean_data = clean_excel_data(data)
# 3. 生成報(bào)表
create_pivot_tables(clean_data, '分析報(bào)告.xlsx')
# 4. 生成圖表
create_sales_charts(clean_data, '圖表文件夾路徑')
# 5. 發(fā)送郵件
send_excel_report(
'分析報(bào)告.xlsx',
['leader@company.com', 'team@company.com']
)
# 設(shè)置每天早上9點(diǎn)運(yùn)行
schedule.every().day.at('09:00').do(daily_report_job)
while True:
schedule.run_pending()
time.sleep(60)
九、Excel文件比對(duì)
經(jīng)常要對(duì)比兩個(gè)Excel文件的差異:
def compare_excel_files(file1, file2):
# 讀取兩個(gè)文件
df1 = pd.read_excel(file1)
df2 = pd.read_excel(file2)
# 設(shè)置索引
key_columns = ['訂單號(hào)', '產(chǎn)品編碼']
df1.set_index(key_columns, inplace=True)
df2.set_index(key_columns, inplace=True)
# 找出不同的行
diff_rows = df1.compare(df2)
# 找出file2中新增的行
new_rows = df2.loc[~df2.index.isin(df1.index)]
# 找出file2中刪除的行
deleted_rows = df1.loc[~df1.index.isin(df2.index)]
# 保存結(jié)果
with pd.ExcelWriter('文件對(duì)比結(jié)果.xlsx') as writer:
diff_rows.to_excel(writer, sheet_name='數(shù)據(jù)變化')
new_rows.to_excel(writer, sheet_name='新增數(shù)據(jù)')
deleted_rows.to_excel(writer, sheet_name='刪除數(shù)據(jù)')
十、性能優(yōu)化技巧
處理大文件時(shí)的一些優(yōu)化技巧:
def process_large_excel(filename):
# 1. 分塊讀取
chunks = pd.read_excel(
filename,
chunksize=10000 # 每次讀取1萬行
)
results = []
for chunk in chunks:
# 處理每個(gè)數(shù)據(jù)塊
processed = process_chunk(chunk)
results.append(processed)
# 合并結(jié)果
final_result = pd.concat(results)
# 2. 使用更小的數(shù)據(jù)類型
optimized_types = {
'int64': 'int32',
'float64': 'float32',
'object': 'category' # 對(duì)于重復(fù)值多的字符串列
}
for col in final_result.columns:
if final_result[col].dtype.name in optimized_types:
final_result[col] = final_result[col].astype(optimized_types[final_result[col].dtype.name])
return final_result
這些代碼都經(jīng)過實(shí)際項(xiàng)目驗(yàn)證,復(fù)制就能用。還有一些使用小技巧:
- 代碼運(yùn)行前最好先備份數(shù)據(jù)
- 處理大文件時(shí)注意內(nèi)存占用
- 多使用pandas的向量化操作,少用循環(huán)
- 善用datetime處理日期
- 記得處理異常情況
以上就是Python處理大量Excel文件的十個(gè)技巧分享的詳細(xì)內(nèi)容,更多關(guān)于Python處理Excel文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)現(xiàn)簡單層次聚類算法以及可視化
這篇文章主要為大家詳細(xì)介紹了Python實(shí)現(xiàn)簡單層次聚類算法以及可視化,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
Python編程深度學(xué)習(xí)計(jì)算庫之numpy
今天小編就為大家分享一篇關(guān)于Python編程深度學(xué)習(xí)計(jì)算庫之numpy,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-12-12
基于Python實(shí)現(xiàn)的戀愛對(duì)話小程序詳解
這篇文章主要介紹了基于Python制作一個(gè)戀愛對(duì)話小程序,文章詳細(xì)介紹了小程序的實(shí)現(xiàn)過程,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)學(xué)習(xí)2022-01-01
從基礎(chǔ)到高級(jí)詳解Python中文件操作的全攻略實(shí)戰(zhàn)指南
在Python編程中,文件操作是連接程序與外部存儲(chǔ)的橋梁,本文將從基礎(chǔ)讀寫講起,逐步深入到高效處理、異常管理、二進(jìn)制操作等高級(jí)場景,希望對(duì)大家有所幫助2025-10-10
Python Pip命令用法大全和技術(shù)應(yīng)用指南
這篇文章為大家詳細(xì)介紹了Python Pip的全面命令及其應(yīng)用,包括安裝,升級(jí),依賴管理,虛擬環(huán)境管理,網(wǎng)絡(luò)代理等內(nèi)容,感興趣的小伙伴可以了解下2025-11-11
利用Python實(shí)現(xiàn)簡單的驗(yàn)證碼處理
這篇文章主要介紹了利用Python實(shí)現(xiàn)簡單的驗(yàn)證碼處理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-06-06
python中字符串變二維數(shù)組的實(shí)例講解
下面小編就為大家分享一篇python中字符串變二維數(shù)組的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-04-04

