Fundamentals 4 min read

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.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Automating Word Report Comments with Python‑docx

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 data

Collect 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 names

Generate 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) + '\n' + "评语:" + comment[index] + "\n" + "评定人:代号22\n" + "时间:2019/12/9\n" return strC

Finally, 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) break

Readers are encouraged to explore python-docx as a powerful tool for programmatically handling Word documents.

automationscriptingEducationwordpython-docx
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.