一个个背单词太低效了,背完就忘,试试放在文章中理解意思,记得更牢固。
运用python生成一个匹配后黑体加粗相同单词的文档,边看文章边理解高亮单词的意思。
准备三个文档:
先准备一个单词表,只有英文的,每行一个单词格式;
另外一个需要匹配的英文文章;
新建一个用来装匹配后的文章的文档;
以上文档都是docx格式。
代码:
from docx import Document
from docx.shared import RGBColor
from docx.oxml import OxmlElement
def highlight_words(article_text, vocabulary):
highlighted_text = ""
words = article_text.split()
for word in words:
if word.lower() in vocabulary:
highlighted_text += f"[{word}] " # 用中括号包围匹配的词,以便之后处理
else:
highlighted_text += word + " "
return highlighted_text
def read_docx(filename):
doc = Document(filename)
text = []
for para in doc.paragraphs:
tex