如何使用asksaveasfile将文本从文本小部件Tkinter保存到.doc? [英] How to save text from a Text-widget Tkinter to a .doc using asksaveasfile?

查看:469
本文介绍了如何使用asksaveasfile将文本从文本小部件Tkinter保存到.doc?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将在文本小部件中输入的某些内容保存为 .txt .doc 格式,使用asksaveasfilename对话框。在此之后,我希望它分别打开。记事本或MS Word。

I want to save some stuff I entered in a Text widget to a .txt or .doc format, using an asksaveasfilename dialog box. After this I want it to open in resp. Notepad or MS Word.

from tkFileDialog import asksaveasfilename
import os.path

name = asksaveasfilename(
           initialdir="dir",
           title="Save as",
           filetypes=[("Text files",".txt"),("Word files",".doc")])

data = open(name,"w")
data.write("text from text widget")

os.startfile(name)

它创建文件,但不会在MS Word或记事本中打开它。相反,它询问我如何打开此文件。如果我选择该程序,它将正确打开,但是我希望它直接打开。 (无需选择用于打开文件的程序)。当我直接在文件名:框中输入扩展名时,它的工作方式与我想要的方式相同。

It creates the file but it won't open it in MS Word or Notepad. Instead it asks how I want to open this file. If I choose the programm it will open correctly, but I want it to open directly. (without choosing a program to open the file with). When I give the extension directly in the "File name:" box it works the way I want though.

此方法有效:
文件名:something.doc
另存为:Word文件(* .doc)
--->创建something.doc并在MS Word中打开。

This works: File name: something.doc Save as type: Word file (*.doc) ---> creates something.doc and opens it in MS Word.

但是这不会
文件名:某事
另存为类型:Word文件(* .doc)
--->创建某事(无扩展名)并询问我要使用哪个程序

But this doesn't File name: something Save as type: Word file (*.doc) ---> creates something (no extension) and ask in what program I want it to open.

我使用Python 2.7.8,Windows 8,Office 2010。

I use Python 2.7.8, Windows 8, Office 2010.

推荐答案

如果添加打印名称,例如

C:/Users/jsharpe/Downloads/testing

请注意,未添加任何扩展名-我只输入了测试 文件类型参数对于限制用户对现有文件的选择更为有用,如果用户不提供扩展名,则不会添加适当的扩展名。

note that no extension has been added - I only entered "testing". The filetypes argument is more useful for limiting the user's selection of existing files, it won't add the appropriate extension if the user doesn't supply one.

对于用户没有输入扩展名的情况,您可以设置 defaultextension ,但这不会t反映在下拉列表中选择的类型(例如,如果设置 defaultextension =。txt ,则不会是 .doc ,即使用户从文件类型)中选择了该选项。

You can set a defaultextension for the cases where the user doesn't enter an extension, but this won't reflect the type selected in the drop-down (e.g. if you set defaultextension=".txt" it won't be .doc even if the user chooses that option from filetypes).

name = asksaveasfilename(defaultextension=".txt",
                         filetypes=[("Text files",".txt"),
                                    ("Word files",".doc")],
                         initialdir="dir",
                         title="Save as")

(请注意,按字母顺序排列的参数顺序会随着您添加越来越多的选项而使生活变得更轻松)

旁注,您(仍然!)不要关闭文件,这可能会导致问题-我建议在中使用用于文件处理的上下文管理器:

On a side note, you (still!) don't close the file, which could cause issues - I'd suggest using the with context manager for file handling:

with open(name, "w") as data:
    data.write("text from text widget")

这篇关于如何使用asksaveasfile将文本从文本小部件Tkinter保存到.doc?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆