有没有办法从Python编写格式化的文本? [英] Is there a way to write formatted text from Python?
问题描述
如果您使用python写入文件,是否可以使文本的某些部分变为粗体,斜体或带下划线?
If you are writing to a file with python, is there any way to make certain parts of the text bold, italic, or underlined ?
我尝试过:
test = '/location/tester.rtf'
out_file = open(test,'w')
out_file.write('is this {\bold}?')
out_file.close() #thanks to the comment below
是否可以通过python编写诸如粗体,斜体或带下划线的文本之类的格式化文本?我觉得.rtf是最基本的格式化文本,但是如果我输入错了,请纠正我
is it possible to write FORMATTED TEXT like bold, italic, or underlined text via python ? i feel like .rtf is the most basic formatted text but correct me if i'm wrong
推荐答案
假设这个MS单词一直在玩,我发现您需要将文档包装在'{}'中并定义doctype,然后以粗体开头'\ b'并以'\ b0'结尾.一个例子是
Just been playing around with this assuming MS word, I found that you needed to wrap the document in '{}' and define the doctype, then start bold with '\b' and end with '\b0'. An example would be
test = 'tester.rtf'
out_file = open(test,'w')
out_file.write("""{\\rtf1
This is \\b Bold \\b0\line\
}""")
out_file.close() #thanks to the comment below
请注意双引号'\',因为python对'\ b'和'\ r'具有特殊含义.
Note the double '\' since python has special meanings for '\b' and '\r'.
完整信息来自 http://www.pindari.com/rtf1.html ,其中还描述了斜体,字体等.
The full info came from http://www.pindari.com/rtf1.html, which also describes italics, font etc.
让我知道这是否对您有用.
Let me know if that worked for you.
这篇关于有没有办法从Python编写格式化的文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!