保存时输入函数命名文件
Input Function to Name File when saving
我想请我的代码的用户输入所需的文件名 f.e。 123456.xlsx。我是 closing/saving 与 wb.Save()
的文件。现在我要输入f.e。以下路径:'H:\Desktop\Example3456.xlsx'。现在我想使用输入功能,这样用户就可以随时更改完成文档的所需名称:'H:\Desktop\Example\input('Desired name of file').xlsx'
你有没有尝试过类似的东西:
filename = input("choose file name")
然后你可以使用 f'H:\Desktop\Exemple\{filename}.xlsx'
您将使用 + 运算符连接字符串
wb.save('H:\Desktop\Example\' + input("file name to save as") + ".xlsx")
我觉得像-
wb.save(f'H:\Desktop\Exemple\{input("Your File name: ")}.xlsx')
应该可以。
我不知道你到底在哪里发现了问题,但这应该有效:
output_file_name = input("Please enter the output file name:\n> ")
# Your work code here
# Then save the file
wb.save(r'H:\Desktop\Example\{}.xlsx'.format(output_file_name))
起初它没有成功,但在重新打开文件并再次保存之后。它工作得很好,就像@jail_BM.
解释的那样
我想请我的代码的用户输入所需的文件名 f.e。 123456.xlsx。我是 closing/saving 与 wb.Save()
的文件。现在我要输入f.e。以下路径:'H:\Desktop\Example3456.xlsx'。现在我想使用输入功能,这样用户就可以随时更改完成文档的所需名称:'H:\Desktop\Example\input('Desired name of file').xlsx'
你有没有尝试过类似的东西:
filename = input("choose file name")
然后你可以使用 f'H:\Desktop\Exemple\{filename}.xlsx'
您将使用 + 运算符连接字符串
wb.save('H:\Desktop\Example\' + input("file name to save as") + ".xlsx")
我觉得像-
wb.save(f'H:\Desktop\Exemple\{input("Your File name: ")}.xlsx')
应该可以。
我不知道你到底在哪里发现了问题,但这应该有效:
output_file_name = input("Please enter the output file name:\n> ")
# Your work code here
# Then save the file
wb.save(r'H:\Desktop\Example\{}.xlsx'.format(output_file_name))
起初它没有成功,但在重新打开文件并再次保存之后。它工作得很好,就像@jail_BM.
解释的那样