使用Python編寫一個自動化辦公小助手
在日常辦公中,我們常常會遇到一些重復(fù)性的任務(wù),如批量處理文件、發(fā)送郵件、生成報表等。這些任務(wù)不僅耗時,還容易出錯。今天,就讓我們一起用 Python 編寫一個自動化辦公小助手,幫助你高效完成這些任務(wù)。
一、自動化辦公小助手的功能
(一)批量重命名文件
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')
(五)定時任務(wù)
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime
def my_job():
print("任務(wù)執(zhí)行時間:", datetime.datetime.now())
# 示例:設(shè)置定時任務(wù)
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("文件處理完成")
# 定時任務(wù)
def my_job():
print("任務(wù)執(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')
# 設(shè)置定時任務(wù)
scheduler = BlockingScheduler()
scheduler.add_job(my_job, 'interval', seconds=10)
scheduler.start()
if __name__ == "__main__":
main()
三、總結(jié)
通過本文的介紹,你已經(jīng)學(xué)會了如何使用 Python 編寫一個自動化辦公小助手,包括批量重命名文件、發(fā)送郵件、生成 Excel 報表、批量處理 Excel 文件和設(shè)置定時任務(wù)。
到此這篇關(guān)于使用Python編寫一個自動化辦公小助手的文章就介紹到這了,更多相關(guān)Python自動化辦公內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python連接數(shù)據(jù)庫使用matplotlib畫柱形圖
這篇文章主要介紹了Python連接數(shù)據(jù)庫使用matplotlib畫柱形圖,文章通過實例展開對主題的相關(guān)介紹。具有一定的知識參考價值性,感興趣的小伙伴可以參考一下2022-06-06
Anaconda修改默認(rèn)虛擬環(huán)境安裝位置的方案分享
新安裝Anaconda后,在創(chuàng)建環(huán)境時環(huán)境自動安裝在C盤,但是C盤空間有限,下面這篇文章主要給大家介紹了關(guān)于Anaconda修改默認(rèn)虛擬環(huán)境安裝位置的相關(guān)資料,需要的朋友可以參考下2023-01-01
python celery beat實現(xiàn)定時任務(wù)的示例代碼
在日常工作中,我們常常會用到需要周期性執(zhí)行的任務(wù),本文主要介紹了python celery beat實現(xiàn)定時任務(wù)的示例代碼,具有一定的參考價值,感興趣的可以了解一下2024-03-03

