如果您正在使用 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 是最基本的格式化文本,但如果我错了请纠正我
最佳答案
只是一直在研究这个假设的 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 ,它还描述了斜体、字体等。
如果这对您有用,请告诉我。
关于python - 有没有办法从 Python 编写格式化文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16162036/