2013-04-23 48 views
4

如果您正在使用python写入文件,是否有任何方法使文本的某些部分变为粗体,斜体或下划线?有没有办法从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)在标题中提出真正的问题:“如何使用Python编写RTF文件?” (或者任何期望的格式;例如DOC,DOCX,Open Office使用的任何等等)和2)包括错误症状(通常包括错误消息)。 – user2246674 2013-04-23 06:01:43

+1

你必须'out_file.close()'才能再次打开它。 – Volatility 2013-04-23 06:01:44

回答

7

刚刚玩弄这个假设MS字,我发现你需要包装文档在'{}',并定义doctype,然后用'\ 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,这也说明楷体,字体等来到

让我知道这是否为你工作。

相关问题