Spending hours to save minutes

I often find myself happily exploring rabbit holes in digital forensics, time discipline is not a strength of mine. So in order to better manage my time and enhance my productivity, the clear choice was to spend (many) hours learning Python to automate some of my day to day activities. The “Case management script” (which doesn’t manage anything, btw… I’m not good at naming things) was my first real undertaking with Python.

When I am beginning a new case at my day job, I will typically create a top-level folder with the case number, followed by a short one or two word description. Within this folder, I create a .txt file for scratch notes, a “Reports” folder for… reports, a report template, and a folder for each item being examined. An example might be:

>25031234 Responsible Use of Time
   >1FC
   >2FC
   >3FC
   >Reports
      Report Template.docx
      >1FC Report
      >2FC Report
   Worklog.txt

To avoid the laborious task of making folders manually and copying template files, I made a script to automate the process. The script started out as a simple command line interface which would create the folders according to a hard-coded destination directory and copy a report template from a hard-coded template location. The files would be named based upon user input from the CLI.

After a while, the GUI bug started gnawing at me. I had already gone through the process of tinkering with TKinter, PySimpleGUI and PyQT6 with the “Warrant Builder” program, so I figured the CLI program that was working fine for the past couple years needed a revamp.

There isn’t much more to do with a program that was only ever intended to set up blank folders and copy templates, but that didn’t stop me from trying to make it more complicated. The new GUI version of the program allows for certain default values to be stored in a local .json file while still requesting a case number and item numbers from the user.

Once completed, the application will generate the directory structure as before, but will leverage a user defined output and template location instead of being hard-coded. Additionally, the application makes some small entries into the template using the Python library “docxtpl”. This is the same document editing library that enables the warrant builder program to run.

By including some loops in the docxtpl tagging, I was able to get the program to iterate through the item numbers and expand tables as well as duplicate tables for each item number entered. Because I had to search through a bunch of old toubleshooting posts with docxtpl, I’ll share the only “complex” part of the docx tagging:

The following block was used for a table designed to include information about a specific item number. A separate table was desired for each item number. To accomplish this, the dictionary passing values to docxtpl contained a list of item numbers under the key “item_list”. This list was iterated through and a table was produced for each item on the list:

{% for item in item_list %}
Content to be repeated, item number can be inserted with: 
{{ item }}
Once the content to be repeated is done, end with:
{% endfor %}

There was also a table which needed to expand with the addition of each item. To accomplish this, another for loop was used, but the beginning and ending tags needed to exist within a merged row of the table. The loop doesn’t wok correctly if the row isn’t merged. The code for expanding the table is represented in expertly crafted ASCII art below:

-----------------------------------------------
|{%tr for item in item_list %}                |
-----------------------------------------------
|{{ item }}             |                     | 	
-----------------------------------------------
|{%tr endfor %}                               |
-----------------------------------------------

Nothing about the program is particularly earth shattering, but I do enjoy the satisfaction of shaving minutes off of my process and getting consistent results. I’m probably not breaking even on the time investment before I retire, but I had fun! If you think the program sounds like something you could get use out of, please go give it a try and let me know what you think!

https://github.com/Whee30/Case-Management-script

If you want to read up on docxtpl, the documentation can be found here:

https://docxtpl.readthedocs.io/en/latest/

Leave a Reply

Your email address will not be published. Required fields are marked *

Digital Forensics and general nerdery. Learning bit by bit (heh) and fighting off imposter syndrome. Learning python, adapting it to my work and overcomplicating simple processes most of the time.