Advertisement
ALEXANDAR_GEORGIEV

import_excel

Feb 7th, 2023
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | Source Code | 0 0
  1. from tkinter import *
  2. from tkinter.ttk import Progressbar
  3. from xlwings import Range
  4.  
  5. from Opiti.from_excel.classes.global_inc import registry
  6. from json_file_io import JsonFileIo
  7.  
  8. class ImportExcel():
  9.     def init(self):
  10.         self.cols = None
  11.         self.row_start = None
  12.         self.row_end = None
  13.         self.file_path = None
  14.         self.bar_win = None
  15.  
  16.         self.progress_bar = None
  17.         self.counter_label = None
  18.  
  19.     # cols: Речник с колоните, напр. {"code": 'A', "name": 'B', ... "close_c": 'H'}
  20.     def defineCols(self, cols):
  21.         self.cols = cols
  22.  
  23.     def defineRows(self, row_start: int, row_end: int):
  24.         self.row_start = row_start
  25.         self.row_end = row_end
  26.  
  27.     def defineFilePath(self, file_path: str):
  28.         self.file_path = file_path
  29.  
  30.     def update_progress(self, percent, progress_bar, counter):      # s -> float
  31.         # progress_bar.start(10)  # Започва през 10% , Пуска се заедно с импорта на данни command=step
  32.         progress_bar['value'] = percent  # % Запълване
  33.         counter.config(text=f"{progress_bar['value']} %")
  34.         counter.config()
  35.         self.bar_win.update_idletasks()
  36.  
  37.     def read_excel_write_json_close(self):
  38.  
  39.         data = []
  40.  
  41.         row_start_indx = self.row_start
  42.         row_end_indx = self.row_end
  43.  
  44.         for v in range(row_start_indx, row_end_indx + 1):
  45.  
  46.             row_data = {}
  47.             for col_key in self.cols:
  48.                 col_name = self.cols[col_key]
  49.                 row_data[col_key] = Range(f'{col_name}{v}').value
  50.             data.append(row_data)
  51.  
  52.             progress_percent = ((v - row_start_indx) / (row_end_indx - row_start_indx)) * 100
  53.             print(progress_percent)
  54.             progress_percent = int(progress_percent)
  55.             self.update_progress(progress_percent, self.progress_bar, self.counter_label)
  56.  
  57.         print("ImportExcel.read_excel_write_json_close() data: ", data)
  58.         json_file_io = JsonFileIo(file_name=self.file_path)
  59.         json_file_io.write_content(data)
  60.         #print("AAAAAAAAAAAAAAAAAA")
  61.  
  62.         #self.bar_win.update()
  63.         #self.bar_win.update_idletasks()
  64.  
  65.         self.bar_win.destroy()
  66.         self.bar_win.quit()
  67.  
  68.     def run_import(self):
  69.         '''
  70.  
  71.        rows: Речник с първи и последен ред, напр. {"fist": 5, "last": 81}
  72.        '''
  73.  
  74.         #def bar():
  75.         self.bar_win = Tk()
  76.         #bar_win.title('Импорт Оборотна ведомост')
  77.         self.bar_win.overrideredirect(True)  # Маха title bar
  78.         self.bar_win.geometry('400x80+500+300')
  79.         self.bar_win.configure(background='lightblue')
  80.         text = Label(self.bar_win, text="Прехвърляне на данни от Excel", bg='lightblue', fg='darkblue')
  81.         text.pack(side=TOP, pady=1)
  82.         self.bar_win.update()
  83.  
  84.         self.progress_bar = Progressbar(self.bar_win, orient=HORIZONTAL, length=300, mode='determinate')  # indeterminate
  85.         self.progress_bar.pack(pady=(5, 5))
  86.  
  87.         self.counter_label = Label(self.bar_win, text='value', bg='lightblue')
  88.         self.counter_label.pack(pady=(0, 5))
  89.  
  90.         # # Import trial balance
  91.  
  92.         # count_progress = 0
  93.         #self.read_excel_write_json_close()
  94.  
  95.         self.bar_win.after_idle(lambda: self.read_excel_write_json_close())
  96.  
  97.         self.bar_win.mainloop()
  98.  
  99. if __name__ == '__main__':
  100.  
  101.     import_excel = ImportExcel()
  102.     import_excel.defineCols({"kredit_po": "G", "turn_d": "H", "turn_c": "I", "debit_po": "J"})
  103.     import_excel.defineRows(3, 15)
  104.     import_excel.defineFilePath("..\\rpna_test.txt")
  105.     import_excel.run_import()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement