Python Forum
Call a .xlsx file outside a class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Call a .xlsx file outside a class (/thread-26228.html)



Call a .xlsx file outside a class - criscferr - Apr-24-2020

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:

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 med1
and 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


RE: Call a .xlsx file outside a class - Jeff900 - Apr-24-2020

You need to understand that when you run import calculus, the code will run. After that you could create an Tentativa() object. You can use the __init__ for that of write an function for it. When using __init__, the class can look like this.

class Tentativa():
    def __init__(self, soma, count, col_data, col_new, lin_new, filename):
        self.soma = soma
        self.count = count
        self.col_data = col_data
        self.col_new = col_new
        self.lin_new = lin_new
        self.filename = filename
Then after importing the class you can create an object. Like this.

obj = calculus.Tentativa(1, 2, 3, 4, 5, 'filename')
You can use obj.filename as reference to the filename now. Does this solve your problem?


RE: Call a .xlsx file outside a class - criscferr - Apr-24-2020

(Apr-24-2020, 03:42 PM)Jeff900 Wrote: You need to understand that when you run import calculus, the code will run. After that you could create an Tentativa() object. You can use the __init__ for that of write an function for it. When using __init__, the class can look like this.

class Tentativa():
    def __init__(self, soma, count, col_data, col_new, lin_new, filename):
        self.soma = soma
        self.count = count
        self.col_data = col_data
        self.col_new = col_new
        self.lin_new = lin_new
        self.filename = filename
Then after importing the class you can create an object. Like this.

obj = calculus.Tentativa(1, 2, 3, 4, 5, 'filename')
You can use obj.filename as reference to the filename now. Does this solve your problem?

Yes! It works. I just had to put in __init__ and in the function "calculo" here:

def calculo(self):
        import openpyxl as xl
        
        filename = self.filename #<------ also put here 
        wb = xl.load_workbook(filename)
        sheet = wb['Sheet1'] #this is object
Thank you so much!!!