I have a python script that will ultimately be converted into an .exe file. The way it is supposed to work is that a user will drag an excel file and drop it on to the python file, which will run, and create another output excel file.
However, it needs 2 inputs from the user: groups and size.
When I drop the excel file, the command prompt opens up automatically, and prompts me for the inputs. However, after submitting the values, the command prompt closes automatically without executing the rest of the script.
The problem goes away if I hard-code the values for groups and size without prompting the user.
Here is the code:
import pandas as pd
import sys
x = sys.argv[1]
file = pd.read_excel(x)
file = file.sample(frac=1).reset_index(drop=True)
file['Order'] = file.groupby('GENDER').cumcount()
file = file.sort_values(['Order', 'GENDER'], ascending=[True,False]).reset_index(drop=True)
groups = input ("Group number?")
size = input ("Group size?")
out = {}
count = 1
students = len(file)
std_sorted = 0
for i in range (0, len(file)-size, size):
if count != groups:
out["group"+str(count)] = file[i:(i+size)]
else:
out["group"+str(count)] = file[i:]
count += 1
std_sorted += size
if std_sorted != students:
out["group"+str(count)] = file[std_sorted:]
index = 0
writer = pd.ExcelWriter('output.xlsx')
for i in out:
out[i].pop("Order")
x = pd.DataFrame(out[i])
x.to_excel(writer, index = False, sheet_name="Sheet"+str(index))
workbook = writer.book # Access the workbook
worksheet= writer.sheets["Sheet"+str(index)] # Access the Worksheet
header_list = x.columns.values.tolist() # Generate list of headers
for i in range(0, len(header_list)):
worksheet.set_column(i, i, len(header_list[i])+2) # Set column widths based on len(header)
index += 1
writer.save()
input("Press Enter to exit...")at the end of your scriptprint (groups)and even that output was not printed. If I hard-code the values inside the script forgroupsandsizethen the file is executed, which is indicated by the generation of an output excel file.groupsandsizeand hit enter in the command prompt, it just closes even if I prompt the user again at the end of the script. This is why it's so confusing.inputline at the end and the print statements after thegroupsandsizeinput and I'm sure they will be printed