Comprehensive Collection of Python Batch Automation Scripts
This article provides a curated set of Python scripts that automate common batch tasks—including merging Excel and CSV files, renaming and moving files, PDF merging, image conversion and processing, QR‑code and word‑cloud generation, holiday marking, and bulk email sending—complete with ready‑to‑run code examples.
These Python scripts demonstrate various batch automation tasks such as merging Excel or CSV files, renaming or moving files, merging PDFs, converting formats, image processing, generating QR codes, creating word clouds, marking holidays, and sending bulk emails.
Script 1: Batch Merge Excel
Iterates through a directory, reads all Excel files, concatenates them into a single DataFrame, and saves the result.
import os
import pandas as pd
def merge_excel_files(directory):
# Get all Excel files in the directory
excel_files = [f for f in os.listdir(directory) if f.endswith('.xlsx') or f.endswith('.xls')]
combined_df = pd.DataFrame()
for file in excel_files:
file_path = os.path.join(directory, file)
df = pd.read_excel(file_path)
combined_df = pd.concat([combined_df, df], ignore_index=True)
return combined_df
# Example usage
directory_path = 'path/to/your/excel/files'
merged_data = merge_excel_files(directory_path)
print(merged_data)
merged_data.to_excel('merged_output.xlsx', index=False)Script 2: Batch Merge CSV
Collects all CSV files in a folder, concatenates them, and writes the combined data to a new CSV file.
import os
import pandas as pd
def merge_csv_files(directory):
csv_files = [f for f in os.listdir(directory) if f.endswith('.csv')]
combined_df = pd.DataFrame()
for file in csv_files:
file_path = os.path.join(directory, file)
df = pd.read_csv(file_path)
combined_df = pd.concat([combined_df, df], ignore_index=True)
return combined_df
# Example usage
directory_path = 'path/to/your/csv/files'
merged_data = merge_csv_files(directory_path)
print(merged_data)
merged_data.to_csv('merged_output.csv', index=False)Script 3: Batch Rename Files
Renames files in a directory by replacing a specified old pattern with a new pattern.
import os
def batch_rename_files(directory, old_pattern, new_pattern):
files = os.listdir(directory)
for filename in files:
if old_pattern in filename:
new_filename = filename.replace(old_pattern, new_pattern)
old_file_path = os.path.join(directory, filename)
new_file_path = os.path.join(directory, new_filename)
os.rename(old_file_path, new_file_path)
print(f'Renamed "{filename}" to "{new_filename}"')
# Example usage
directory_path = 'path/to/your/files'
old_pattern = 'old_text'
new_pattern = 'new_text'
batch_rename_files(directory_path, old_pattern, new_pattern)Script 4: Batch Move Files to a Folder
Moves all files from a source directory into a target directory, creating the target if it does not exist.
import os
import shutil
def batch_move_files(source_directory, target_directory):
if not os.path.exists(target_directory):
os.makedirs(target_directory)
files = os.listdir(source_directory)
for filename in files:
source_file_path = os.path.join(source_directory, filename)
if os.path.isfile(source_file_path):
target_file_path = os.path.join(target_directory, filename)
shutil.move(source_file_path, target_file_path)
print(f'Moved "{source_file_path}" to "{target_file_path}"')
# Example usage
source_directory_path = 'path/to/source/files'
target_directory_path = 'path/to/target/folder'
batch_move_files(source_directory_path, target_directory_path)Script 5: Batch Merge PDF Files
Combines all PDF files in a directory into a single PDF document.
import os
from PyPDF2 import PdfReader, PdfWriter
def merge_pdf_files(directory, output_filename):
pdf_files = [f for f in os.listdir(directory) if f.endswith('.pdf')]
pdf_writer = PdfWriter()
for file in pdf_files:
file_path = os.path.join(directory, file)
pdf_reader = PdfReader(file_path)
for page_num in range(len(pdf_reader.pages)):
page = pdf_reader.pages[page_num]
pdf_writer.add_page(page)
with open(output_filename, 'wb') as out_file:
pdf_writer.write(out_file)
print(f'Merged PDFs into "{output_filename}"')
# Example usage
directory_path = 'path/to/your/pdf/files'
output_file_name = 'merged_output.pdf'
merge_pdf_files(directory_path, output_file_name)Script 6: Batch Convert Excel to CSV
Converts every Excel file in a folder to a CSV file with the same base name.
import os
import pandas as pd
def excel_to_csv(directory):
excel_files = [f for f in os.listdir(directory) if f.endswith('.xlsx') or f.endswith('.xls')]
for file in excel_files:
file_path = os.path.join(directory, file)
df = pd.read_excel(file_path)
csv_file_name = os.path.splitext(file)[0] + '.csv'
csv_file_path = os.path.join(directory, csv_file_name)
df.to_csv(csv_file_path, index=False)
print(f'Converted "{file}" to "{csv_file_name}"')
# Example usage
directory_path = 'path/to/your/excel/files'
excel_to_csv(directory_path)Script 7: Batch Convert CSV to Excel
Transforms each CSV file in a directory into an Excel workbook.
import os
import pandas as pd
def csv_to_excel(directory):
csv_files = [f for f in os.listdir(directory) if f.endswith('.csv')]
for file in csv_files:
file_path = os.path.join(directory, file)
df = pd.read_csv(file_path)
excel_file_name = os.path.splitext(file)[0] + '.xlsx'
excel_file_path = os.path.join(directory, excel_file_name)
df.to_excel(excel_file_path, index=False, engine='openpyxl')
print(f'Converted "{file}" to "{excel_file_name}"')
# Example usage
directory_path = 'path/to/your/csv/files'
csv_to_excel(directory_path)Script 8: Batch Image Cropping
Crops all images in a source folder to a specified rectangle and saves them to a target folder.
import os
from PIL import Image
def batch_crop_images(source_directory, target_directory, crop_box):
if not os.path.exists(target_directory):
os.makedirs(target_directory)
image_files = [f for f in os.listdir(source_directory) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
for file in image_files:
file_path = os.path.join(source_directory, file)
with Image.open(file_path) as img:
cropped_img = img.crop(crop_box)
output_file_path = os.path.join(target_directory, file)
cropped_img.save(output_file_path)
print(f'Cropped "{file}" and saved to "{output_file_path}"')
# Example usage
source_directory_path = 'path/to/source/images'
target_directory_path = 'path/to/target/images'
crop_box = (100, 100, 400, 400)
batch_crop_images(source_directory_path, target_directory_path, crop_box)Script 9: Batch Image Deduplication
Uses perceptual hashing to find duplicate images in a directory.
from PIL import Image
import os
import imagehash
def get_image_hashes(directory):
hashes = {}
for filename in os.listdir(directory):
if filename.lower().endswith(('.png', '.jpg', '.jpeg')):
filepath = os.path.join(directory, filename)
with Image.open(filepath) as img:
hash_value = imagehash.average_hash(img)
hashes.setdefault(hash_value, []).append(filename)
return hashes
def find_duplicates(hashes):
return [files for files in hashes.values() if len(files) > 1]
if __name__ == "__main__":
directory = 'path_to_your_images_directory'
hashes = get_image_hashes(directory)
duplicates = find_duplicates(hashes)
if duplicates:
print("Found duplicate images:")
for group in duplicates:
print(group)
else:
print("No duplicate images found")Script 10: Batch Unzip Files
Extracts every .zip archive in a folder into a sub‑folder with the same name.
import os
import zipfile
def unzip_files_in_directory(directory):
for item in os.listdir(directory):
full_path = os.path.join(directory, item)
if os.path.isfile(full_path) and item.endswith('.zip'):
with zipfile.ZipFile(full_path, 'r') as zip_ref:
extract_dir = os.path.splitext(full_path)[0]
zip_ref.extractall(extract_dir)
print(f"Extracted: {item} -> {extract_dir}")
# Example usage
unzip_files_in_directory('/path/to/your/zipped/files')Script 11: Batch Zip Files
Compresses all files under a directory (recursively) into a single zip archive.
import os
import zipfile
def zip_files_in_directory(directory, output_zip):
with zipfile.ZipFile(output_zip, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(directory):
for file in files:
full_path = os.path.join(root, file)
arcname = os.path.relpath(full_path, start=directory)
zipf.write(full_path, arcname=arcname)
print(f"Added to zip: {arcname}")
# Example usage
zip_files_in_directory('/path/to/your/files', '/path/to/output.zip')Script 12: Batch Send Emails
Sends a plain‑text email to multiple recipients using an SMTP server.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
def send_email(smtp_server, smtp_port, sender_email, sender_password, recipient_emails, subject, body):
msg = MIMEMultipart()
msg['From'] = sender_email
msg['Subject'] = subject
msg.attach(MIMEText(body, 'plain'))
try:
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, sender_password)
for recipient in recipient_emails:
msg['To'] = recipient
server.sendmail(sender_email, recipient, msg.as_string())
print(f"Email sent to {recipient}")
server.quit()
print("All emails have been sent successfully.")
except Exception as e:
print(f"Failed to send email: {e}")
# Example usage
smtp_server = 'smtp.example.com'
smtp_port = 587
sender_email = '[email protected]'
sender_password = 'your-email-password'
recipient_emails = ['[email protected]', '[email protected]']
subject = 'Test Email Subject'
body = 'This is the body of the test email.'
send_email(smtp_server, smtp_port, sender_email, sender_password, recipient_emails, subject, body)Script 13: Batch Resize Images
Resizes all images in a folder to a given size and saves them to an output folder.
from PIL import Image
import os
def resize_image(input_image_path, output_image_path, size):
with Image.open(input_image_path) as image:
resized_image = image.resize(size)
resized_image.save(output_image_path)
print(f"Resized image saved to {output_image_path}")
def batch_resize_images(directory, output_directory, size):
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for filename in os.listdir(directory):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
file_path = os.path.join(directory, filename)
output_file_path = os.path.join(output_directory, filename)
resize_image(file_path, output_file_path, size)
# Example usage
input_dir = 'path/to/your/images'
output_dir = 'path/to/output/images'
new_size = (800, 600)
batch_resize_images(input_dir, output_dir, new_size)Script 14: Batch Screenshot
Takes a series of screenshots at defined intervals and saves them with timestamped filenames.
import pyautogui
import os
import time
from datetime import datetime
def batch_screenshot(directory, num_shots, interval_seconds=5):
if not os.path.exists(directory):
os.makedirs(directory)
for i in range(num_shots):
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = os.path.join(directory, f"screenshot_{timestamp}.png")
screenshot = pyautogui.screenshot()
screenshot.save(filename)
print(f"Screenshot saved as {filename}")
if i < num_shots - 1:
time.sleep(interval_seconds)
# Example usage
output_dir = 'path/to/output/screenshots'
num_shots = 5
interval_seconds = 2
batch_screenshot(output_dir, num_shots, interval_seconds)Script 15: PNG to JPG / JPG to PNG Conversion
Two utilities that convert image files between PNG and JPG formats in bulk.
# PNG to JPG
from PIL import Image
import os
def png_to_jpg(input_directory, output_directory):
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for filename in os.listdir(input_directory):
if filename.lower().endswith('.png'):
file_path = os.path.join(input_directory, filename)
img = Image.open(file_path).convert('RGB')
new_filename = os.path.splitext(filename)[0] + '.jpg'
output_file_path = os.path.join(output_directory, new_filename)
img.save(output_file_path, 'JPEG')
print(f"Converted {filename} to {new_filename}")
# Example usage
png_to_jpg('path/to/png/files', 'path/to/output/jpg/files')
# JPG to PNG
from PIL import Image
import os
def jpg_to_png(input_directory, output_directory):
if not os.path.exists(output_directory):
os.makedirs(output_directory)
for filename in os.listdir(input_directory):
if filename.lower().endswith(('.jpg', '.jpeg')):
file_path = os.path.join(input_directory, filename)
img = Image.open(file_path)
new_filename = os.path.splitext(filename)[0] + '.png'
output_file_path = os.path.join(output_directory, new_filename)
img.save(output_file_path, 'PNG')
print(f"Converted {filename} to {new_filename}")
# Example usage
jpg_to_png('path/to/jpg/files', 'path/to/output/png/files')Script 16: QR Code Generator
Creates a QR code image from a given string.
import qrcode
def create_qr_code(data, filename):
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save(filename)
# Example usage
data = "https://www.example.com"
filename = "example_qr.png"
create_qr_code(data, filename)
print(f"QR code has been saved as {filename}")Script 17: Word Cloud Generator
Generates and optionally saves a word‑cloud image from a text string.
from wordcloud import WordCloud
import matplotlib.pyplot as plt
def create_word_cloud(text, filename=None):
wordcloud = WordCloud(
width=800,
height=400,
background_color='white',
max_words=100,
contour_width=3,
contour_color='steelblue'
).generate(text)
plt.figure(figsize=(10, 5))
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
if filename:
plt.savefig(filename, format="png")
print(f"Word cloud saved as {filename}")
else:
plt.show()
# Example usage
text = "Python is an interpreted high-level general-purpose programming language. Python's design philosophy emphasizes code readability with its notable use of significant indentation."
create_word_cloud(text, "wordcloud.png")Script 18: Chinese Holiday Marker
Prints whether each date in a range is a Chinese public holiday.
import holidays
from datetime import date, timedelta
def is_holiday(date_obj):
cn_holidays = holidays.CountryHoliday('CN')
return date_obj in cn_holidays
def mark_holidays(start_date, end_date):
current_date = start_date
while current_date <= end_date:
if is_holiday(current_date):
print(f"{current_date}: Holiday - {holidays.CountryHoliday('CN').get(current_date)}")
else:
print(f"{current_date}: Not a holiday")
current_date += timedelta(days=1)
# Example usage
mark_holidays(date(2024, 1, 1), date(2024, 12, 31))Script 19: Chinese to Pinyin Converter
Shows how to convert Chinese characters to pinyin with and without tone marks.
from pypinyin import lazy_pinyin, pinyin
# Without tones
result = lazy_pinyin("中国")
print(result) # ['zhong', 'guo']
# With tone numbers (TONE3)
result_with_tone = pinyin("中国", heteronym=False)
print(result_with_tone) # [['zhong1'], ['guo2']]
# With heteronym (multiple pronunciations)
result_heteronym = pinyin("好", heteronym=True)
print(result_heteronym) # e.g., [['hao3'], ['hao4']]Script 20: Color Picker (RGB and Hex)
GUI tool built with Tkinter that lets users pick a color and copies its RGB/Hex values to the clipboard.
import tkinter as tk
from tkinter import colorchooser, Tk, Button, Label, StringVar
def choose_color():
color = colorchooser.askcolor(title="请选择颜色")
if color[1] is None:
return
color_label.config(bg=color[1])
rgb_value.set(f"RGB: {color[0]}")
hex_value.set(f"Hex: {color[1]}")
def copy_to_clipboard(value):
root.clipboard_clear()
root.clipboard_append(value)
root = Tk()
root.title("颜色选择器")
color_label = Label(root, width=20, height=5, bg="white")
color_label.pack(pady=10)
Button(root, text="选择颜色", command=choose_color).pack(pady=5)
rgb_value = StringVar()
hex_value = StringVar()
Label(root, textvariable=rgb_value).pack(pady=5)
Label(root, textvariable=hex_value).pack(pady=5)
Button(root, text="复制RGB", command=lambda: copy_to_clipboard(rgb_value.get())).pack(pady=5)
Button(root, text="复制Hex", command=lambda: copy_to_clipboard(hex_value.get())).pack(pady=5)
root.mainloop()The remainder of the original page contains promotional material and links to additional Python learning resources, which are omitted here to keep the focus on the technical content.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.