Automating My Freelance Workflow with Python: A Step-by-Step Guide
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 how I use Python to automate tasks, from project initialization to invoicing, and how it's helped me scale my business.
Project Initialization
When a new project comes in, I create a new directory with the client's name and project details. This involves creating a new Git repository, setting up the project structure, and initializing the necessary files. I've automated this process using a Python script:
import os
import git
def create_project(client_name, project_name):
# Create a new directory for the project
project_dir = f"{client_name}-{project_name}"
os.mkdir(project_dir)
# Initialize a new Git repository
repo = git.Repo.init(project_dir)
# Create a new branch for the project
repo.create_head(f"{client_name}-{project_name}")
# Create the project structure
os.mkdir(f"{project_dir}/src")
os.mkdir(f"{project_dir}/tests")
# Initialize the necessary files
with open(f"{project_dir}/README.md", "w") as f:
f.write(f"# {client_name}-{project_name}")
with open(f"{project_dir}/requirements.txt", "w") as f:
f.write("")
# Example usage:
create_project("john-doe", "website")
This script saves me around 10-15 minutes per project, which may not seem like a lot, but it adds up over time.
Time Tracking
As a freelancer, accurate time tracking is crucial for invoicing clients. I use a Python script to track my time spent on each project:
import datetime
import json
def track_time(project_name, start_time, end_time):
# Calculate the time spent on the project
time_spent = end_time - start_time
# Load the existing time tracking data
try:
with open("time_tracking.json", "r") as f:
time_tracking_data = json.load(f)
except FileNotFoundError:
time_tracking_data = {}
# Update the time tracking data
if project_name not in time_tracking_data:
time_tracking_data[project_name] = []
time_tracking_data[project_name].append({
"start_time": start_time.isoformat(),
"end_time": end_time.isoformat(),
"time_spent": str(time_spent)
})
# Save the updated time tracking data
with open("time_tracking.json", "w") as f:
json.dump(time_tracking_data, f)
# Example usage:
start_time = datetime.datetime.now()
# Work on the project...
end_time = datetime.datetime.now()
track_time("john-doe-website", start_time, end_time)
This script allows me to easily track my time spent on each project and generates a JSON file with the time tracking data.
Invoicing
With the time tracking data, I can generate invoices for my clients. I use a Python script to create invoices based on the time tracking data:
python
import json
import datetime
def generate_invoice(project_name, client_name, hourly_rate):
# Load the time tracking data
with open("time_tracking.json", "r") as f:
time_tracking_data = json.load(f)
# Calculate the total time spent on the project
total_time_spent = datetime.timedelta()
for time_entry in time_tracking_data[project_name]:
start_time = datetime.datetime.fromisoformat(time_entry["start_time"])
end_time = datetime.datetime.fromisoformat(time_entry["end_time"])
time_spent = end_time - start_time
total_time_spent
Top comments (0)