用python打印彩色文本
我有一些文字,特别强调了一些单词用python打印彩色文本,python,plot,Python,Plot,我有一些文字,特别强调了一些单词 from termcolor import colored text='left foot right foot left foot right. Feet in the day, feet at night.' l1=['foot','feet'] result = " ".join(colored(t,'white','on_red') if t in l1 else t for t in text.lower().split()) print(result
from termcolor import colored
text='left foot right foot left foot right. Feet in the day, feet at night.'
l1=['foot','feet']
result = " ".join(colored(t,'white','on_red') if t in l1 else t for t in text.lower().split())
print(result)
我对pyhthon比较陌生,我想知道是否有办法创建一个视觉/情节来显示句子并将其打印到屏幕上。我只想打印几个句子。如果我能将整个故事(10万字)打印到文档文件中,那就太好了
我想到了画突出显示的句子,想到了matplotlib,但我认为这行不通。还有其他的视觉效果吗
如果可能,我想打印并保存png等:
非常有趣的问题。我为您提供了一个针对几个句子的案例的解决方案,但是如果您想为整个文档创建这个,我建议您使用任何PDF编写器
import matplotlib.pyplot as plt
def color_code():
text='left foot right foot left foot right. Feet in the day, feet at night.'
text_split = text.split(" ")
l1=['foot','feet']
# Pixels where you want to start first word
start_x = 20
start_y = 450
# Decide what is the last pixel for writing words in one line
end = 600
# Whitespace in pixels
whitespace = 8
# Create figure
figure = plt.figure()
# From renderer we can get textbox width in pixels
rend = figure.canvas.get_renderer()
for word in (text_split):
# Text box parameters and colors
bbox = dict(boxstyle="round,pad=0.3", fc="red", ec="b", lw=2)
# Check if word contains "foot", "feet", "foot." or "feet." or caps locked.
# Depending what you are trying to achieve.
if (word in l1 or word.replace('.','') in l1 or word.lower() in l1):
txt = plt.text(start_x, start_y, word, color="black", bbox=bbox,transform=None)
else:
txt = plt.text(start_x, start_y, word, transform=None)
# Textbox width
bb = txt.get_window_extent(renderer=rend)
# Calculate where next word should be written
start_x = bb.width + start_x + whitespace
# Next line if end parameter in pixels have been crossed
if start_x >= end:
start_x = 20
start_y -= 40
# Skip plotting axis
plt.axis("off")
# Save and plot figure
plt.savefig("plot.png")
plt.show()
color_code()
字符串较长的结果应如下所示:
非常有趣的问题。我为您提供了一个针对几个句子的案例的解决方案,但是如果您想为整个文档创建这个,我建议您使用任何PDF编写器
import matplotlib.pyplot as plt
def color_code():
text='left foot right foot left foot right. Feet in the day, feet at night.'
text_split = text.split(" ")
l1=['foot','feet']
# Pixels where you want to start first word
start_x = 20
start_y = 450
# Decide what is the last pixel for writing words in one line
end = 600
# Whitespace in pixels
whitespace = 8
# Create figure
figure = plt.figure()
# From renderer we can get textbox width in pixels
rend = figure.canvas.get_renderer()
for word in (text_split):
# Text box parameters and colors
bbox = dict(boxstyle="round,pad=0.3", fc="red", ec="b", lw=2)
# Check if word contains "foot", "feet", "foot." or "feet." or caps locked.
# Depending what you are trying to achieve.
if (word in l1 or word.replace('.','') in l1 or word.lower() in l1):
txt = plt.text(start_x, start_y, word, color="black", bbox=bbox,transform=None)
else:
txt = plt.text(start_x, start_y, word, transform=None)
# Textbox width
bb = txt.get_window_extent(renderer=rend)
# Calculate where next word should be written
start_x = bb.width + start_x + whitespace
# Next line if end parameter in pixels have been crossed
if start_x >= end:
start_x = 20
start_y -= 40
# Skip plotting axis
plt.axis("off")
# Save and plot figure
plt.savefig("plot.png")
plt.show()
color_code()
字符串较长的结果应如下所示:
使用相同的逻辑,将10万个文档写入doc或pdf实际上相当容易使用相同的逻辑,将10万个文档写入doc或pdf实际上相当容易