Apr-24-2020, 01:47 PM
My code works fine. My doubt is how can I call a .xlsx file outside a class and use inside the class. I need to call a bunch of different .xlsx files and want to automate the call. In part of the code I did this in functions, but now inside the class I don't figure out how to do this.
This is my principal and I want to call the file .xlsx just here:
This is my principal and I want to call the file .xlsx just here:
import calculus2 filename= 'dadocru_test1.xlsx' #(here I want to call the file) calculus2.calc_temp_inlet(filename) calculus2.calc_temp_outlet(filename) calculus2.calc_vazao(filename) calculus2.calc_potencia(filename) calculus2.calc_diff_temp(filename) calculus2.calc_tempo(filename) calculus2.extra_calcs(filename) calculus2.interpol_thermo(filename) calculus2.pot_util(filename)but I have this class:
class Tentativa(): def __init__(self, soma, count, col_data, col_new, lin_new): self.soma = soma self.count = count self.col_data = col_data self.col_new = col_new self.lin_new = lin_new def calculo(self): import openpyxl as xl wb = xl.load_workbook('dadocru_test1.xlsx') #(here I have to call the file again. I don't want this) sheet = wb['Sheet1'] #this is object soma = int(self.soma) count = self.count col_data = self.col_data col_new = self.col_new lin_new = self.lin_new for row in range(60, sheet.max_row+1): cell1 = sheet.cell(row,col_data) actual = cell1.value soma += actual count += 1 med1 = soma/count # new_cell1 = sheet.cell(lin_new, col_new) acho q não precisa # new_cell1.value = med1 return med1and here is an example of how I could call in the functions:
import openpyxl as xl def calc_temp_inlet(filename): wb = xl.load_workbook(filename) #(here I can access the file with my principal) sheet = wb['Sheet1'] #this is object sheet.cell(1, 4).value = "Temp_ent" for row in range(2, sheet.max_row+1): #+1 to include 4 cell = sheet.cell(row, 3) corrected_price = (cell.value - 100.0820374)/0.3854 corrected_price_cell = sheet.cell(row, 4) corrected_price_cell.value = corrected_price wb.save(filename)I don't know how to name this doubt. In my head I know what I want, but I don't know how to search this in google. If someone understood and could help me. I would appreciate. Thanks