Top 25 Python-Skripte

Top 25 Python-Skripte zur Automatisierung Ihrer täglichen Aufgaben

vg

Python ist dank seiner Einfachheit und einer großen Auswahl an Bibliotheken ein hervorragendes Werkzeug für die Automatisierung täglicher Aufgaben. Im Folgenden finden Sie die Top 25 Python-Skripte, mit denen Sie häufige Aufgaben in verschiedenen Bereichen automatisieren können.

1. Automatisieren Sie den E-Mail-Versand

  • Verwenden Sie Python, um E-Mails mit Anhängen zu senden.
  • Bibliotheken: smtplibemail
import smtplib
from email.mime.text import MIMEText

def send_email(subject, body, to_email):
smtp_server = "smtp.gmail.com"
smtp_port = 587
sender_email = "your_email@gmail.com"
sender_password = "your_password"

msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = to_email

with smtplib.SMTP(smtp_server, smtp_port) as server:
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, to_email, msg.as_string())

2. Web Scraping zur Datenextraktion

  • Automatisieren Sie die Datenextraktion von Websites.
  • Bibliotheken: requestsBeautifulSoup
import requests
from bs4 import BeautifulSoup

def scrape_weather():
url = "https://weather.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title.string)

3. Dateien aus dem Internet herunterladen

  • Automatisieren Sie das Herunterladen von Dateien von URLs.
  • Bibliotheken: requests
import requests

def download_file(url, save_path):
response = requests.get(url)
with open(save_path, 'wb') as file:
file.write(response.content)

4. Automatisieren Sie die Dateisortierung

  • Organisieren Sie Dateien automatisch nach Erweiterungen.
  • Bibliotheken: osshutil
import os
import shutil

def sort_files(directory):
for file in os.listdir(directory):
ext = file.split('.')[-1]
folder = os.path.join(directory, ext)
os.makedirs(folder, exist_ok=True)
shutil.move(os.path.join(directory, file), os.path.join(folder, file))

5. Benennen Sie mehrere Dateien um

  • Batch-Umbenennung von Dateien in einem Verzeichnis.
  • Bibliotheken: os
import os

def rename_files(directory, prefix):
for i, file in enumerate(os.listdir(directory)):
os.rename(os.path.join(directory, file), os.path.join(directory, f"{prefix}_{i}.txt"))

6. Automatisieren Sie die Erstellung von Backups

  • Sichern Sie wichtige Dateien in einer ZIP-Datei.
  • Bibliotheken: shutil
import shutil

def create_backup(source_dir, backup_file):
shutil.make_archive(backup_file, 'zip', source_dir)

7. Automatisieren Sie Social-Media-Posts

  • Planen Sie Tweets/Posts.
  • Bibliotheken: tweepyfacebook-sdk
import tweepy

def post_tweet(api_key, api_secret, access_token, access_secret, tweet):
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
api.update_status(tweet)

8. Automatisieren Sie Tabellenkalkulationsdaten

  • Excel-Dateien lesen/schreiben.
  • Bibliotheken: openpyxl
import openpyxl

def read_excel(file):
wb = openpyxl.load_workbook(file)
sheet = wb.active
for row in sheet.iter_rows():
print([cell.value for cell in row])

9. Automatisieren Sie die Textübersetzung

  • Übersetzen Sie Text mithilfe von APIs.
  • Bibliotheken: googletrans
from googletrans import Translator

def translate_text(text, dest_lang):
translator = Translator()
return translator.translate(text, dest=dest_lang).text

10. Automatisieren Sie die PDF-Manipulation

  • Zusammenführen, Teilen oder Extrahieren von Text aus PDFs.
  • Bibliotheken: PyPDF2
from PyPDF2 import PdfReader, PdfMerger

def merge_pdfs(pdf_list, output):
merger = PdfMerger()
for pdf in pdf_list:
merger.append(pdf)
merger.write(output)

11. Automatisieren Sie die Bildverarbeitung

  • Ändern Sie die Größe, drehen Sie sie oder fügen Sie Wasserzeichen hinzu.
  • Bibliotheken: Pillow
from PIL import Image

def resize_image(image_path, output_path, size):
with Image.open(image_path) as img:
img.resize(size).save(output_path)

12. Automatisieren Sie die Website-Überwachung

  • Benachrichtigen, wenn eine Website aktualisiert wird.
  • Bibliotheken: requeststime
import requests
import time

def monitor_website(url, interval):
prev_content = None
while True:
response = requests.get(url)
if response.text != prev_content:
print("Website updated!")
prev_content = response.text
time.sleep(interval)

13. Automatisieren Sie Datenbank-Backups

  • Sichern Sie Datenbanken wie MySQL.
  • Bibliotheken: subprocess
import subprocess

def backup_mysql(user, password, db_name, output):
cmd = f"mysqldump -u {user} -p{password} {db_name} > {output}"
subprocess.run(cmd, shell=True)

14. Automatisieren Sie Slack-Benachrichtigungen

  • Senden Sie Slack-Nachrichten programmgesteuert.
  • Bibliotheken: slack-sdk
from slack_sdk import WebClient

def send_slack_message(token, channel, text):
client = WebClient(token=token)
client.chat_postMessage(channel=channel, text=text)

15. Automatisieren Sie Wetteraktualisierungen

  • Abrufen von Wetterdaten.
  • Bibliotheken: requests
import requests

def get_weather(api_key, city):
url = f"http://api.openweathermap.org/data/2.5/weather?q={city}&appid={api_key}"
return requests.get(url).json()

16. Automatisieren Sie Text-to-Speech

  • Konvertieren Sie Text in Sprache.
  • Bibliotheken: pyttsx3
import pyttsx3

def text_to_speech(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()

17. Automatisieren Sie die Währungsumrechnung

  • Konvertieren Sie Währungen mithilfe von APIs.
  • Bibliotheken: forex-python
from forex_python.converter import CurrencyRates

def convert_currency(amount, from_currency, to_currency):
c = CurrencyRates()
return c.convert(from_currency, to_currency, amount)

18. Automatisieren Sie die Aufgabenplanung

  • Planen Sie Python-Aufgaben.
  • Bibliotheken: schedule
import schedule
import time

def task():
print("Task running!")

schedule.every().day.at("10:00").do(task)

while True:
schedule.run_pending()
time.sleep(1)

19. Automatisieren Sie Benachrichtigungen

  • Push-Benachrichtigungen auf Ihr Telefon.
  • Bibliotheken: pushbullet
from pushbullet import Pushbullet

def send_notification(api_key, title, body):
pb = Pushbullet(api_key)
pb.push_note(title, body)

20. Automatisieren Sie die Verzeichnisbereinigung

  • Löschen Sie alte Dateien in einem Verzeichnis.
  • Bibliotheken: ostime
import os
import time

def cleanup(directory, days):
now = time.time()
for file in os.listdir(directory):
filepath = os.path.join(directory, file)
if os.stat(filepath).st_mtime < now - days * 86400:
os.remove(filepath)

21. Automatisieren Sie die Aktienkursüberwachung

  • Holen Sie sich Aktienkurse.
  • Bibliotheken: yfinance
import yfinance as yf

def get_stock_price(ticker):
stock = yf.Ticker(ticker)
return stock.history(period="1d")["Close"]

22. Automatisieren Sie die QR-Code-Generierung

  • Generieren Sie QR-Codes für Text oder URLs.
  • Bibliotheken: qrcode
import qrcode

def generate_qr(data, filename):
qr = qrcode.make(data)
qr.save(filename)

23. Automatisieren Sie die Simulation von Tastendrücken

  • Automatisieren Sie das Drücken der Tastatur.
  • Bibliotheken: pyautogui
import pyautogui

def automate_typing(text):
pyautogui.typewrite(text)

24. Automatisieren Sie Git-Vorgänge

  • Automatisieren Sie git push/pull.
  • Bibliotheken: subprocess
import subprocess

def git_push(message):
subprocess.run(["git", "add", "."])
subprocess.run(["git", "commit", "-m", message])
subprocess.run(["git", "push"])

25. Automatisieren Sie die Zeiterfassung

  • Verfolgen Sie Ihre Zeit, die Sie für Aufgaben aufgewendet haben.
  • Bibliotheken: time
import time

start_time = time.time()
# Do some work
print("Time spent:", time.time() - start_time)

Fazit

Diese Top 25 Python-Skripte können Ihnen helfen, Zeit zu sparen und sich wiederholende Aufgaben zu vereinfachen. Kombinieren Sie diese mit Cron-Jobs oder Aufgabenplanern, um leistungsstarke Automatisierungen freizuschalten!

Weitere lesenswerte Beiträge: 10 Software-Architektur-Pattern auf den Punkt gebracht und Was ist neu in PHP 8.4?

com

Newsletter Anmeldung

Bleiben Sie informiert! Wir informieren Sie über alle neuen Beiträge (max. 1 Mail pro Woche – versprochen)