Automating My Freelance Workflow with Python: A Step-by-Step Guide
As a freelance developer, I've learned that automation is key to increasing productivity and reducing the time spent on repetitive tasks. In this article, I'll share how I use Python to automate my freelance workflow, from project management to invoicing.
Introduction to Automation
Automation is the process of using software or machines to perform tasks that would otherwise be done manually. By automating tasks, I can free up more time to focus on high-priority tasks, such as coding and client communication. Python is an ideal language for automation due to its simplicity, flexibility, and extensive library support.
Step 1: Project Management with Trello and Python
I use Trello to manage my projects, and Python to automate tasks such as creating new boards, lists, and cards. I'll use the requests library to interact with the Trello API.
import requests
# Trello API credentials
api_key = "your_api_key"
api_token = "your_api_token"
# Create a new board
board_name = "New Project"
response = requests.post(
f"https://api.trello.com/1/boards/",
params={"name": board_name, "key": api_key, "token": api_token}
)
# Create a new list
list_name = "To-Do"
response = requests.post(
f"https://api.trello.com/1/lists",
params={"name": list_name, "idBoard": response.json()["id"], "key": api_key, "token": api_token}
)
# Create a new card
card_name = "Task 1"
response = requests.post(
f"https://api.trello.com/1/cards",
params={"name": card_name, "idList": response.json()["id"], "key": api_key, "token": api_token}
)
Step 2: Time Tracking with Python
I use a Python script to track the time spent on each task. The script uses the datetime module to record the start and end times of each task.
import datetime
# Start time
start_time = datetime.datetime.now()
# End time
end_time = datetime.datetime.now()
# Calculate elapsed time
elapsed_time = end_time - start_time
# Save time log to file
with open("time_log.txt", "a") as f:
f.write(f"Task: {task_name}, Start Time: {start_time}, End Time: {end_time}, Elapsed Time: {elapsed_time}\n")
Step 3: Invoicing with Python
I use a Python script to generate invoices based on the time log. The script uses the pdfkit library to create a PDF invoice.
import pdfkit
# Load time log
with open("time_log.txt", "r") as f:
time_log = f.readlines()
# Calculate total hours
total_hours = 0
for line in time_log:
elapsed_time = datetime.datetime.strptime(line.split(",")[3].split(":")[1], "%H:%M:%S") - datetime.datetime.strptime(line.split(",")[2].split(":")[1], "%H:%M:%S")
total_hours += elapsed_time.total_seconds() / 3600
# Create invoice
invoice_html = f"""
<html>
<body>
<h1>Invoice</h1>
<p>Client: {client_name}</p>
<p>Project: {project_name}</p>
<p>Hours: {total_hours}</p>
<p>Rate: ${hourly_rate}</p>
<p>Total: ${total_hours * hourly_rate}</p>
</body>
</html>
"""
# Save invoice to PDF
pdfkit.from_string(invoice_html, "invoice.pdf")
Top comments (0)