# Opening Revit File in Background with Python

**URL:** <https://forum.dynamobim.com/t/opening-revit-file-in-background-with-python/63454>\
**Category:** Revit\
**Created:** [April 29, 2021, 8:25pm UTC](https://forum.dynamobim.com/t/opening-revit-file-in-background-with-python/63454 "2021-04-29T20:25:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruben.romero](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ruben.romero/32/115842_2.png) [@ruben.romero](https://forum.dynamobim.com/u/ruben.romero)\
**Post date:** [April 29, 2021, 8:25pm UTC](https://forum.dynamobim.com/t/opening-revit-file-in-background-with-python/63454/1 "2021-04-29T20:25:23Z")

</div>

I make this work but it handles one single item as input, I need a list.

```
import clr

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

doc = DocumentManager.Instance.CurrentDBDocument
app = doc.Application

filePath = IN[0]
modelPath = ModelPathUtils.ConvertUserVisiblePathToModelPath(filePath)

options = OpenOptions()
options.DetachFromCentralOption = DetachFromCentralOption.DetachAndPreserveWorksets

docOnDisk = app.OpenDocumentFile(modelPath, options)

OUT = docOnDisk
```

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [April 29, 2021, 11:08pm UTC](https://forum.dynamobim.com/t/opening-revit-file-in-background-with-python/63454/2 "2021-04-29T23:08:57Z")

</div>

You need to provide a looping mechanism for each action which requires a list. In this context utilizing List Comprehension may be easiest to implement.

The structure is basically this:  
‘`variable` = `[` `thingToDo` `for``item` `in` `list` `]`

You can find lots of good tutorials on this online.

Your first action which needs to loop might look like this (on my phone and my python app wont start so pardon any inaccuracies):

`modelPaths = [ModelPathUtils.ConvertUserVisiblePathToModelPath(path) for path in filePaths]`

---

<div class="post-metadata">

**Author:** ![J\_Sanford](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@J\_Sanford](https://forum.dynamobim.com/u/J_Sanford)\
**Post date:** [August 24, 2023, 3:53pm UTC](https://forum.dynamobim.com/t/opening-revit-file-in-background-with-python/63454/3 "2023-08-24T15:53:26Z")

</div>

> [@ruben.romero](#):
>
> ```auto
> doc = DocumentManager.Instance.CurrentDBDocument
> app = doc.Application
> 
> ```

I am working on this now too and found this post- so this will not iterate and I dont know why?

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/a/e/ae41f3dc49bd468f46913b22c224396e3022b767.png)

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [August 25, 2023, 7:31am UTC](https://forum.dynamobim.com/t/opening-revit-file-in-background-with-python/63454/4 "2023-08-25T07:31:12Z")

</div>

Same query solved here:

> [@Learning how to handle background documents in Python](https://forum.dynamobim.com/t/learning-how-to-handle-background-documents-in-python/93172):
>
> Hello! I am learning more Python and am having trouble working with docs- I cant iterate over a list of docs to open them all. I can open and close one doc though. Any idea why that is? import clr clr.AddReference('RevitServices') import RevitServices from RevitServices.Persistence import DocumentManager clr.AddReference('RevitAPI') import Autodesk from Autodesk.Revit.DB import \* def tolist(input): result = input if isinstance(input, list) else [input] return result fileP…

---

<div class="post-metadata">

**Author:** ![J\_Sanford](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@J\_Sanford](https://forum.dynamobim.com/u/J_Sanford)\
**Post date:** [August 25, 2023, 12:03pm UTC](https://forum.dynamobim.com/t/opening-revit-file-in-background-with-python/63454/5 "2023-08-25T12:03:22Z")

</div>

yeah I just made a post out of it 🙂 working with documents is such a hassle lol. this is really just me learning one step of this as I need a way to get elements from an external document to copy.
