Writing Excel spreadsheets – xlsx_writer.py

The xlsx_writer.py script contains the logic for creating an excel document containing our processed UserAssist values. In addition to this, this script also creates an additional worksheet that contains summarizing charts of our data. The xlsxwriter is imported on line 1 and is the third-party module we use to create the Excel document. The itemgetter function, imported on line 3, will be used and explained in the sorting functions later in this section. We have seen the datetime and logging modules from previous chapters:

001 from __future__ import print_function
002 import xlsxwriter
003 from operator import itemgetter
004 from datetime import datetime, timedelta
005 import logging

There are six functions in the xlsx_writer.py script. The coordinating logic is handled by the excel_writer() function defined on line 36. This function creates our Excel workbook object and then hands it off to the dashboard_writer() and userassist_writer() functions to create the dashboard and UserAssist worksheets, respectively.

The remaining three functions, file_time(), sort_by_count(), and sort_by_date(), are helper functions used by the dashboard and UserAssist writers. The file_time() function is responsible for converting FILETIME objects that we parsed from the raw UserAssist data into datetime objects. The sorting functions are used to sort the data by either count or date. We use these sorting functions to answer some basic questions about our data. What are the most-used applications? What are the least-used applications? What were the last 10 applications used on the machine (according to UserAssist)?

036 excel_writer(): 
... 
071 dashboard_writer(): 
... 
156 userassist_writer(): 
... 
201 file_time(): 
... 
214 sort_by_count(): 
... 
227 sort_by_date():