def write(self): self.workbook = Workbook() self.sheet = self.workbook.get_active_sheet() if not len(self.header): raise BRException("Header not provided") if self.data and len(self.header) != len(self.data[0]): raise BRException("Header and Data must be in same format") # Write headers row = 1 col = 1 for col_header in self.header: self.sheet.cell(row=row, column=col, value=col_header) col += 1 row += 1 # Now write data for data_row in self.data: col = 1 for col_value in data_row: col_value = col_value if col_value else '' self.sheet.cell(row=row, column=col, value=col_value) col += 1 row += 1 if self.response: self.workbook.save(self.response) else: if self.file_name: self.workbook.save(self.file_name)
def read_file(self): self.workbook = openpyxl.load_workbook(self.file_name) self.sheet_name = self.kwargs.get('sheet_name') all_sheets = self.workbook.get_sheet_names() if all_sheets: if self.sheet_name: if self.sheet_name in all_sheets: pass else: self.sheet_name = all_sheets[0] else: self.sheet_name = all_sheets[0] else: raise BRException("No sheets found") self.sheet = self.workbook.get_sheet_by_name(self.sheet_name) self.start_row = self.kwargs.get('start_row') if self.kwargs.get( 'start_row') else 2 self.start_column = self.kwargs.get('start_col') if self.kwargs.get( 'start_col') else 2 contents = [] for row in self.sheet.iter_rows(): row_list = [] for cell in row: row_list += [cell.value] contents += [row_list] return contents
def data_as_list(self): if not self.data: raise BRException("No data found") data_list = [] for currency_code, name in self.data.items(): data_list += [[currency_code.upper(), name]] return data_list
def data_as_list(self): if not self.data: raise BRException("No data found") data_list = [] for entry in self.data: data_list += [[entry['alpha2'], entry['English']]] return data_list
def data_as_list(self): if not self.data: raise BRException("No data found") currency_list, lan_list = [], [] for entry in self.data: for c in entry['currencies']: currency_list += [[ entry['name'], entry['alpha2Code'], entry['alpha3Code'], c ]] for l in entry['languages']: lan_list += [[ entry['name'], entry['alpha2Code'], entry['alpha3Code'], l ]] data_list = [currency_list, lan_list] return data_list
def download(self, data=None, writer=None, *args, **kwargs): if data is None: return if not type(data) is list and not type(data) is tuple: return headers = kwargs.get("headers", []) response = kwargs.pop("response", None) writer_class = writer if writer else self.get_default_writer() if issubclass(writer_class, Writter): writer_instance = writer_class(data=data, header=headers, file_path=self.file_name, response=response, *args, **kwargs) writer_instance.write() return response if response else self.file_name else: raise BRException("Writer must be an instance of Writter")
def ensure_status(self, data): if data and not data.get('status'): raise BRException("Ajax response does not contain status field") return data