Advertisement
justinegh

Python is the Devil

Apr 13th, 2018
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.81 KB | None | 0 0
  1. #! /usr/bin/python3
  2. # pass in 1/1/now.year to current date, get expense report for that date range
  3. # return csv
  4. # ** TODO ** support last known exp ID
  5. def get_expense_report():
  6.     import requests
  7.     import datetime
  8.  
  9.     now = datetime.datetime.now()
  10.     session = requests.Session()
  11.     session.auth = (username, password)
  12.     auth = session.post('https://manage2.cpanel.net/login.cgi')
  13.    
  14.     params = dict(
  15.         user="all",
  16.         card="",
  17.         company="",
  18.         dept="----",
  19.         justified="",
  20.         hasimage="",
  21.         smonth="1",
  22.         sday="1",
  23.         syear=str(now.year),
  24.         emonth=str(now.month),
  25.         eday=str(now.day),
  26.         eyear=str(now.year),
  27.         submit="Filter",
  28.         download=1
  29.     )
  30.     url = 'https://manage2.cpanel.net/list_txn.cgi'
  31.    
  32.     print("Requesting ", url, params)
  33.     response = session.get(
  34.         url, params=params, stream=True, cookies=auth.cookies)
  35.    
  36.     return response
  37.  
  38.  
  39. # pass in csv, return clean list of lists, each row a top level list
  40. def sanitize_expense_report(report):
  41.     import csv
  42.     data = [row for row in csv.reader(report.content.splitlines())]
  43.     return data
  44.  
  45.  
  46. # pass in list of lists, each top level a row
  47. def append_report_to_budget(sanitized_report):
  48.     from openpyxl import load_workbook
  49.     from openpyxl import Workbook
  50.     from pprint import pprint as pp
  51.  
  52.     wb = load_workbook(
  53.         path + 'Product_Dev_Budget.xlsx')
  54.    
  55.     sheet = wb['RAW DATA']
  56.    
  57.     # delete test sheet
  58.     wb.remove(sheet)
  59.  
  60.     wb.save(path + "Product_Dev_Budget.xlsx")
  61.  
  62.  # create new sheet
  63.  
  64.     wb.create_sheet('RAW DATA')
  65.    
  66.     # this line worked previously, but now it returns an error
  67.     active_sheet = wb.get_sheet_by_name("RAW DATA")
  68.  
  69.     # changing the previous line to the following removed the error, but did not return an action.
  70.         # active_sheet = wb['RAW DATA']
  71.    
  72.     for row in sanitized_report:
  73.     # exclude the overall total row
  74.         # removed "Marketing"
  75.         if (row[0] != 'Overall Total: ' and row[1] in ["Documentation", "Product Development", "Quality Assurance"]):
  76.             active_sheet.append(row)
  77.             pp(row[0])
  78.  
  79.             # adjust column widths
  80.             for col in active_sheet.columns:
  81.                  max_length = 0
  82.                  column = col[0].column # Get the column name
  83.                  for cell in col:
  84.                      try: # Necessary to avoid error on empty cells
  85.                          if len(str(cell.value)) > max_length:
  86.                              max_length = len(cell.value)
  87.                      except:
  88.                          pass
  89.                  adjusted_width = (max_length + 2) * 1.2
  90.                  active_sheet.column_dimensions[column].width = adjusted_width
  91.  
  92.                  # apply Wrap Text style and set widths for columns D, E, and G
  93.                  from openpyxl.styles import Alignment
  94.                  wrap_alignment = Alignment(wrap_text=True)
  95.  
  96.                  cell.alignment = wrap_alignment
  97.  
  98.                  active_sheet.column_dimensions['D'].width = 30
  99.                  active_sheet.column_dimensions['E'].width = 30
  100.                  active_sheet.column_dimensions['G'].width = 30
  101.  
  102.                  # hide active_sheet
  103.                  active_sheet.sheet_state = 'hidden'
  104.  
  105.     wb.save(path + "Product_Dev_Budget.xlsx")
  106.  
  107. if __name__ == '__main__':
  108.     import ConfigParser
  109.     data_dir = "/Users/justinehernandez/Desktop/Python/Phil_Python/"
  110.     config = ConfigParser.ConfigParser()
  111.     config.read(data_dir + "auth.config")
  112.     username = config.get("auth", "username")
  113.     password = config.get("auth", "password")
  114.     path = config.get("general", "path")
  115.    
  116.     response = get_expense_report()
  117.     sanitized_report = sanitize_expense_report(response)
  118.     append_report_to_budget(sanitized_report)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement