Skip to content

Commit 016bb6a

Browse files
fix: allow win32com operations on files already open in Excel
win32com's Dispatch("Excel.Application") connects to the existing instance, so operations like pivot create/refresh, chart --pivot, and table --native can work with files already open in Excel. Remove unnecessary file lock checks for these commands. Co-Authored-By: martyy-code <nesalia.inc@gmail.com>
1 parent 8431bb4 commit 016bb6a

3 files changed

Lines changed: 8 additions & 21 deletions

File tree

xlforge/commands/chart.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -264,11 +264,8 @@ def _create_pivot_chart(
264264

265265
xl_chart_type = xl_chart_types[chart_type_lower]
266266

267-
# Check if file is open in Excel
268-
is_blocked, error_msg = _check_file_not_open_in_excel(path)
269-
if is_blocked:
270-
typer.secho(f"Error: {error_msg}", fg=typer.colors.RED, err=True)
271-
raise typer.Exit(code=int(ErrorCode.FILE_IN_USE))
267+
# Note: We don't check for file open in Excel here because win32com's
268+
# Dispatch("Excel.Application") can connect to an already-open instance
272269

273270
excel = None
274271
try:

xlforge/commands/pivot.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,8 @@ def create(
176176
)
177177
raise typer.Exit(code=int(ErrorCode.FILE_DOES_NOT_EXIST))
178178

179-
# Check if file is open in Excel
180-
is_blocked, error_msg = _check_file_not_open_in_excel(path)
181-
if is_blocked:
182-
typer.secho(f"Error: {error_msg}", fg=typer.colors.RED, err=True)
183-
raise typer.Exit(code=int(ErrorCode.FILE_IN_USE))
179+
# Note: We don't check for file open in Excel here because win32com's
180+
# Dispatch("Excel.Application") can connect to an already-open instance
184181

185182
# Determine target sheet
186183
target_sheet = sheet if sheet else f"{source_sheet}_Pivot"
@@ -553,11 +550,8 @@ def refresh(
553550
)
554551
raise typer.Exit(code=int(ErrorCode.FILE_DOES_NOT_EXIST))
555552

556-
# Check if file is open in Excel
557-
is_blocked, error_msg = _check_file_not_open_in_excel(path)
558-
if is_blocked:
559-
typer.secho(f"Error: {error_msg}", fg=typer.colors.RED, err=True)
560-
raise typer.Exit(code=int(ErrorCode.FILE_IN_USE))
553+
# Note: We don't check for file open in Excel here because win32com's
554+
# Dispatch("Excel.Application") can connect to an already-open instance
561555

562556
excel = None
563557
try:

xlforge/commands/table.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from openpyxl.worksheet.table import Table
1616

1717
from xlforge.core.errors import ErrorCode
18-
from xlforge.core.utils import _check_file_not_open_in_excel
1918

2019
table_app = typer.Typer(help="Table operations for Excel workbooks.")
2120

@@ -121,11 +120,8 @@ def _create_native_table(
121120
)
122121
raise typer.Exit(code=int(ErrorCode.FEATURE_UNAVAILABLE))
123122

124-
# Check if file is open in Excel
125-
is_blocked, error_msg = _check_file_not_open_in_excel(path)
126-
if is_blocked:
127-
typer.secho(f"Error: {error_msg}", fg=typer.colors.RED, err=True)
128-
raise typer.Exit(code=int(ErrorCode.FILE_IN_USE))
123+
# Note: We don't check for file open in Excel here because win32com's
124+
# Dispatch("Excel.Application") can connect to an already-open instance
129125

130126
excel = None
131127
try:

0 commit comments

Comments
 (0)