tldr: openpyxl dont work, pywin32 works. My question is why?
................................................................
Openpyxl way =>
import os
from openpyxl import load_workbook
wb = load_workbook("./exported_from_vt.xlsx")
ws_context = wb["Context"]
ws_context.sheet_state='visible'
r = ws_context.max_row+1
ws_context.cell(row=r,column=2, value="Asia22")
ws_context.cell(row=r,column=10, value="Asia22")
r+=1
ws_context.cell(row=r,column=2, value="Asia22\\\\Himalayas")
ws_context.cell(row=r,column=10, value="Himalayas")
r+=1
ws_context.cell(row=r,column=2, value="Asia22\\\\Ocean")
ws_context.cell(row=r,column=10, value="Ocean")
# wb.save("exported_from_vt.xlsx")
wb.save("to_sync.xlsx")
- I first go to vtscada application configuration to generate the current snapshot of tags (exported_from_vt.xlsx)
- this python code inserts rows in the excel sheet to add 3 vt tags and saves the file (to_sync.xlsx)
- Then i take the to_sync.xlsx file and Sync to vtscada
- But VT do not take any tags, says: 0 deleted, 0 added
But when instead of openpyxl , I use pywin32 package and its methods, VT Sync works fine.
import win32com.client as win32
import os
input_file = os.path.abspath("./exported_from_vt.xlsx")
output_file = os.path.abspath("./to_sync.xlsx")
excel = win32.gencache.EnsureDispatch("Excel.Application")
excel.Visible = False
wb = excel.Workbooks.Open(input_file)
ws_context = wb.Worksheets("Context")
ws_context.Visible = -1
xlUp = -4162
last_row = ws_context.Cells(ws_context.Rows.Count, 1).End(xlUp).Row
r = last_row + 1
ws_context.Cells(r, 2).Value = "Asia22"
ws_context.Cells(r, 10).Value = "Asia22"
r += 1
ws_context.Cells(r, 2).Value = "Asia22\\\\Himalayas"
ws_context.Cells(r, 10).Value = "Himalayas"
r += 1
ws_context.Cells(r, 2).Value = "Asia22\\\\Ocean"
ws_context.Cells(r, 10).Value = "Ocean"
wb.SaveAs(output_file)
wb.Close(SaveChanges=0)
excel.Quit()
(I'm posting this question for future reference actually, letting other people know about openpyxl vs pywin32 behaviour)
I asked chatgpt...
tldr: openpyxl dont work, pywin32 works. My question is why?
................................................................
Openpyxl way =>
```
import os
from openpyxl import load_workbook
wb = load_workbook("./exported_from_vt.xlsx")
ws_context = wb["Context"]
ws_context.sheet_state='visible'
r = ws_context.max_row+1
ws_context.cell(row=r,column=2, value="Asia22")
ws_context.cell(row=r,column=10, value="Asia22")
r+=1
ws_context.cell(row=r,column=2, value="Asia22\\\\Himalayas")
ws_context.cell(row=r,column=10, value="Himalayas")
r+=1
ws_context.cell(row=r,column=2, value="Asia22\\\\Ocean")
ws_context.cell(row=r,column=10, value="Ocean")
# wb.save("exported_from_vt.xlsx")
wb.save("to_sync.xlsx")
```
1. I first go to vtscada application configuration to generate the current snapshot of tags (exported_from_vt.xlsx)
2. this python code inserts rows in the excel sheet to add 3 vt tags and saves the file (to_sync.xlsx)
3. Then i take the _to_sync.xlsx_ file and **Sync** to vtscada
4. But VT do not take any tags, says: _0 deleted, 0 added_
But when instead of [openpyxl](https://pypi.org/project/openpyxl/) , I use [pywin32](https://pypi.org/project/pywin32/) package and its methods, VT Sync works fine.
```
import win32com.client as win32
import os
input_file = os.path.abspath("./exported_from_vt.xlsx")
output_file = os.path.abspath("./to_sync.xlsx")
excel = win32.gencache.EnsureDispatch("Excel.Application")
excel.Visible = False
wb = excel.Workbooks.Open(input_file)
ws_context = wb.Worksheets("Context")
ws_context.Visible = -1
xlUp = -4162
last_row = ws_context.Cells(ws_context.Rows.Count, 1).End(xlUp).Row
r = last_row + 1
ws_context.Cells(r, 2).Value = "Asia22"
ws_context.Cells(r, 10).Value = "Asia22"
r += 1
ws_context.Cells(r, 2).Value = "Asia22\\\\Himalayas"
ws_context.Cells(r, 10).Value = "Himalayas"
r += 1
ws_context.Cells(r, 2).Value = "Asia22\\\\Ocean"
ws_context.Cells(r, 10).Value = "Ocean"
wb.SaveAs(output_file)
wb.Close(SaveChanges=0)
excel.Quit()
```
(I'm posting this question for future reference actually, letting other people know about openpyxl vs pywin32 behaviour)
I asked chatgpt...

edited May 31 '25 at 6:25 am