I'm using Jupyter Notebook to run a piece of code that outputs an Excel file into a SharePoint folder; however, that file is only sent to the folder AFTER I manually shutdown the program (see below). Is there a piece of code that I can add to the program that would shut the program down automatically after it runs?
path = r"C:\Users\XXXXX\OneDrive - XXXXXX\Update"
os.chdir(path)
filename = 'Boarder_Data' DateRange '.xlsx'
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
blank.to_excel(writer, sheet_name='Graphs',float_format="%.0f")
workbook = writer.book
worksheet = writer.sheets['Graphs']
DF.to_excel(writer, index=False, sheet_name='XXXX')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
CodePudding user response:
You could add it to a function and exit it after the code in this function is finished. Then your code will automatically stop after the function is done. If you want to write more code after that function just replace the exit()
with a break()
.
def function():
path = r"C:\Users\XXXXX\OneDrive - XXXXXX\Update"
os.chdir(path)
filename = 'Boarder_Data' DateRange '.xlsx'
writer = pd.ExcelWriter(filename, engine='xlsxwriter')
blank.to_excel(writer, sheet_name='Graphs', float_format="%.0f")
workbook = writer.book
worksheet = writer.sheets['Graphs']
DF.to_excel(writer, index=False, sheet_name='XXXX')
# Close the Pandas Excel writer and output the Excel file.
writer.save()
exit()
function()
CodePudding user response:
Perhaps a context manager will give you the behavior you're looking for.
with pd.ExcelWriter(filename, engine='xlsxwriter') as writer:
blank.to_excel(writer, sheet_name='Graphs',float_format="%.0f")
workbook = writer.book
worksheet = writer.sheets['Graphs']
DF.to_excel(writer, index=False, sheet_name='XXXX')
writer.save()