I would like to run a Python script from Excel. The Python script has the task to create a file. As a help I used the quickstart "Call Python from Excel" as you can see here:
https://docs.xlwings.org/en/stable/quickstart.html
However, I don't want Python to write "hello world" to Excel, but instead create a file:
import numpy as np
import xlwings as xw
def world():
wb = xw.Book.caller()
wb.sheets[0].range('A1').value = 'Hello World!'
Instead of the code shown above I replaced the lines with:
import numpy as np
import xlwings as xw
def world():
f = open("myfile.txt", "w")
However, no file is created. I have not found an answer to this problem on my research, so I ask for help here.
You need to save the workbook for anything to be created
https://docs.xlwings.org/en/stable/api.html#xlwings.Book.save
In your second example, you're not specifying a full path - The working directory may be different to what you expect. Instead of "myFile.txt" can you do "c:\full\path\to\folder\myFile.txt" (Obviously change the path to suit your environment)