Error Questions About Creating Python Files

Asked 2 years ago, Updated 2 years ago, 114 views

When you create a file through Python openpyxl, running it in a vcode environment works well, but running it through an exe file through pyinstaller results in an error.I've been looking for it, but I can't figure it out. I don't think it's a problem with the path, and there seems to be a problem with the part where you create and write the file and save it, but I don't how to solve it.

from openpyxl import Workbook

wb = Workbook()

# # grab the active worksheet
ws = wb.active


# # Data can be assigned directly to cells
ws['A1'] = 42

# # Rows can also be appended
ws.append([1, 2, 3])

# # Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()

# # Save the file
wb.save("test.xlsx")

print("Complete!")

# Line Stop
a = input()

openpyxl python pyinstaller

2022-09-20 08:42

1 Answers

I thought it was a matter of route We proceeded with the code below using the path as the absolute path.

from openpyxl import Workbook
import os

dir = r"C:\Users\82107\Desktop\TEST"

os.chdir(dir)

# ------ Create txt file1
open("test1.txt", "a", encoding='utf-8').close()
f = open("test1.txt", "w")
f.write ("test 1")
f.close()
print("test1 creation succeeded!")

# ----- Create txt file2
# # open("test2.txt", "a").close()
f = open("test2.txt", "w")
f.write("test2")
f.close()
print("test2 creation succeeded!")


# ----- Create xlsx file
wb = Workbook()
ws = wb.active
ws['A1'] = 42
ws.append([1, 2, 3])
import datetime
ws['A2'] = datetime.datetime.now()
wb.save("test_xlsx.xlsx")
print("test_xlsx creation succeeded!")

a = input()

When running in a vcode environment, the file was successfully created as shown below.

However, when you created the test.exe file using the pyinstaller -w-F test.py command and ran the exe file, the test1.txt file was successfully created, but the test2.txt, test_xlsx.The xlsx file was not created and the following error occurred:


2022-09-20 08:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.