Skip to content

Commit

Permalink
Added xlwings py edit CLI command (#2331)
Browse files Browse the repository at this point in the history
* Added xlwings py edit CLI command

* docs
  • Loading branch information
fzumstein committed Sep 18, 2023
1 parent 08818da commit f2e3113
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/command_line.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,10 @@ xlwings comes with a command line client. On Windows, type the commands into a C
form or the properties of a module), you have to run
"xlwings vba export".
(New in 0.26.3, changed in 0.27.0)
py This functionality allows you to easily write Python code for
Microsoft's Python in Excel cells (=PY) via a local editor:
run "xlwings py edit" to export the code of the selected cell
into a local file. Whenever you save the file, the code will be
synced back to the cell.
(New in 0.30.12)
5 changes: 5 additions & 0 deletions docs/whatsnew.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Changelog
=========

v0.30.12 (Sep 18, 2023)
-----------------------

* :bdg-success:`Feature` New CLI command ``xlwings py edit``: this allows you to edit Microsoft's Python in Excel cells (`=PY`) in an external editor of your choice with auto-sync (:issue:`2331`).

v0.30.11 (Aug 26, 2023)
-----------------------

Expand Down
49 changes: 49 additions & 0 deletions xlwings/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,40 @@ def vba_edit(args):
book.save()


def py_edit(args):
try:
from watchgod import RegExpWatcher, watch
except ImportError:
sys.exit(
"Please install watchgod to use this functionality: pip install watchgod"
)
book = xw.books.active
selection = book.selection
source_path = (
selection.get_address(include_sheetname=True, external=True)
.replace("!", "")
.replace(" ", "_")
.replace("'", "")
.replace("[", "")
.replace("]", "_")
+ ".py"
)

Path(source_path).write_text(selection.formula.strip()[5:-4].replace('""', '"'))
print(f"Open {Path(source_path).resolve()} to edit!")
print("Syncing changes... (Hit Ctrl-C to stop)")
for changes in watch(
Path(".").resolve(),
watcher_cls=RegExpWatcher,
watcher_kwargs=dict(re_files=r"^.*(\.py)$"),
normal_sleep=400,
):
source_code = Path(source_path).read_text()
# 1 = Object, 0 = Value
# Note that the initial beta version only supports 1
selection.value = '=PY("{0}",1)'.format(source_code.replace('"', '""'))


def main():
print("xlwings version: " + xw.__version__)
parser = argparse.ArgumentParser()
Expand Down Expand Up @@ -1299,6 +1333,21 @@ def main():

vba_import_parser.set_defaults(func=vba_import)

# Edit =PY cells
py_parser = subparsers.add_parser(
"py",
help="""This functionality allows you to easily write Python code for
Microsoft's Python in Excel cells (=PY) via a local editor: run "xlwings py edit" to
export the code of the selected cell into a local file. Whenever you save the
file, the code will be synced back to the cell.
""",
)
py_subparsers = py_parser.add_subparsers(dest="subcommand")
py_subparsers.required = True

py_edit_parser = py_subparsers.add_parser("edit")
py_edit_parser.set_defaults(func=py_edit)

# Show help when running without commands
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
Expand Down

0 comments on commit f2e3113

Please sign in to comment.