# String to Spec Type

**URL:** <https://forum.dynamobim.com/t/string-to-spec-type/95456>\
**Category:** Revit\
**Tags:** dynamo\
**Created:** [November 13, 2023, 2:52pm UTC](https://forum.dynamobim.com/t/string-to-spec-type/95456 "2023-11-13T14:52:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![amatrigianiGGLTT](https://avatars.discourse-cdn.com/v4/letter/a/b5e925/32.png) [@amatrigianiGGLTT](https://forum.dynamobim.com/u/amatrigianiGGLTT)\
**Post date:** [November 13, 2023, 2:52pm UTC](https://forum.dynamobim.com/t/string-to-spec-type/95456/1 "2023-11-13T14:52:15Z")

</div>

Buongiorno a tutti,

nel creare dei parametri condivisi da un file excel non riesco a convertire una lista di stringhe in Spec Type.

Avete qualche suggerimento in merito?  
Grazie

* * *

**EDIT : Translated by Moderation (English forum)**

Good morning, everyone,

in creating shared parameters from an excel file I cannot convert a list of strings to Spec Type.

Do you have any suggestions on this?  
Thanks

 ![Cattura](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/f/3/f363289065919f0397021a1b3a1d5b4342ee12cf.png)

---

<div class="post-metadata">

**Author:** ![pyXam](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/pyxam/32/163009_2.png) [@pyXam](https://forum.dynamobim.com/u/pyXam)\
**Post date:** [November 13, 2023, 8:12pm UTC](https://forum.dynamobim.com/t/string-to-spec-type/95456/2 "2023-11-13T20:12:49Z")

</div>

This code was made for GroupTypes but there wouldn’t be much to change for Spectype, Give it a go 🙂

> [@Get GroupType by name? Is this possible?](https://forum.dynamobim.com/t/get-grouptype-by-name-is-this-possible/93811/5):
>
> Good Morning (NZ Time) @wesleyJHXGK Hopefully this works for you. Given a list of strings, the code will check to see if the string matches any of the parameter grouptypes in the project and if there is, it will output it as a string for you to input into your list as required. The handling of a not matching item is not strictly correct, but i’m assuming that your input list is perfect wink I hope this answers your question, Have a great day! 
> 
> > **Python Script**
> >
> > #Made by pyXam #https://…

Hint for you\*

```auto
# Collect the parameter groups and convert them to a string list
parameter_groups_enum = System.Enum.GetValues(BuiltInParameterGroup)
parameter_groups = [System.Enum.GetName(BuiltInParameterGroup, group) for group in parameter_groups_enum]

```

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [November 13, 2023, 8:50pm UTC](https://forum.dynamobim.com/t/string-to-spec-type/95456/3 "2023-11-13T20:50:29Z")

</div>

Hi,

here is a solution with Python

the input is a flat list of the Name of Parameter Type (Spec Type) :

- in English  
**or**
- in Revit Current language

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/5/d/5d5374c8e1ac40598c59eeec793257f513e87c2f.png)

Python code (compatible to all engines)

```auto
import clr
import sys
import System
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

def toList(x):
    if isinstance(x, list):
        return x
    elif hasattr(x, "GetType") and x.GetType().GetInterface("IEnumerable") is not None:
        return x
    else :
        return [x]

lst_specType_Name = toList(IN[0])
invalid_forgeTypeId = ForgeTypeId()

dict_forgeTypeId = {}
lst_specTypeId_class = [clr.GetClrType(SpecTypeId), 
                        clr.GetClrType(SpecTypeId.Boolean), 
                        clr.GetClrType(SpecTypeId.Int), 
                        clr.GetClrType(SpecTypeId.String)]
#
for specTypeId_class in lst_specTypeId_class :
    prop_infos = specTypeId_class.GetProperties()
    # get the Name of SpecType and put it in a dictionary {SpecType Name : SpecType }
    dict_tempA_forgeTypeId = {p.Name : p.GetGetMethod().Invoke(None, None) for p in prop_infos}
    # get the LabelForSpec and put it in a dictionary {label : SpecType }
    dict_tempB_forgeTypeId = {}
    for key_, value_ in dict_tempA_forgeTypeId.items() :
        try:
            dict_tempB_forgeTypeId[LabelUtils.GetLabelForSpec(value_)] = value_ 
        except Exception as ex:
            print(ex)
    # merge dicts in main dict
    dict_forgeTypeId = dict(dict_forgeTypeId, **dict_tempA_forgeTypeId)
    dict_forgeTypeId = dict(dict_forgeTypeId, **dict_tempB_forgeTypeId)
    
# get DB.SpecTypeId by input Name
lst_db_specTypeId = [dict_forgeTypeId.get(txt_value, invalid_forgeTypeId) for txt_value in lst_specType_Name]
# convert to Dynamo Revit.Elements.SpecType
OUT = [Revit.Elements.SpecType.ByTypeId(v.TypeId) for v in lst_db_specTypeId]

```
