简体   繁体   English

使用Python中包含的格式标记从Gtk3 TextView / TextBuffer获取文本

[英]Get text from Gtk3 TextView/TextBuffer with formatting tags included in Python

I'm working on a Python 3 project that uses the Gtk3 TextView/TextBuffer to get a user's input, and I've got it working to where I can have the user typing in rich text and able to format it as Bold/Italic/Underline/Combination of these. 我正在研究一个使用Gtk3 TextView / TextBuffer来获取用户输入的Python 3项目,并且可以将用户输入富文本格式并将其格式化为粗体/斜体/下划线/这些的组合。

However, I'm stuck on trying to figure out how to get the text from the TextBuffer with those flags included so I can use the formatting flags to convert the text to properly formatted HTML when I need to. 但是,我一直试图找出如何从包含这些标志的TextBuffer中获取文本,因此我可以在需要时使用格式标志将文本转换为正确格式的HTML。

Calling textbuffer.get_text(start, end, True) simply returns the text without any flags. 调用textbuffer.get_text(start, end, True)只会返回没有任何标志的文本。

Here's the code and the editor.glade file . 这是代码editor.glade文件 Save them both in the same directory. 将它们都保存在同一目录中。

How can I get the text with the flags included? 如何获得带有标志的文本? Or, alternatively, is there a way I can get the user's input formatted as HTML automatically in another variable automatically? 或者,是否有一种方法可以自动在另一个变量中自动将用户输入的格式设置为HTML?

That's not very easy. 那不是很容易。 Here is a link to some code that I once wrote to do the same thing for RTF output. 是一些代码的链接,我曾经编写这些代码以对RTF输出执行相同的操作。 You can probably adapt it to produce HTML output. 您可能可以对其进行调整以产生HTML输出。 If you manage to do so, I'd possibly integrate it into that library's successor. 如果您设法这样做,我可能会将其集成到该库的后继程序中。

Alternatively, if you prefer text processing to the above, you can export the rich text in GtkTextBuffer's internal serialization format and convert it to HTML yourself later: 另外,如果您更喜欢上面的文本处理,则可以采用GtkTextBuffer的内部序列化格式导出富文本格式,并稍后自己将其转换为HTML:

format = textbuffer.register_serialize_tagset('my-tagset')
exported = textbuffer.serialize(textbuffer, format, start, end)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM