Modifying the color of word keywords using Python docx

  • 2021-09-16 07:35:37
  • OfStack

Requirements:

When brushing word question bank, the answer is below the question, which interferes with the review effect and turns the answer font into white. When viewing the answer, only the background of the answer needs to be brushed black

Conversion requirements:

Find the keyword "answer" in word and change the color of the information behind it to white

Because I used import docx for the first time, it was troublesome to realize it according to the above idea at first. After finishing the idea, I saved the question bank as txt, read it one by one and transferred it to word, and divided the keywords by using the segmentation function. The information behind the keywords changed the color, effect and demand for the answer. 1, but a new file was created

Implementation code:


import os
import re
import docx
from docx.shared import RGBColor# Set fonts 
from docx import Document
from docx.shared import Pt# Set fonts 
from docx.oxml.ns import qn# Set Chinese fonts 
f = open("test.txt","r")# Combine the document with the python Put on 1 There is no need for complicated paths in directories 
i=0
document = Document()# New word
p = document.add_paragraph('')# Create a new paragraph. Put this sentence outside the loop to reduce blank lines 
while i<3190:
	content = f.readline()
	#print(content)
	
	if content.find(' Answer ')!=-1: # If the line has the keyword "answer", it is divided by the keyword as the boundary 
		pt = r'( Answer )'
		res = re.split(pt, content)# Segmentation 
		#print(res[0],end=' ')
		#print(res[1],end=' ')
		#print(res[2])
		
		#add_run In the same 1 Add content to paragraph 
		run = p.add_run(res[0])# Enter the character before the keyword 
		run.font.name=u' Song Style ' # Set the font to be inserted 
		run.font.size = Pt(15)
		r = run._element
		r.rPr.rFonts.set(qn('w:eastAsia'), u' Song Style ')
		run = p.add_run(res[1])# Enter keywords 
		run.font.name=u' Song Style ' # Set the font to be inserted 
		run.font.size = Pt(15)
		r = run._element
		r.rPr.rFonts.set(qn('w:eastAsia'), u' Song Style ')
		run = p.add_run(res[2])# Enter the character after the keyword 
		run.font.name=u' Song Style ' # Set the font to be inserted 
		run.font.size = Pt(15)
		r = run._element
		r.rPr.rFonts.set(qn('w:eastAsia'), u' Song Style ')
		# After setting the keyword, that is, the font color of the answer, which is set to white here 
		run.font.color.rgb = RGBColor(250,250,250)
	else:
		run = p.add_run(content)# If there is no keyword "answer" in this line, enter it directly word
		#print(content)
		run.font.name=u' Song Style ' # Set the font to be inserted 
		run.font.size = Pt(15)
		r = run._element
		r.rPr.rFonts.set(qn('w:eastAsia'), u' Song Style ')
	i+=1
document.save(' Route 1.docx')# Turn off saving word
f.close() # Shut down TXT

Requirements were quite simple, because of the first use of DOCx, take a small bend and make a record

Add: If there are multiple keywords, you can use the following code


for one_key in keys:
 if content.find(one_key) != -1:
  p = document.add_paragraph()
  pt = r'({})'.format(one_key)
  res = re.split(pt, content)
  for one_split in res:
   run = p.add_run(one_split)
   run.font.name = u' Song Style '
   r = run._element
   r.rPr.rFonts.set(qn('w:eastAsia'), u' Song Style ')
   if one_split == one_key:
    run.font.color.rgb = RGBColor(220, 20, 60)
    run.font.bold = True
  break
else:
 p = document.add_paragraph(u'{}'.format(content))

Related articles: