使用Python編寫一個自動化辦公小助手
在日常辦公中,我們常常會遇到一些重復性的任務,如批量處理文件、發(fā)送郵件、生成報表等。這些任務不僅耗時,還容易出錯。今天,就讓我們一起用 Python 編寫一個自動化辦公小助手,幫助你高效完成這些任務。
一、自動化辦公小助手的功能
(一)批量重命名文件
import os
def batch_rename_files(directory, prefix):
"""批量重命名指定目錄下的所有文件,添加前綴"""
for filename in os.listdir(directory):
new_name = f"{prefix}_{filename}"
os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
print("文件重命名完成")
# 示例:批量重命名文件
batch_rename_files('path/to/your/directory', 'new_prefix')
(二)發(fā)送郵件
import smtplib
from email.mime.text import MIMEText
def send_email(to_email, subject, body):
"""發(fā)送郵件"""
sender = "your_email@example.com"
password = "your_password"
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to_email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(sender, password)
server.sendmail(sender, to_email, msg.as_string())
print("郵件發(fā)送成功")
# 示例:發(fā)送郵件
send_email('recipient@example.com', 'Subject', 'Email body')
(三)生成 Excel 報表
import pandas as pd
def generate_excel_report(data, output_file):
"""生成 Excel 報表"""
df = pd.DataFrame(data)
df.to_excel(output_file, index=False)
print("報表生成完成")
# 示例:生成 Excel 報表
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
generate_excel_report(data, 'report.xlsx')
(四)批量處理 Excel 文件
import pandas as pd
import os
def batch_process_excel_files(directory, output_file):
"""批量處理 Excel 文件,合并到一個文件中"""
all_data = []
for filename in os.listdir(directory):
if filename.endswith('.xlsx'):
file_path = os.path.join(directory, filename)
df = pd.read_excel(file_path)
all_data.append(df)
combined_df = pd.concat(all_data, ignore_index=True)
combined_df.to_excel(output_file, index=False)
print("文件處理完成")
# 示例:批量處理 Excel 文件
batch_process_excel_files('path/to/your/directory', 'combined_output.xlsx')
(五)定時任務
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
def my_job():
print("任務執(zhí)行時間:", datetime.datetime.now())
# 示例:設置定時任務
scheduler = BlockingScheduler()
scheduler.add_job(my_job, 'interval', seconds=10)
scheduler.start()
二、整合自動化辦公小助手
將上述功能整合到一個腳本中,創(chuàng)建一個自動化辦公小助手。
import os
import smtplib
from email.mime.text import MIMEText
import pandas as pd
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
# 批量重命名文件
def batch_rename_files(directory, prefix):
for filename in os.listdir(directory):
new_name = f"{prefix}_{filename}"
os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))
print("文件重命名完成")
# 發(fā)送郵件
def send_email(to_email, subject, body):
sender = "your_email@example.com"
password = "your_password"
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to_email
with smtplib.SMTP('smtp.example.com', 587) as server:
server.starttls()
server.login(sender, password)
server.sendmail(sender, to_email, msg.as_string())
print("郵件發(fā)送成功")
# 生成 Excel 報表
def generate_excel_report(data, output_file):
df = pd.DataFrame(data)
df.to_excel(output_file, index=False)
print("報表生成完成")
# 批量處理 Excel 文件
def batch_process_excel_files(directory, output_file):
all_data = []
for filename in os.listdir(directory):
if filename.endswith('.xlsx'):
file_path = os.path.join(directory, filename)
df = pd.read_excel(file_path)
all_data.append(df)
combined_df = pd.concat(all_data, ignore_index=True)
combined_df.to_excel(output_file, index=False)
print("文件處理完成")
# 定時任務
def my_job():
print("任務執(zhí)行時間:", datetime.datetime.now())
# 主函數(shù)
def main():
# 批量重命名文件
batch_rename_files('path/to/your/directory', 'new_prefix')
# 發(fā)送郵件
send_email('recipient@example.com', 'Subject', 'Email body')
# 生成 Excel 報表
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
generate_excel_report(data, 'report.xlsx')
# 批量處理 Excel 文件
batch_process_excel_files('path/to/your/directory', 'combined_output.xlsx')
# 設置定時任務
scheduler = BlockingScheduler()
scheduler.add_job(my_job, 'interval', seconds=10)
scheduler.start()
if __name__ == "__main__":
main()
三、總結
通過本文的介紹,你已經(jīng)學會了如何使用 Python 編寫一個自動化辦公小助手,包括批量重命名文件、發(fā)送郵件、生成 Excel 報表、批量處理 Excel 文件和設置定時任務。
到此這篇關于使用Python編寫一個自動化辦公小助手的文章就介紹到這了,更多相關Python自動化辦公內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python通過yagmail實現(xiàn)發(fā)送郵件代碼解析
這篇文章主要介紹了Python通過yagmail實現(xiàn)發(fā)送郵件代碼解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-10-10
Python Django搭建文件下載服務器的實現(xiàn)
這篇文章主要介紹了Python Django搭建文件下載服務器的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05
使用OpenCV獲取圖片連通域數(shù)量,并用不同顏色標記函
這篇文章主要介紹了使用OpenCV獲取圖片連通域數(shù)量,并用不同顏色標記函,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06
快速進修Python指南之網(wǎng)絡編程及并發(fā)編程
這篇文章主要為大家介紹了Java開發(fā)者如何快速進修Python指南之網(wǎng)絡編程及并發(fā)編程實例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-12-12
Python實現(xiàn)在Windows平臺修改文件屬性
這篇文章主要介紹了Python實現(xiàn)在Windows平臺修改文件屬性,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03
Python使用FastAPI+FastCRUD自動生成API接口的實現(xiàn)
FastCRUD 是一個為 FastAPI + SQLAlchemy 場景設計的開源庫,由 Benav Labs 維護,旨在簡化 CRUD 端點搭建與數(shù)據(jù)庫操作的流程,本文呢給大家介紹了Python使用FastAPI+FastCRUD自動生成API接口的實現(xiàn),需要的朋友可以參考下2025-11-11

