DEV Community

Caper B
Caper B

Posted on

Automating My Freelance Workflow with Python: A Step-by-Step Guide

Automating My Freelance Workflow with Python: A Step-by-Step Guide

As a freelance developer, I'm always looking for ways to streamline my workflow and increase productivity. One of the most effective tools I've found for achieving this is Python. In this article, I'll walk you through the specific steps I take to automate my freelance workflow using Python, including code examples and a monetization angle.

Step 1: Client Onboarding

The first step in my freelance workflow is client onboarding. This involves sending a welcome email, contract, and invoice to the client. I use Python's smtplib library to automate this process. Here's an example code snippet:

import smtplib
from email.mime.text import MIMEText

def send_welcome_email(client_email, client_name):
    # Define email parameters
    sender_email = "your_email@gmail.com"
    sender_password = "your_password"
    subject = "Welcome to My Freelance Services"

    # Create email body
    body = f"Dear {client_name}, welcome to my freelance services! Please find attached your contract and invoice."

    # Create email message
    msg = MIMEText(body)
    msg["Subject"] = subject
    msg["From"] = sender_email
    msg["To"] = client_email

    # Send email
    server = smtplib.SMTP("smtp.gmail.com", 587)
    server.starttls()
    server.login(sender_email, sender_password)
    server.sendmail(sender_email, client_email, msg.as_string())
    server.quit()

# Example usage
send_welcome_email("client_email@example.com", "John Doe")
Enter fullscreen mode Exit fullscreen mode

This code sends a welcome email to the client with their contract and invoice attached.

Step 2: Time Tracking

Next, I need to track the time spent on each project. I use Python's datetime library to create a simple time tracking script. Here's an example code snippet:

import datetime

def track_time(project_name):
    # Define time tracking parameters
    start_time = datetime.datetime.now()
    end_time = None

    # Start time tracking
    input("Press Enter to start tracking time...")

    # End time tracking
    input("Press Enter to stop tracking time...")
    end_time = datetime.datetime.now()

    # Calculate time spent
    time_spent = end_time - start_time

    # Save time spent to file
    with open("time_log.txt", "a") as f:
        f.write(f"{project_name}: {time_spent}\n")

# Example usage
track_time("Project X")
Enter fullscreen mode Exit fullscreen mode

This code tracks the time spent on a project and saves it to a file.

Step 3: Invoice Generation

Once I've tracked the time spent on a project, I need to generate an invoice. I use Python's fpdf library to create a simple invoice generator. Here's an example code snippet:


python
from fpdf import FPDF

def generate_invoice(project_name, time_spent, hourly_rate):
    # Define invoice parameters
    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Arial", size = 15)

    # Add invoice header
    pdf.cell(200, 10, txt = "Invoice", ln = True, align = 'C')

    # Add project details
    pdf.cell(200, 10, txt = f"Project: {project_name}", ln = True, align = 'L')
    pdf.cell(200, 10, txt = f"Time Spent: {time_spent}", ln = True, align = 'L')
    pdf.cell(200, 10, txt = f"Hourly Rate: ${hourly_rate}", ln = True, align = 'L')

    # Add invoice total
    total = time_spent * hourly_rate
Enter fullscreen mode Exit fullscreen mode

Top comments (0)