Python to ironpython

hi. I wrote my code in pycharm first and I was not able to link the output with dynamo for visualization of output in Revit. then I started in dynamo. i parse my excel data but I am not able to write the pycharm loop code in my dynamo python script.
offset coordinates.dyn (67.1 KB)
roof edges+S.txt (1.5 KB) (pycharm code that I want to run in dynamo )
s data.xlsx (7.9 KB) the excel sheetscoordinates.xlsx (9.2 KB)

#importing openpyxl module
import openpyxl
# workbook object is created
workbook1 = openpyxl.load_workbook("edges.coordinates.xlsx")
workbook2 = openpyxl.load_workbook("s data.xlsx")
#For sheet1
sheet_obj1 = workbook1["sheet1"]
#For sheet2
sheet_obj2 = workbook1["sheet2"]
#for s workbook
sheet_obj3 = workbook2["sheet1"]

# Will print a particular row value
for j in range(0, sheet_obj3.max_row):
    #Fetching Data From Cell For Sheet1/workbook 2
    x= sheet_obj3.cell(row = 1+j, column = 1).value
    y = sheet_obj3.cell(row = 1+j, column = 2).value
    z = sheet_obj3.cell(row = 1+j, column = 3).value

    # Will print a particular row value
    for i in range(1, sheet_obj1.max_row):
        #Fetching Data From Cell For Sheet1
        x1 = sheet_obj1.cell(row = i, column = 1)
        y1 = sheet_obj1.cell(row = i, column = 2)
        x2 = sheet_obj1.cell(row = i+1, column = 1)
        y2 = sheet_obj1.cell(row = i+1, column = 2)
        z1 = sheet_obj1.cell(row = i, column = 3)

        #Fetching Data From Cell For Sheet2
        x11 = sheet_obj2.cell(row = i, column = 1)
        y11 = sheet_obj2.cell(row = i, column = 2)
        x22 = sheet_obj2.cell(row = i+1, column = 1)
        y22 = sheet_obj2.cell(row = i+1, column = 2)

        #Printing Warning If Condition Is Satisfied
        if z == z1.value:
             if min(x1.value,x2.value,x11.value,x22.value)<=x <=max(x1.value,x2.value,x11.value,x22.value) and  min(y1.value,y2.value,y11.value,y22.value)<=y<=max(y1.value,y2.value,y11.value,y22.value):
                 print("Warning Between Row"+str(i)+" and Row"+str((i+1)))

thanks in advance.

Hi can you attach the error snap, so that others can try to suggest answers by inference first, instead of reproducing the error by themselves.

Hello @fil

an example using Microsoft.Office.Interop (no modules necessary)

# coding: utf-8 
import clr
import sys
import System
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

from System import Array
from System.Collections.Generic import *

clr.AddReferenceByName('Microsoft.Office.Interop.Excel, Version=11.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' )
from Microsoft.Office.Interop import Excel
from System.Runtime.InteropServices import Marshal

xlDirecDown = System.Enum.Parse(Excel.XlDirection, "xlDown")
xlDirecRight = System.Enum.Parse(Excel.XlDirection, "xlToRight")

out = []

ex = Excel.ApplicationClass()
ex.Visible = True
lst_xls = []
workbook1  = ex.Workbooks.Open(IN[0])
workbook2  = ex.Workbooks.Open(IN[1])
# For sheet1
sheet_obj1 = workbook1.Worksheets["sheet1"]
#For sheet2
sheet_obj2 = workbook1.Worksheets["sheet2"]
# for s workbook
sheet_obj3 = workbook2.Worksheets["sheet1"]
# number of Rows  and Columns
obj1_rowCountF = sheet_obj1.Columns[1].End(xlDirecDown).Row
obj1_colCountF = sheet_obj1.Rows[1].End(xlDirecRight).Column

obj3_rowCountF = sheet_obj3.Columns[1].End(xlDirecDown).Row
obj3_colCountF = sheet_obj3.Rows[1].End(xlDirecRight).Column

for j in range(obj3_rowCountF):
	x = sheet_obj3.Cells(j + 1, 1).Value2
	y = sheet_obj3.Cells(j + 1, 2).Value2
	z = sheet_obj3.Cells(j + 1, 3).Value2
	for i in range(1, obj1_rowCountF):
		#Fetching Data From Cell For Sheet1
		x1 = sheet_obj1.Cells(i,  2).Value2
		y1 = sheet_obj1.Cells(i, 3).Value2
		x2 = sheet_obj1.Cells(i + 1, 2).Value2
		y2 = sheet_obj1.Cells(i + 1, 3).Value2
		z1 = sheet_obj1.Cells(i, 4).Value2
		#Fetching Data From Cell For Sheet2
		x11 = sheet_obj2.Cells(i, 2).Value2
		y11 = sheet_obj2.Cells(i, 3).Value2
		x22 = sheet_obj2.Cells(i + 1, 2).Value2
		y22 = sheet_obj2.Cells(i + 1, 3).Value2
		#Printing Warning If Condition Is Satisfied
		if z == z1:
			if min(x1, x2, x11, x22) <= x <= max(x1, x2, x11, x22):
				if min(y1, y2, y11, y22) <= y <= max(y1, y2, y11, y22):
					out.append("Warning Between Row"+str(i)+" and Row"+str((i+1)))

OUT = out
1 Like

this is in dynamo?

it’s “in” IronPython
IronPython exposes .NET concepts as Python constructs and supports accessing COM objects
https://ironpython.net/documentation/dotnet/dotnet.html#oleautomation-and-com-interop

its not giving any out? it should give out put warning between rows 2 and 3

remove “File from Path” Nodes

still empty

Workbooks.Open() take a file path string as parameter

still empty

1 Like

thankyou

1 Like