python输出中的字体格式 [英] Fonts formatting in python output

查看:89
本文介绍了python输出中的字体格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 python 中的输出字体.我读过你可以使用 Tkinter 包,但它对我来说太复杂了.

I am trying to change the output fonts in python. I have read you can use Tkinter package but it is too complicated for me.

我正在尝试解析两个文档,一个是纯文本,另一个是斜体.我希望输出从每个文档中交替行,普通和斜体.

I am trying to parse two documents, one plain text and the other one in italics. I want the output to alternate lines from each document, plain and italic.

当前程序显示第一行普通,第二行斜体显示,之后全部显示斜体.

Currently the program displays the first line plain, the second in italics and afterwards it displays all in italics.

我想知道是否有任何方法可以更改/重置控制台输出中的字体格式.

I would like to know if there is any way to change/reset the fonts format in the console output.

谢谢,

这是程序.基本上我不知道如何获得不同格式的输出:一种是纯文本,另一种是斜体.我在 Windows 下的 Python 3 中运行它.

Here it is the program. Basically I do not know what to do to get output in different formats: one in plain text and the other in italics. I am running it in Python 3 under windows.

L1 = open ('vera.txt','r',-1,'utf-8')
L2 = open ('vera_en2.txt','r',-1,'utf-8')
O = open ('output.txt','w',-1,'utf-8')
for line in L1:
    line2 = L2.readline()
    print(line, end='')
    print(line2)
    O.write(line)
    O.write(line2)
    O.write("\n")
L1.close()
L2.close()
O.close()

推荐答案

似乎有一个 ANSIvera_en2.txt 中的转义码 将字体设置为斜体.打开文件或打印该文件第一行的 repr() 以验证这一点.

It seems likely that there is an ANSI escape code in your vera_en2.txt that sets the font to italic. Open the file or print the repr() of the first line of that file to verify this.

显然您已经在兼容 ANSI 的终端中工作,否则您将看不到斜体文本.因此,您应该能够使用 print("\033[0m") 重置字体,并使用 print("\033[3m") 再次启用斜体.

Apparently you are working in an ANSI compatible terminal already, or you wouldn't be seeing italic text. So, you should be able to reset the font with print("\033[0m"), and enable italics again with print("\033[3m").

如果这一切看起来太深奥,我建议使用 colorama.

If this all seems too esoteric, I recommend using colorama.

啊,所以你想写一个带有斜体文本的 RTF 文件.而您的 .txt 文件实际上并不是 .txt 文件.这与我上面解释的完全不同.

Ah, so you want to write an RTF file with some italic text. And your .txt files aren't actually .txt files. That's completely different from what I explained above.

通过分析实际 rtf 的纯文本,我想出了这个小例子.它生成一个带有普通、斜体和粗体字的小型 rtf 文件;\i 和 \b 用于切换这些格式.

By analyzing the plain text of an actual rtf I came up with this small example. It generate a small rtf file with normal, italic and bold words; \i and \b are used to toggle these formats.

# open file
myrtf = open('myrtf.rtf', 'w ')

# write some necessary header info
myrtf.write(r'{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}')

# write the actual text - notice I'm using raw strings (r'')
myrtf.write(r'normal \i italic \i0 \b bold \b0 normal\n')

# write end of file stuff
myrtf.write(r'}\n\x00')

# close file
myrtf.close()

另外,回答另一个问题;在控制台中获取斜体可能很困难,大多数终端不支持它.

Also, answering the other question; getting italics in the console might be difficult, most terminals just don't support it.

使上述代码示例适应您问题中的问题:

To adapt the above code sample to the problem in your question:

L1 = open ('file1.txt','r')
L2 = open ('file2.txt','r')
O = open ('output.rtf','w')

# write header
O.write(r'{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fcharset0 Arial;}}')

for line in L1:
    line2 = r"\i  " + L2.readline() + r" \i0\line\par" # make text from L2 italic
    print(line, end='')
    print(line2)
    O.write(line)
    O.write(line2)

# close rtf
O.write(r'}\n\x00')

L1.close()
L2.close()
O.close()

另一个补充:

根据我们在评论中的讨论,这是一种通过编写 html 文件而不是 rtf 来实现的方法:

Based on our discussion in the comments, here's one way to do it by writing a html file instead of rtf:

L1 = open ('file1.txt','r')
L2 = open ('file2.txt','r')
O = open ('output.html','w')

# write header
O.write(r'<html><head><title>Foobar</title></head><body><table>')

for line in L1:
    line2 = r"<i>" + L2.readline() + r"</i></br>" # make text from L2 italic
    O.write("<tr>")
    O.write("<td>%s</td>" % line)
    O.write("<td>%s</td>" % line2)
    O.write("</tr>\n")

# footer
O.write(r'</table></body></html>')

L1.close()
L2.close()
O.close()

这里可以看到file1.txt和file2.txt的结果是来自 Beowulf 的诗歌的古英语和现代版本.

Here you can see the result when file1.txt and file2.txt are Old English and modern versions of a verse from Beowulf.

这篇关于python输出中的字体格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆