python - 有没有办法从 Python 编写格式化文本?

标签 python text format fwrite

如果您正在使用 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/

相关文章:

java - Textalignment 在 java swing jTextPane 中不起作用

r - 从 R 中的数字和字母列表中分离多个数字

python - 使用 `str.format` 函数但不使用 %s 语法时出现键错误

php - 标准时间的日期时间格式方法(laravel 5.3)

python - 尝试用 Python 解决 Monty Hall 问题

python - 将 <Return> 绑定(bind)到按钮未按预期工作

python - spyder IDE 侧边栏、重命名、查找所有出现的地方?

python - 在 Cython 中使用 regex.h C 库

css - 如何使基本文本更具吸引力?

python - NaN 值与分隔符相同 - 如何导入?