DEV Community

Caper B
Caper B

Posted on

How I Automate My Freelance Workflow with Python

How I Automate My Freelance Workflow with Python

As a freelance developer, managing multiple projects and clients can be overwhelming. To streamline my workflow and increase productivity, I've turned to Python for automation. In this article, I'll share my step-by-step approach to automating tasks, from project setup to invoicing, using Python scripts and tools.

Project Setup Automation

When a new project comes in, I need to set up a repository, create a new branch, and initialize a basic project structure. I've written a Python script using the gitpython library to automate this process.

import git
from git import Repo

def create_project(repo_name, branch_name):
    # Create a new repository
    repo = Repo.init(repo_name)

    # Create a new branch
    repo.git.checkout('-b', branch_name)

    # Initialize a basic project structure
    repo.git.mkdir('src')
    repo.git.mkdir('tests')

    # Commit the initial changes
    repo.git.add(all=True)
    repo.git.commit('-m', 'Initial commit')

# Example usage
create_project('my-new-project', 'main')
Enter fullscreen mode Exit fullscreen mode

This script saves me around 10 minutes per project, and I can use that time to focus on more important tasks.

Time Tracking Automation

Accurate time tracking is crucial for freelancers to bill clients correctly. I use the pytz and datetime libraries to create a simple time tracking script.

import pytz
from datetime import datetime

def track_time(project_name, start_time, end_time):
    # Calculate the time difference
    time_diff = end_time - start_time

    # Convert the time difference to hours and minutes
    hours = time_diff.seconds // 3600
    minutes = (time_diff.seconds // 60) % 60

    # Print the time tracked
    print(f'Time tracked for {project_name}: {hours} hours {minutes} minutes')

# Example usage
start_time = datetime.now(pytz.utc)
# ... work on the project ...
end_time = datetime.now(pytz.utc)
track_time('my-project', start_time, end_time)
Enter fullscreen mode Exit fullscreen mode

This script helps me keep track of the time spent on each project, making it easier to generate invoices.

Invoicing Automation

Creating invoices can be a tedious task, especially when dealing with multiple clients and projects. I use the fpdf library to generate invoices programmatically.

from fpdf import FPDF

def generate_invoice(client_name, project_name, hours_worked, rate):
    # Create a new PDF
    pdf = FPDF()

    # Add a page
    pdf.add_page()

    # Set the font and size
    pdf.set_font('Arial', size=15)

    # Add the client name and project name
    pdf.cell(200, 10, txt=f'Invoice for {client_name} - {project_name}', ln=True, align='C')

    # Add the hours worked and rate
    pdf.set_font('Arial', size=12)
    pdf.cell(200, 10, txt=f'Hours worked: {hours_worked}', ln=True, align='L')
    pdf.cell(200, 10, txt=f'Rate: ${rate}/hour', ln=True, align='L')

    # Calculate the total amount
    total_amount = hours_worked * rate

    # Add the total amount
    pdf.set_font('Arial', size=15)
    pdf.cell(200, 10, txt=f'Total amount: ${total_amount}', ln=True, align='R')

    # Save the PDF
    pdf.output('invoice.pdf')

# Example usage
generate_invoice('John Doe', 'my-project', 10, 50)
Enter fullscreen mode Exit fullscreen mode

This script generates a professional

Top comments (0)