Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.98 KB | None | 0 0
  1. import ctypes
  2. import pandas as pd
  3. import os.path
  4. import win32com.client
  5. import os
  6.  
  7. def Mbox(title, text, style):
  8.     return ctypes.windll.user32.MessageBoxW(0, text, title, style)
  9.  
  10. def run_macro(pathof):
  11.     try:
  12.         xlApp = win32com.client.DispatchEx('Excel.Application')
  13.         xlsPath = os.path.expanduser(pathof)
  14.         wb = xlApp.Workbooks.Open(Filename=xlsPath)
  15.         xlApp.Run('importCSV')
  16.         wb.Close(True)
  17.         xlApp.Quit()
  18.     except:
  19.         wb.Close(True)
  20.         xlApp.Quit()
  21.  
  22.  
  23. def main():
  24.  
  25.  
  26.     excel_name = "Audit_Soft.xlsm"
  27.     sheet_name = "Main"
  28.     table_Sheet = "Group Table"
  29.     output_name = "output.csv"
  30.  
  31.     excel_path = os.path.dirname(os.path.realpath('__file__'))
  32.     excel_path = os.path.join(excel_path, excel_name)
  33.    
  34.     df=pd.read_excel(excel_path, sheet_name=sheet_name, header=None)
  35.  
  36.     column_name = df.iloc[2,1]
  37.     txt_path = df.iloc[1,0]
  38.  
  39.     #reading txt data file line by line
  40.     with open(txt_path) as f:
  41.         lines = []
  42.         for line in f:
  43.             if line.strip() == "":
  44.                 continue
  45.             lines.append(line.strip())
  46.  
  47.     masterdb = []
  48.  
  49.     #saving txt db into array
  50.     for line_index, _ in enumerate(lines):
  51.         masterdb.append([lines[line_index].split()[0], lines[line_index].split()[1]])
  52.     ##################
  53.  
  54.     df = pd.read_excel(excel_name, sheet_name=table_Sheet)
  55.     cpt_list = df[column_name].tolist()
  56.  
  57.     results = []
  58.  
  59.     col1, col2 = set(), set()
  60.     for master_elem in masterdb:
  61.         col1.add(master_elem[0])
  62.         col2.add(master_elem[1])
  63.  
  64.     for elem in cpt_list:
  65.         to_append = ""
  66.         if elem in col1:
  67.             to_append = "Col1"
  68.         elif elem in col2:
  69.             to_append = "Col2"
  70.         results.append(to_append)
  71.  
  72.     with open(output_name, 'w') as file_handler:
  73.         file_handler.write("\n".join(results))
  74.  
  75.     run_macro(excel_path)
  76.     os.startfile(excel_path)
  77.  
  78. if __name__ == '__main__':
  79.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement