Hello,
I am familiar with VBA, but new at Python. I wrote a program that counts how many characters there are in a bunch of text files, and inserts these figures into specified cells on whichever Excel file is currently open. The main element of the code is:
with open(active_file, 'r') as f:
for line in f:
linestring = str(line)
if linestring.startswith (tuple(ignore_string)):
line = f.next()
lines_ignored += 1
chars_ignored += len(linestring)
print "String ignored (%d characters):\n%s" % (chars_ignored, linestring)
else:
num_chars += len(line)
The script works fine when I run it from my text editor (Sublime). the next step is to create a button in my spreadsheet that runs the python code, thus filling out the spreadsheet with the required info (the value of num_chars).
I am really struggling with the tutorials on how to use ExcelPython. I got it working (ie, typing ?Py.Str(Py.Eval("1+2")) in locals returns "3"), but I don't know how to code a macro that runs my .py file.
?Py.Str(Py.Module("my programs name")) returns the expected text: <module "prog name" from "directory"
?Py.Call(Py.Str(Py.Module("my programs name"))) returns a run-time error (-2147467259 (80004005) ) with a lot of information. Before I go on typing down all the information, can anybody tell me how to run my python program from Excel?
Thanks :)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
As an alternative you might also consider xlwings, which is technically a bit simpler than ExcelPython and may be easier for you to get started with. With xlwings too you can set up a button in Excel to call a Python script.
Regards,
Eric
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I am familiar with VBA, but new at Python. I wrote a program that counts how many characters there are in a bunch of text files, and inserts these figures into specified cells on whichever Excel file is currently open. The main element of the code is:
with open(active_file, 'r') as f:
for line in f:
linestring = str(line)
if linestring.startswith (tuple(ignore_string)):
line = f.next()
lines_ignored += 1
chars_ignored += len(linestring)
print "String ignored (%d characters):\n%s" % (chars_ignored, linestring)
else:
num_chars += len(line)
The script works fine when I run it from my text editor (Sublime). the next step is to create a button in my spreadsheet that runs the python code, thus filling out the spreadsheet with the required info (the value of num_chars).
I am really struggling with the tutorials on how to use ExcelPython. I got it working (ie, typing ?Py.Str(Py.Eval("1+2")) in locals returns "3"), but I don't know how to code a macro that runs my .py file.
?Py.Str(Py.Module("my programs name")) returns the expected text: <module "prog name" from "directory"
?Py.Call(Py.Str(Py.Module("my programs name"))) returns a run-time error (-2147467259 (80004005) ) with a lot of information. Before I go on typing down all the information, can anybody tell me how to run my python program from Excel?
Thanks :)
Hi Derp,
You could try using the ExcelPython add-in. That basically writes the VBA wrapper code, which can be quite complicated, for you.
BTW are you following the tutorials on GitHub? Getting started with the ExcelPython add-in is probably what you need.
As an alternative you might also consider xlwings, which is technically a bit simpler than ExcelPython and may be easier for you to get started with. With xlwings too you can set up a button in Excel to call a Python script.
Regards,
Eric