-2

I need to import this path variable from first class to another classes, this is my code:

import openpyxl

class Excell():
      global path
      def send_message(self, data):
          global path
          print("Path Excel is : '{}'".format(data))
          path = data # I need to export this variable 'path'

global path

class First():
      global path
      wb = openpyxl.load_workbook(path)
      sheet = wb['sheet']
      firstCell= sheet["A1"].value
      print("Cell is :" + firstCell)

After run code, I see this message:

C:\Python\python.exe E:/PycharmProjects/test/firstTest.py
Traceback (most recent call last):
   File "E:\PycharmProjects\test\firstTest.py", line 11, in <module>
      class First():
   File "E:\PycharmProjects\test\firstTest.py", line 13, in First
      wb = openpyxl.load_workbook(path)
NameError: name 'path' is not defined

Process finished with exit code 1
  • 2
    With all the respect, your code doesn't make any sense! There are so many questions: why are you using classes? What should this code do? What do you want to achieve? – Grajdeanu Alex Apr 18 '20 at 20:48
  • One problem is that class `First` uses `path` in its class definition... this happens before anything has called `Excell().send_message(data)`. – tdelaney Apr 18 '20 at 21:00
  • 1
    Does this answer your question? [how-would-i-access-variables-from-one-class-to-another](https://stackoverflow.com/questions/19993795) – stovfl Apr 18 '20 at 21:24
  • @GrajdeanuAlex. The function send_message(self, data) , called excel path from GUI modul. Look in this article https://www.raspberrypi.org/forums/viewtopic.php?t=195973 , but I'm using excel brows to read Excel. – John Gomail Apr 19 '20 at 11:25
  • @tdelaney Not working – John Gomail Apr 19 '20 at 11:31
  • @stovfl this is another way Not working – John Gomail Apr 19 '20 at 11:31

2 Answers2

1

Delete the globals.

Have the send function return the path value.

Just before First class, assign a value to a newly defined global variable:

path = Excell().send_message("foo")

Then the value is available when you assign to workbook.

J_H
  • 17,926
  • 4
  • 24
  • 44
0

I use this but Not work I see this message I import function `def send_message(self, data):

from GUI module like this example https://www.raspberrypi.org/forums/viewtopic.php?t=195973 , but change change entry to label get path from brows file:

import openpyxl

class Excell():

     def send_message(self, data):

           print("Path Excel is : '{}'".format(data))
           return data

path = Excell().send_message("foo")

class First():

       wb = openpyxl.load_workbook(path)
       sheet = wb['sheet']
       firstCell = sheet["A1"].value
       print("Cell is :" + firstCell)

I see this error:

C:\Python\python.exe E:/PycharmProjects/Amazon/bb.py
Path Excel is : 'data'
Traceback (most recent call last):
      File "E:/PycharmProjects/Amazon/bb.py", line 12, in <module>
           class First():
      File "E:/PycharmProjects/Amazon/bb.py", line 14, in First
           wb = openpyxl.load_workbook(path)
      File "C:\Python\lib\site-packages\openpyxl\reader\excel.py", line 312, in                load_workbook
            reader = ExcelReader(filename, read_only, keep_vba,
      File "C:\Python\lib\site-packages\openpyxl\reader\excel.py", line 124, in __init__
            self.archive = _validate_archive(fn)
      File "C:\Python\lib\site-packages\openpyxl\reader\excel.py", line 94, in _validate_archive
            raise InvalidFileException(msg)
openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support  file format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,.xltx,.xltm

Process finished with exit code 1

  • Instead of `send_message(“data”)` you should actually provide the path to your excel file. Something like: `send_message(“C:\path\excel_file.xlsx”)` – Grajdeanu Alex Apr 19 '20 at 14:54
  • this path is dynamic. I get the path automatically after upload from GUI tkinter. – John Gomail Apr 19 '20 at 17:28