Advertisement
Guest User

Untitled

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