Reading/writing from Excel

Hi,

I’m trying to write descriptions to model elements by reading from an excel spreadsheet. I’m able to read from the example excel (physical_components.xlsx) that’s found within the Resources folder of the Python4Capella project. When I try to change the example path to the excel I wish to read, I produce an attribute error where:

File “L/Python4Capella/utilities/CapellaPlatform.py”, line 50, in getAbsolutePath
except ImportError:
AttributeError: ‘NoneType’ object has no attribute ‘getLocation’

This leads me to believe that it’s not able to read the xlsx file for some reason. However, in the example excel, I am able to edit that file and read the excel file from that example excel only. Is there something that I’m missing in the file path? Example of my code below (<----- indicates the erroneous lines):


if len(argv) != 2:
    aird_path = '/test/test.aird'
    xlsx_path = "/Python4Capella/resources/physical_components.xlsx" **<---- This line works**
    #xlsx_path = "/Python4Capella/resources/test_components.xlsx" **<----- This line does not work (direct copy of the excel file just with name changed)**
else:
    aird_path = argv[0]
    xlsx_path = argv[1]

model = CapellaModel()
model.open(aird_path)

se = model.get_system_engineering()

xlsx_file = CapellaPlatform.getWorkspaceFile(xlsx_path)
xlsx_file_name = CapellaPlatform.getAbsolutePath(xlsx_file)

print("Read " + xlsx_file_name)

wb = load_workbook(xlsx_file_name)

ws = wb.active

row = ws.max_row
column = ws.max_column

for i in range(1, row + 1):
    a = ws.cell(row = i, column = 1)
    print(a.value)
    for j in se.get_physical_architecture().get_all_contents_by_type(PhysicalComponentPkg):
        if j.get_name() == "test Elements":
            for x in j.get_all_contents_by_type(PhysicalComponent):
                if (x.get_sid):
                    if x.get_id() == a.value:
                        b = ws.cell(row = i, column = 2)
                        try:
                            x.set_sid(b.value)
                        except:
                            pass

Note: I have no issues running this script and setting the sid of each component with the supplied sample file, I just can’t change the file path without receiving the error.

I get the same error if the file doesn’t exists. I used the following script:

include('workspace://Python4Capella/simplified_api/capella.py')
if False:
    from simplified_api.capella import *

# include needed for utilities
include('workspace://Python4Capella/utilities/CapellaPlatform.py')
if False:
    from utilities.CapellaPlatform import *


xlsx_path = "/Python4Capella/resources/test_components.xlsx"

xlsx_file = CapellaPlatform.getWorkspaceFile(xlsx_path)
if xlsx_file is None:
    print("file" + xlsx_path + " doesn't exists.")
else:
    xlsx_file_name = CapellaPlatform.getAbsolutePath(xlsx_file)
    print("absolute path: " + xlsx_file_name)

The file /Python4Capella/resources/test_components.xlsx exists:

absolute path: /home/development/git/python4capella/plugins/Python4Capella/resources/test_components.xlsx

The file /Python4Capella/resources/test_component.xlsx doesn’t:

file/Python4Capella/resources/test_component.xlsx doesn't exists.

Your workspace need to be sychrnoized with the file system if the file is created outside of Capella, you can refresh a project or a folder with Python4Capella by using:

CapellaPlatform.refresh("/project/folder")

You can also use the refresh menu by right clicking on your project or folder.