【问题标题】:Input Function to Name File when saving保存时输入函数到名称文件
【发布时间】:2020-10-05 11:38:10
【问题描述】:

我想要求我的代码的用户输入所需的文件名 f.e. 123456.xlsx。我正在使用wb.Save() 关闭/保存文件。现在我想输入 f.e.以下路径:'H:\Desktop\Example\123456.xlsx'。现在我想使用输入函数,这样用户就可以随时更改完成文档的所需名称:'H:\Desktop\Example\input('Desired name of file').xlsx'

【问题讨论】:

    标签: python file input save


    【解决方案1】:

    我不知道您到底在哪里找到了问题,但这应该可以:

    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))
    

    【讨论】:

    • 此解决方案返回错误:Save() 采用 1 个位置参数,但给出了 2 个。
    【解决方案2】:

    您可以使用 + 运算符来连接字符串

    wb.save('H:\Desktop\Example\' + input("file name to save as") + ".xlsx")
    

    【讨论】:

      【解决方案3】:

      我想像-

      wb.save(f'H:\Desktop\Exemple\{input("Your File name: ")}.xlsx')
      

      应该可以。

      【讨论】:

        【解决方案4】:

        您是否尝试过类似的方法:

        filename = input("choose file name")
        

        然后你可以使用f'H:\Desktop\Exemple\{filename}.xlsx'

        【讨论】:

          【解决方案5】:

          起初它没有成功,但在重新打开文件并再次保存后。就像@jail_BM 解释的那样,它工作得很好。

          【讨论】:

            猜你喜欢
            • 2019-12-07
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-04-23
            • 1970-01-01
            • 1970-01-01
            • 2013-12-24
            • 2011-02-01
            相关资源
            最近更新 更多