0

I'm using the last python version to date (3.13.1). I can compile code with xlwt, and executing my code though the console works. But when I want to execute the file double-click style it fails to create the xls file. It also fails when I export it as an .exe

Researching this issue, it seems it might not be xlwt fault since many people had the same problem with different python-to-excel libraries. I wonder if the issue is the python version itself (or a mix of both)

Does anyone know a reliable python3 version for writing on xls/xlsx and compiling to .exe? Or a recommended python-to-excel library since xlwt was archived in 2020?

Edit: It wasn't xlwt nor python, it was the SO. I'll leave the answer here so anyone with the same problem know how to solve it.

4
  • It sounds like you're just executing the code in the wrong virtual environment. I'm guessing the code is failing with a ModuleNotFound error because you're running it in an environment that doesn't have the xlwt` library installed. Either run it using a terminal after enabling the virtual environment, or make a batch script that activates the environment then runs your code, and you can double-click that batch script instead. Commented Dec 29, 2024 at 19:36
  • Nope. Is the same environment, it all works perfect until executing save() which should create the file but it doesn't. I don't know how to see if there is an error since it works fine from the console. Commented Dec 29, 2024 at 20:25
  • 1
    Throw a try over the entire entry point and log the error. Commented Dec 29, 2024 at 20:30
  • 1
    When you 'double-click' it, Windows is likely using a different version of Python, or a different Python environment to execute your script in. Write a small script that writes the Python version and perhaps the installed packages to the screen and then waits for user input. Save that as a .py and run it by double-clicking it and you should be able to confirm what Python is set to handle the double-click. Commented Dec 29, 2024 at 23:04

1 Answer 1

0

It took me some time to understand what's going on because there are a lot of unanswered questions like this out there, and similar problems for different reasons.

I logged the error:

try:
    wb.save(file)
except Exception as e:
   # this way we can know about the type of error occurring
    print("The error is: ",e)
    time.sleep(30)

And find the issue.

#The error is:  [Errno 13] Permission denied: 

I didn't wanted to modify permissions for different reasons, so I opted for this solution:

#create folder if not exists
path = f'{tempfile.gettempdir()}' + "\\This_program\\"
file = path + f'{program_name}' + ".xls"
os.makedirs(os.path.dirname(file), exist_ok=True)

Then I can open the folder once I'm done:

wb.save(file)
subprocess.Popen(f'explorer {os.path.realpath(path)}')

I had to add this imports:

import tempfile
import subprocess
import os

And that's all! I hope the next person with this issue find this post. I now think it looks pretty obvious how to procede but at the moment was very confusing.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.