Automating Word Report Comments with Python‑docx
This article demonstrates how to use Python and the python‑docx library to read student scores from CSV files, generate appropriate evaluation comments, and programmatically insert those comments into each student's Word report, streamlining the grading workflow.
Many repetitive tasks in daily life can be solved with code; this example shows how to automate the insertion of evaluation comments into Word lab reports for over a hundred students using Python.
First, install the python-docx library: pip install python-docx Import the Document class to open Word files: from docx import Document Read the scores from a CSV file (the readScore function) and store them as a list of records.
def readScore(name):
import csv
vec = []
data = []
with open(name, 'rb') as f:
reader = csv.reader(f)
flag = 1
for row in reader:
if flag:
flag = 0
continue
for i in row:
vec.append(i.decode("utf8"))
data.append(vec)
vec = []
return dataCollect the file names of all Word reports (the getFileName function) by walking through the target folder:
def getFileName(folderName):
for root, dirs, files in os.walk(folderName):
files = files
names = []
for file in files:
if "docx" in file:
names.append(folderName + '/' + file)
print "读取文件名字成功"
print names
return namesGenerate a comment based on the numeric score (the getComment function) with four predefined remarks:
def getComment(score):
score = int(score)
comment = ['功能实验完整,报告相对清晰。', '实现功能,报告略简洁。', '缺乏部分功能、不完全符合要求。', '实验不完整,报告敷衍。']
if score >= 85:
index = 0
elif score >= 75:
index = 1
elif score >= 65:
index = 2
else:
index = 3
strC = "成绩:" + str(score) + '
' + "评语:" + comment[index] + "
" + "评定人:代号22
" + "时间:2019/12/9
"
return strCFinally, insert the generated comment into the first paragraph of each corresponding Word report (the setComment function) and save the modified document:
def setComment(score, filename):
for name in filename:
for i in range(len(score)):
if score[i][0] in name:
file = Document(name)
str = getComment(score[i][2]).decode("utf8")
file.paragraphs[0].insert_paragraph_before(str)
file.save(name)
breakReaders are encouraged to explore python-docx as a powerful tool for programmatically handling Word documents.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
