Reputation:
I'm using xlwings to create a Python UDF for Excel. So I create a new Excel file, do the whole setup and the UDF returns me correct results. After that, I save the Excel file, close it, reopen it and re-import the function to use again. But that's when the UDF stops working. It doesn't give me any error messages. Instead it simply starts to return wrong results, despite the fact I didn't change anything. More specifically, the UDF informs that number_of_files is 0, even though folder Docs_Received has several pdf files saved. I looked up for previous questions, tested multiple times and double-checked the file settings but I still have no clue. Does any one know how I can make a UDF work correctly after reopening a file?
def FILE_COUNT():
folder_name = "Docs_Received"
number_of_files = 0
for file in glob.glob(os.path.join(os.path.abspath(os.path.curdir),folder_name,"[!~]*.pdf")):
number_of_files += 1
return number_of_files
Upvotes: 0
Views: 56
Reputation: 94
Your code works for me after saving and re-opening.
You shouldn't have to re-import the UDFs. You should save as a macro enabled file (as .xlsm or.xlsb).
Upvotes: 0