8

如果您正在使用 python 写入文件,有没有办法使文本的某些部分变为粗体、斜体或下划线?

我试过了:

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 是最基本的格式化文本,但如果我错了,请纠正我

4

1 回答 1

10

一直在玩这个假设的 MS 字,我发现您需要将文档包装在“{}”中并定义文档类型,然后以“\b”开始粗体并以“\b0”结束。一个例子是

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' 有特殊的含义。

完整信息来自http://www.pindari.com/rtf1.html,其中还描述了斜体、字体等。

让我知道这是否对你有用。

于 2013-04-23T06:31:06.540 回答