Python Forum
Call a .xlsx file outside a class
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Call a .xlsx file outside a class
#1
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
Reply
#2
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?
- Everybody is a genius. But if you judge a fish by its ability to climb a tree, it will live its whole life believing that it is stupid. Albert Einstein
Reply
#3
(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!!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,431 Nov-09-2023, 10:56 AM
Last Post: mg24
  How to call a class in other class? 3lnyn0 3 924 Oct-24-2022, 09:18 AM
Last Post: GenTorossi
  Convert legacy print file to XLSX file davidm 1 1,803 Oct-17-2021, 05:08 AM
Last Post: davidm
  guys please help me , pycharm is not able to identify my xlsx file CrazyGreenYT7 1 2,009 Jun-13-2021, 02:22 PM
Last Post: Larz60+
  Python3 doesn't populate xlsx file with openpyxl Auldyin75 2 2,532 Feb-16-2021, 12:00 PM
Last Post: Auldyin75
  how do i store then call a mp3 from sqlite3 .db file gr3yali3n 3 5,726 Dec-11-2020, 10:28 AM
Last Post: snippsat
  subprocess call cannot find the file specified RRR 6 16,573 Oct-15-2020, 11:29 AM
Last Post: RRR
  Call a python file with anothr python file skaailet 3 2,305 Apr-30-2020, 03:41 PM
Last Post: deanhystad
  Is it mandatory to call superclass init inside the class init? psolar 3 6,033 Feb-14-2020, 09:16 PM
Last Post: wavic
  How to Call a method of class having no argument dataplumber 7 6,441 Oct-31-2019, 01:52 PM
Last Post: dataplumber

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020