HR Process Automation Scripts: Resume Parsing, Interview Emails, Pay‑Slip Generation, and Data Analysis
This article presents a collection of Python scripts that automate key HR tasks—including resume parsing, interview invitation emailing, pay‑slip creation, hiring‑channel analysis, turnover calculation, satisfaction survey reporting, salary competitiveness comparison, employee data updates, Word document generation, and leave‑request handling—complete with ready‑to‑run code examples.
This guide provides a series of Python automation scripts for common HR workflows.
1. Resume Parsing : Extracts name and email from a resume file using regular expressions.
import re
from os import path
def parse_resume(file_path):
with open(file_path, 'r', encoding='utf-8') as file:
content = file.read()
name_pattern = re.compile(r"(?<=Name:|姓名:).*?(?=\n|$)", re.IGNORECASE)
email_pattern = re.compile(r"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}", re.IGNORECASE)
name = re.findall(name_pattern, content)
email = re.findall(email_pattern, content)
print(f"Name: {name}\nEmail: {email}")
# Example usage
parse_resume('path_to_your_resume.txt')2. Interview Invitation Email Sending : Reads a CSV of candidates and sends personalized interview invitations via SMTP.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import csv
def send_interview_email(subject, body, candidates_file, smtp_server, port, sender, password):
with open(candidates_file, newline='', encoding='utf-8') as csvfile:
reader = csv.reader(csvfile)
next(reader) # Skip header
for row in reader:
name, email = row
msg = MIMEMultipart()
msg['From'] = sender
msg['To'] = email
msg['Subject'] = subject
msg.attach(MIMEText(body.format(name=name), 'plain'))
server = smtplib.SMTP(smtp_server, port)
server.starttls()
server.login(sender, password)
server.sendmail(sender, email, msg.as_string())
server.quit()
# Example usage
send_interview_email(
"Interview Invitation",
"Dear {name}, you are invited for an interview.",
"candidates.csv",
"smtp.example.com",
587,
"[email protected]",
"your_password"
)3. Pay‑Slip Generation : Reads an Excel file of employee salaries and creates individual text pay‑slip files.
import pandas as pd
def generate_pay_slips(data_path, output_path):
df = pd.read_excel(data_path)
for idx, row in df.iterrows():
slip_content = f"""
Employee ID: {row['Employee ID']}
Name: {row['Name']}
Salary: {row['Salary']}
Month: {row['Month']}
Year: {row['Year']}
"""
with open(f"{output_path}/{row['Employee ID']}_pay_slip.txt", 'w') as file:
file.write(slip_content)
# Example usage
generate_pay_slips("employees_salary.xlsx", "pay_slips")4. Hiring‑Channel Data Analysis : Summarizes hiring results per source and calculates hire rates.
import pandas as pd
def analyze_hiring_channels(channel_data_path):
df = pd.read_csv(channel_data_path)
channel_summary = df.groupby('Source')['Hired'].sum().reset_index()
channel_summary.rename(columns={'Hired': 'Total Hires'}, inplace=True)
channel_summary['Hire Rate'] = (channel_summary['Total Hires'] / df['Hired'].sum()) * 100
print(channel_summary.sort_values('Total Hires', ascending=False))
# Example usage
analyze_hiring_channels("hiring_channels.csv")5. Turnover Rate Calculation : Computes overall employee turnover percentage.
import pandas as pd
def calculate_turnover_rate(employee_data_path):
df = pd.read_csv(employee_data_path)
total_employees = len(df)
resigned = df[df['Status'] == 'Resigned'].shape[0]
turnover_rate = (resigned / total_employees) * 100
print(f"Turnover Rate: {turnover_rate:.2f}%")
# Example usage
calculate_turnover_rate("employee_records.csv")6. Employee Satisfaction Survey Analysis : Calculates average satisfaction score and distribution.
import pandas as pd
def analyze_satisfaction_survey(survey_data_path):
df = pd.read_csv(survey_data_path)
avg_satisfaction = df['Satisfaction Score'].mean()
print(f"Average Satisfaction Score: {avg_satisfaction:.2f}")
satisfaction_counts = df['Satisfaction Score'].value_counts(normalize=True) * 100
print(satisfaction_counts)
# Example usage
analyze_satisfaction_survey("satisfaction_survey.csv")7. Salary Competitiveness Comparison : Merges company and market salary data to compute a competitive ratio.
import pandas as pd
def compare_salaries(company_salaries_path, market_salaries_path):
company_df = pd.read_csv(company_salaries_path)
market_df = pd.read_csv(market_salaries_path)
merged_df = pd.merge(company_df, market_df, on='Job Title', how='outer')
merged_df['Competitive Ratio'] = merged_df['Company Salary'] / merged_df['Market Salary']
print(merged_df[['Job Title', 'Company Salary', 'Market Salary', 'Competitive Ratio']])
# Example usage
compare_salaries("company_salaries.csv", "market_salaries.csv")8. Bulk Employee Information Update : Updates an employee CSV with changes from another file.
import pandas as pd
def update_employee_info(input_file, updates_file, output_file):
df = pd.read_csv(input_file)
updates = pd.read_csv(updates_file)
df.update(updates, overwrite=True)
df.to_csv(output_file, index=False)
# Example usage
update_employee_info("employees.csv", "updates.csv", "updated_employees.csv")9. Word Document Automation : Fills a Word template with employee data to generate personalized documents.
from docx import Document
import pandas as pd
def fill_word_template(template_path, data_path, output_path):
df = pd.read_csv(data_path)
doc = Document(template_path)
for index, row in df.iterrows():
for paragraph in doc.paragraphs:
for run in paragraph.runs:
if '{' in run.text and '}' in run.text:
field_name = run.text.strip('{} ')
if field_name in row:
run.text = str(row[field_name])
doc.save(f"{output_path}/employee_{row['Employee ID']}.docx")
# Example usage
fill_word_template("template.docx", "employee_data.csv", "generated_docs")10. Leave‑Request Email Automation : Connects to an IMAP server, detects unread leave‑request emails, extracts dates (placeholder logic), and sends a confirmation reply.
import imaplib
import email
from email.mime.text import MIMEText
import smtplib
def fetch_emails(imap_server, username, password):
mail = imaplib.IMAP4_SSL(imap_server)
mail.login(username, password)
mail.select('inbox')
_, data = mail.search(None, 'UNSEEN')
mail_ids = data[0].split()
for id in mail_ids:
_, msg_data = mail.fetch(id, '(RFC822)')
msg = email.message_from_bytes(msg_data[0][1])
process_email(msg)
def process_email(msg):
from_email = email.utils.parseaddr(msg['From'])[1]
subject = msg['Subject']
if "休假申请" in subject:
body = ""
if msg.is_multipart():
for part in msg.walk():
ctype = part.get_content_type()
cdispo = str(part.get('Content-Disposition'))
if ctype == 'text/plain' and 'attachment' not in cdispo:
body = part.get_payload(decode=True).decode('utf-8')
break
else:
body = msg.get_payload(decode=True).decode('utf-8')
date_start, date_end = extract_dates(body)
send_confirmation_email(from_email, date_start, date_end)
def send_confirmation_email(to_email, start_date, end_date):
sender_email = "[email protected]"
sender_password = "your_email_password"
smtp_server = "smtp.example.com"
port = 587
subject = "休假申请已收到"
body = f"您好,\n\n您的休假申请(从{start_date}至{end_date})已收到。我们将尽快处理并回复您。\n\n祝好,\n人力资源部"
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender_email
msg['To'] = to_email
server = smtplib.SMTP(smtp_server, port)
server.starttls()
server.login(sender_email, sender_password)
server.sendmail(sender_email, to_email, msg.as_string())
server.quit()
def extract_dates(text):
# Placeholder: implement actual date extraction logic
date_start = "需要解析的起始日期"
date_end = "需要解析的结束日期"
return date_start, date_end
# Example usage (replace with real credentials)
fetch_emails("imap.example.com", "your_username", "your_password")Each script is self‑contained, uses standard Python libraries, and can be adapted to specific organizational needs, offering a practical toolkit for automating repetitive HR processes.
Test Development Learning Exchange
Test Development Learning Exchange
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.