10 Python Scripts That Save Hours of Work Every Day
Are you tired of wasting hours on tedious tasks in your Python projects? As a developer, you're always looking for ways to improve your productivity and get more done in less time. In this article, we'll explore 10 Python scripts that can help you automate common tasks, saving you hours of work every day.
Why Python?
Python is an excellent language for automation because it's easy to learn, has a vast number of libraries, and is highly versatile. With Python, you can automate tasks in various domains, from data analysis to web development. Whether you're a beginner or an experienced developer, Python is an ideal choice for automation tasks.
Script 1: Automating File Renaming
Have you ever had to rename multiple files in a directory? It can be a tedious task, especially if you have to rename hundreds of files. Here's a Python script that can automate the process:
import os
import re
def rename_files(directory):
for filename in os.listdir(directory):
new_filename = re.sub(r'old_name', 'new_name', filename)
os.rename(os.path.join(directory, filename), os.path.join(directory, new_filename))
rename_files('/path/to/directory')
This script uses the os and re libraries to rename files in a specified directory. Simply replace /path/to/directory with your actual directory path and old_name and new_name with your desired file name patterns.
Script 2: Generating Markdown Tables
If you're working with data, you often need to generate tables for documentation or reporting. Here's a Python script that can help you generate Markdown tables:
import pandas as pd
def generate_markdown_table(data):
table = pd.DataFrame(data)
return table.to_markdown()
data = {'Name': ['John', 'Jane', 'Bob'], 'Age': [25, 30, 35]}
print(generate_markdown_table(data))
This script uses the pandas library to generate a Markdown table from a dictionary. Simply replace the data dictionary with your actual data.
Script 3: Sending Automated Emails
Do you need to send automated emails to your team or clients? Here's a Python script that can help you send emails using the smtplib library:
import smtplib
from email.mime.text import MIMEText
def send_email(subject, body, from_addr, to_addr, password):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = from_addr
msg['To'] = to_addr
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(from_addr, password)
server.sendmail(from_addr, to_addr, msg.as_string())
server.quit()
send_email('Test Email', 'This is a test email.', 'your-email@gmail.com', 'recipient-email@example.com', 'your-password')
This script uses the smtplib library to send an email using Gmail's SMTP server. Simply replace the subject, body, from_addr, to_addr, and password variables with your actual email credentials.
Script 4: Extracting Data from CSV Files
Do you need to extract data from CSV files? Here's a Python script that can help you extract data using the pandas library:
import pandas as pd
def extract_data(csv_file):
data = pd.read_csv(csv_file)
return data
data = extract_data('data.csv')
print(data)
This script uses the pandas library to extract data from a CSV file. Simply replace 'data.csv' with your actual CSV file path.
Script 5: Creating PDF Reports
Do you need to create PDF reports from your data? Here's a Python script that can help you create PDF reports using the fpdf library:
from fpdf import FPDF
def create_pdf_report(data):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=15)
for row in data:
for col in row:
pdf.cell(40, 10, txt=str(col), ln=True, align='L')
pdf.output("report.pdf")
return "report.pdf"
data = [['Name', 'Age'], ['John', 25], ['Jane', 30]]
print(create_pdf_report(data))
This script uses the fpdf library to create a PDF report from a dictionary. Simply replace the data dictionary with your actual data.
Script 6: Scanning Directories for Specific Files
Do you need to scan directories for specific files? Here's a Python script that can help you scan directories using the os library:
import os
def scan_directory(directory, file_pattern):
for filename in os.listdir(directory):
if file_pattern in filename:
print(filename)
scan_directory('/path/to/directory', 'pattern.txt')
This script uses the os library to scan a directory for files that match a specific pattern. Simply replace /path/to/directory with your actual directory path and 'pattern.txt' with your desired file pattern.
Script 7: Creating Zip Archives
Do you need to create zip archives from your files? Here's a Python script that can help you create zip archives using the zipfile library:
import zipfile
def create_zip_archive(directory):
zip_file = zipfile.ZipFile('archive.zip', 'w')
for filename in os.listdir(directory):
zip_file.write(os.path.join(directory, filename))
zip_file.close()
create_zip_archive('/path/to/directory')
This script uses the zipfile library to create a zip archive from a directory. Simply replace /path/to/directory with your actual directory path.
Script 8: Parsing HTML Pages
Do you need to parse HTML pages? Here's a Python script that can help you parse HTML pages using the beautifulsoup4 library:
from bs4 import BeautifulSoup
def parse_html_page(html):
soup = BeautifulSoup(html, 'html.parser')
return soup
html = '<html><body>Hello World!</body></html>'
print(parse_html_page(html))
This script uses the beautifulsoup4 library to parse an HTML page. Simply replace the html variable with your actual HTML page content.
Script 9: Creating Cron Jobs
Do you need to create cron jobs for your tasks? Here's a Python script that can help you create cron jobs using the schedule library:
import schedule
import time
def job():
print("Hello World!")
schedule.every(10).minutes.do(job)
while True:
schedule.run_pending()
time.sleep(1)
This script uses the schedule library to create a cron job that runs every 10 minutes. Simply replace the job function with your actual task function.
Script 10: Validating User Input
Do you need to validate user input? Here's a Python script that can help you validate user input using regular
Top comments (0)