Basic - conversion of a python script to ironpython

Hi i am new with ironpython, and i am having problems with converting some python libraries into ironpython while coding in dynamo in revit.
Someone please tell me the basics of converting a python script to ironpython?!

could you help me with this script as an example?

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import time

service = Service(r’C:\\Users\\shanm\\chromedriver.exe’)  # Using raw string for Windows path
driver = webdriver.Chrome(service=service)

url = “https://www.environdec.com/library/epd8104”
driver.get(url)

time.sleep(3)

try:

  pdf_element = driver.find_element(By.LINK_TEXT, “EPD document S-P-08104 en.pdf”)
  pdf_url = pdf_element.get_attribute(“href”)
  print(“Extracted PDF URL:”, pdf_url)
  
  import requests 
  pdf_response = requests.get(pdf_url) 
  if pdf_response.status_code == 200:
   with open(“EPD_document.pdf”, “wb”) as f:
    f.write(pdf_response.content)
    print(“PDF downloaded successfully!”) 
  else:
    print(“Failed to download PDF. Status code:”, pdf_response.status_code)

except Exception as e:
  print(“Error occurred:”, e)

finally:

  driver.quit()

Hello @uaftab.cem19 - Curious if you need to use IronPython? It’s a language we’ve not supported now for numerous years and will stop working at some point in the not-to-distant future.

Depending on what version of Revit you are in, we would suggest switching to CPython :slight_smile: If you are in Revit 2025+, then you can use the “PythonNET3” engine which is on the package manager now, but will be the out of the box version soon.

There seems to be a well-structured public API at https://www.environdec.com/.

For example, if you look at this public link, you will find all the data, including the PDF ID.

https://api.environdec.com/api/v1/EPDLibrary/EPD/epd8104

So you don’t need Selenium, just requests.

import sys
import clr
import System

import json
import urllib.request
import urllib.error
from urllib.request import Request, urlopen
from urllib.error import URLError, HTTPError

my_path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments)


def data_request(url):
    try:
        req = Request(url, headers={"User-Agent": "python-urllib/3"})
        with urlopen(req, timeout=30) as resp:
            content_type = resp.headers.get("Content-Type", "")
            raw = resp.read()
            # if JSON, decode and load; else return raw bytes
            if "application/json" in content_type or "text/json" in content_type:
                # pick charset if provided, default to utf-8
                encoding = resp.headers.get_content_charset() or "utf-8"
                text = raw.decode(encoding, errors="replace")
                return json.loads(text)
            else:
                return raw
    except HTTPError as e:
        print("HTTP error:", e.code, e.reason)
    except URLError as e:
        print("Connection error:", e.reason)
    return None
    
OUT = []
url = "https://www.environdec.com/library/epd8104"
ref_product = url.split("/")[-1]
url_api = f"https://api.environdec.com/api/v1/EPDLibrary/EPD/{ref_product}"

response_data = data_request(url_api)
if response_data is not None:
    for data_doc in response_data["documents"]:
        if data_doc["name"].endswith("pdf"):
            id_pdf = data_doc["id"]
            name_pdf = data_doc["name"]
            url_pdf = f"https://api.environdec.com/api/v1/EPDLibrary/Files/{id_pdf}/Data"
            pdf_byte = data_request(url_pdf)
            print(type(pdf_byte))
            with open(f"{my_path}\\{name_pdf}", "wb") as f:
                f.write(pdf_byte)
                OUT.append(f"{my_path}\\{name_pdf}")

Notes

  • I have only tested 2-3 technical data sheets.
  • Check the site’s terms and conditions to see if downloading is permitted in this way (if not, you will need to use their REST API).