How to export the table pane contents to an excel using mechanical scripting?

Vishnu Member, Employee Posts: 198
Name Dropper First Anniversary Solution Developer Community of Practice Member First Comment
edited June 2023 in Structures

You could do the same for multiple result objects,forceobjects,ACT objects: Please see below script to add multiple items in one go.

Export to excel

Basically any object which has contents in tabular pane can be exported in similar way



  • Vishnu
    Vishnu Member, Employee Posts: 198
    Name Dropper First Anniversary Solution Developer Community of Practice Member First Comment
    Answer ✓

    Make Sure you edit result_names argument in last line before you execute the below script as per your requirement

    def extractresultdata_to_excel(result_names):
        extracts the result table to an excel and store them in user files dir
        use like: 
        extractresultdata_to_excel(['Total Deformation', 'Equivalent Stress'])
        #Get the alphabet number corresponding to excel cell
        def Cells(a, b):
            return str(chr(a+96) + str(b))
        #Import statements
        import clr
        import Microsoft.Office.Interop.Excel as Excel
        import os
        from System import Array
        # Loop every object and extract table pane contents
        for result_name in result_names:
            resultobject = ExtAPI.DataModel.GetObjectsByName(result_name)[0]
            obj = resultobject
            while obj.GetType()!= Ansys.ACT.Automation.Mechanical.Analysis:obj = obj.Parent
            analysis = obj
            user_files_dir = os.path.join(
                analysis.WorkingDir, '../../../user_files')
            file_name = os.path.join(user_files_dir, result_name+'_result'+'.xlsx')
            paneTabular = ExtAPI.UserInterface.GetPane(
            control = paneTabular.ControlUnknown
            num_columns = control.ColumnsCount+1
            num_rows = control.RowsCount+1
            result = []
            for row in range(1, num_rows):
                r = []
                for col in range(1, num_columns):
                    r.append(control.cell(row, col).Text)
            #Write the table pane contents to excel
            ex = Excel.ApplicationClass()
            ex.Visible = False
            ex.DisplayAlerts = False
            workbook = ex.Workbooks.Add()
            worksheet = workbook.Worksheets[1]
            worksheet.Name = "Results"
            array = Array.CreateInstance(object, len(result), len(result[0]))
            for rowcount, row in enumerate(result):
                for columncount, column in enumerate(row):
                    array[rowcount, columncount] = column
            worksheet.Range[Cells(1, 1), Cells(
                len(result[0]), len(result))].Value2 = array
            worksheet.Columns("A:AA").HorizontalAlignment = -4108
            worksheet.Columns("A:AA").VerticalAlignment = -4108
            worksheet.Columns("A:AA").Font.Bold = True
            ex.DisplayAlerts = False
            #save excel in user files dir
            ex.DisplayAlerts = True
            ex.Visible = True  # Comment if do not want to see excel popping up
    #Use the above function. Please edit result names argument below as per your case
        ['Total Deformation', 'Equivalent Stress','Force'])
    #note: you could as well add force or displacement objects to export too

    - Also Sharing a button .

    After importing button, All you need to do is select objects you need to to export table contents in mechanical to an excel and click the button. (Make sure the objects selected has table pane contents available)