# Close a workbook with openpyxl

**URL:** <https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720>\
**Category:** Ignition\
**Tags:** perspective, ignition81\
**Created:** [February 3, 2022, 4:32pm UTC](https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720 "2022-02-03T16:32:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![g.cascales](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@g.cascales](https://forum.inductiveautomation.com/u/g.cascales)\
**Post date:** [February 3, 2022, 4:32pm UTC](https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720/1 "2022-02-03T16:32:32Z")

</div>

Hello there!

as @zxcslo indicated in [Copying a Excel file to a tables Dataset - #38 by zxcslo](https://forum.inductiveautomation.com/t/copying-a-excel-file-to-a-tables-dataset/34942/38) the openpyxl worked also for me.

But I am having issues when it comes to close or save the file. The code I am using is:

`
from openpyxl import Workbook
from openpyxl import load_workbook
import os
upload_path = “” #whatever
sheetName = “” #whatever
wb = load_workbook(upload_path)
ws = wb[sheetName]
cellValue = ws[“C6”]
wb.close() #necessary? alternatives?
self.getSibling(“Label”).props.text = cellValue.value
`

I got the cell value correctly, but then if I navigate to the folder and try to delete the file, I can´t.  
I get the message from Windows: “action could not be completed because java.exe has the file opened”

Anyone has a clue? I do not know where the error comes from.

Thanks in advance,

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 3, 2022, 5:17pm UTC](https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720/2 "2022-02-03T17:17:57Z")

</div>

The function you want to call seems to be `wb.save($filepath)`:  
[https://openpyxl.readthedocs.io/en/stable/index.html](https://openpyxl.readthedocs.io/en/stable/index.html)

But I don’t know if the API is exactly the same on whatever version you have.

---

<div class="post-metadata">

**Author:** ![g.cascales](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@g.cascales](https://forum.inductiveautomation.com/u/g.cascales)\
**Post date:** [February 3, 2022, 5:48pm UTC](https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720/3 "2022-02-03T17:48:09Z")

</div>

Dear @paul-griffith ,

thanks for the quick response. I will try soon, but what “$” stands for?

Thanks again,

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [February 3, 2022, 5:49pm UTC](https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720/4 "2022-02-03T17:49:05Z")

</div>

Sorry, that’s just meant as “placeholder” syntax. Use whatever filepath you actually want to save to as a string.

---

<div class="post-metadata">

**Author:** ![g.cascales](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@g.cascales](https://forum.inductiveautomation.com/u/g.cascales)\
**Post date:** [February 4, 2022, 6:36am UTC](https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720/5 "2022-02-04T06:36:40Z")

</div>

Hello,

I just tried with this code, and I got the cell value correctly showed in the label. However, **I can´t still delete the file**.

`
from openpyxl import Workbook
from openpyxl import load_workbook
import os
upload_path = “” #whatever
sheetName = “” #whatever
wb = load_workbook(upload_path)
ws = wb[sheetName]
cellValue = ws[“C6”]
wb.save(upload_path) #using both wb.save(upload_path) and wb.close() it does not work
self.getSibling(“Label”).props.text = cellValue.value
`

The “upload\_path” variable is using the format: C:\Users\GENIOTIC\Desktop\PruebasIgnition\Filename.xlsx

Two more notes:

- I also wanted to try with the pylight library, but I am stuck because I do not know exactly what files/folders I need to copy to the Ignition directory.

- I am already using the function created by @JordanCClark [Copying a Excel file to a tables Dataset - #13 by JordanCClark](https://forum.inductiveautomation.com/t/copying-a-excel-file-to-a-tables-dataset/34942/13). But I am not able to find out how to **get the value of a specific cell indicating the address** (i.e. “C6”) instead of row and column numbers. I have been reading some of the Apache POI documentation and I think I need to create a cell reference but I still do not know how.

Thanks !!

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [February 4, 2022, 1:07pm UTC](https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720/6 "2022-02-04T13:07:31Z")

</div>

> [@g.cascales](#):
>
> But I am not able to find out how to **get the value of a specific cell indicating the address** (i.e. “C6”)

Add this to your imports

```python
import org.apache.poi.ss.util.CellReference as CellReference

```

Then you should be able to use something like:

```python
ref = CellReference('C6')
refRow = sheet.getRow(ref.getRow())
cellValue = refRow.getCell(ref.getCol())

```

---

<div class="post-metadata">

**Author:** ![g.cascales](https://avatars.discourse-cdn.com/v4/letter/g/71c47a/32.png) [@g.cascales](https://forum.inductiveautomation.com/u/g.cascales)\
**Post date:** [February 4, 2022, 1:20pm UTC](https://forum.inductiveautomation.com/t/close-a-workbook-with-openpyxl/55720/7 "2022-02-04T13:20:21Z")

</div>

Yep! That works, I was missing the correct syntax for the import sentence.

Thanks again @JordanCClark
