Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.06 KB | None | 0 0
  1. import pandas as pd
  2. import pythoncom
  3. import threading
  4. import time
  5. import xlwings as xw
  6.  
  7. from concurrent.futures import ThreadPoolExecutor
  8. from rx import Observer
  9. from string import ascii_uppercase
  10.  
  11.  
  12. def create_curr_selection_df(curr_row_df, selection):
  13.     if len(selection) == 3:
  14.         column_ids = [selection[1]]
  15.     else:
  16.         column_ids = [x for x in (selection[1], selection[3])]
  17.     curr_selection_df = curr_row_df.loc[:, column_ids]
  18.     print(curr_row_df)
  19.     print(selection)
  20.     return curr_selection_df
  21.  
  22.  
  23. def monitor_loop(app: xw.App, observer: Observer, end_event: threading.Event):
  24.     pythoncom.CoInitialize()
  25.     workbook = xw.books.active
  26.     sheet = workbook.sheets.active
  27.     all_column_values = sheet.range(xw.Range('L2:Q2')).value
  28.     column_to_header = {column_id: header for column_id, header in zip(ascii_uppercase[11:17], all_column_values)}
  29.     while not end_event.is_set():
  30.         # If you want to get current sheet name use: workbook.selection.sheet.name
  31.         curr_selection = workbook.selection.address
  32.         if workbook.selection.address != curr_selection:
  33.             selection_address = workbook.selection.address.split('$')
  34.             row_idx = selection_address[-1]
  35.             cells_range_to_fetch = xw.Range(''.join(['L', row_idx, ':', 'Q', row_idx]))
  36.  
  37.             curr_data_frame = sheet.range(cells_range_to_fetch).options(pd.DataFrame, index=False, header=False).value
  38.             curr_data_frame.columns = all_column_values
  39.  
  40.             if len(selection_address) == 3:
  41.                 curr_selection_columns = [column_to_header[selection_address[1]]]
  42.             else:
  43.                 curr_selection_columns = \
  44.                     [column_to_header[letter] for letter in column_to_header.keys()
  45.                      if selection_address[1] <= letter <= selection_address[3]]
  46.  
  47.             # print('\n')
  48.             # print(selection_address)
  49.             # print(workbook.selection.address)
  50.             # print(curr_selection)
  51.  
  52.             curr_selection_df = curr_data_frame.loc[:, curr_selection_columns]
  53.             curr_selection = workbook.selection.address
  54.             observer.on_next((curr_data_frame, all_column_values, row_idx, curr_selection_df))
  55.             time.sleep(0.5)
  56.  
  57. class CellObserver(Observer):
  58.  
  59.     def __init__(self, process_shared_resource):
  60.         self.shared_res = process_shared_resource
  61.  
  62.     def on_next(self, excel_data):
  63.         curr_row_df, all_columns, row_idx, curr_selection_df = excel_data
  64.         self.shared_res.curr_row_df = curr_row_df
  65.         self.shared_res.curr_selection_df = curr_selection_df
  66.         self.shared_res.all_columns = all_columns
  67.         self.shared_res.row_idx = row_idx
  68.  
  69.     def on_completed(self):
  70.         print("done")
  71.  
  72.     def on_error(self, error):
  73.         print('error')
  74.  
  75.  
  76. def launch_monitor_method(process_shared_resource):
  77.     while not xw.apps:
  78.         time.sleep(5)
  79.     evt = threading.Event()
  80.     tpe = ThreadPoolExecutor(1)
  81.     f = tpe.submit(monitor_loop, xw.apps[0], CellObserver(process_shared_resource), evt)
  82.     f.result()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement