由于测试数据不够等问题,很可惜最终没有参选成功,仍记录一下。
基于大学计算机基础课程研发的辅助教学工具,自动审核和批改学生提交的作业。
本人负责word板块的功能函数编写,主要采取的是通过答案属性和学生文件的对比,来判断赋分,根据前端选取的考察内容来调用内部函数。
主要写了一下的模块,需要导入第三方库docx(这个库不是很全,属性有限)
# *文字基本格式(字体,颜色,大小,加粗,斜体,高亮,下划线) # 标题:标题文本内容,标题对齐方式,文字基本格式 # 页面设置:纸张版式,纸张方向,上下左右页边距 # 正文:首行缩进,首字字体名称,行间距,段前间距,段后间距 # 查找和替换:替换文字的文字内容,文字基本格式 # 页眉和页脚:奇数偶数页不同,文字内容,文字基本格式
# 正文文字替换
from docx import Document
# 文字格式替换
# 格式包括:文字颜色,文字大小,文字是否加粗,文字字体名称
def total_test(testpath,answerpath,Score,shorttext):
doc1=Document(testpath)
doc2=Document(answerpath)
# 获取文档所有段落对象
paragraphs1 = doc1.paragraphs
paragraphs2 = doc2.paragraphs
word_count1=0
word_count2=0
for pra2 in paragraphs2[1::]:
word_count2=pra2.text.count(shorttext)+word_count2
for pra1 in paragraphs1[1::]:
word_count1=pra1.text.count(shorttext)+word_count1
# 检查文字内容是否替换
if word_count1 != word_count2:
return 0
for pra2 in paragraphs2[1::]:
for run2 in pra2.runs:
if shorttext in run2.text:
# 文字颜色
rgb_answer = run2.font.color.rgb
# 字体名称
fontname_answer = run2.font.name
# 文字大小
size_answer = run2.font.size
# 是否加粗
bold_answer = run2.font.bold
break
for pra1 in paragraphs1[1::]:
for run1 in pra1.runs:
if shorttext in run1.text:
if run1.font.color.rgb != rgb_answer:
Score=0
pass
if run1.font.name != fontname_answer:
Score=0
pass
if run1.font.size != size_answer:
Score=0
pass
if run1.font.bold != bold_answer:
Score =0
pass
return Score
# 纸张设置
from docx import Document
def Papersize_test(testpath,answerpath,Score):
doc1=Document(testpath)
doc2=Document(answerpath)
# 获取文档所有章节
sections1 = doc1.sections
sections2 = doc2.sections
# 获取章节对象的页边距等信息
sec01 = sections1[0]
sec02 = sections2[0]
if sec02.page_width != sec01.page_width:
Score=0
pass
if sec02.page_height != sec01.page_height:
Score=0
pass
return Score
def orientation_test(testpath,answerpath,Score):
doc1=Document(testpath)
doc2=Document(answerpath)
# 获取文档所有章节
sections1 = doc1.sections
sections2 = doc2.sections
# 获取章节对象的页边距等信息
sec01 = sections1[0]
sec02 = sections2[0]
if sec01.orientation != sec02.orientation:
Score = 0
pass
return Score
# 页面边距
from docx import Document
def Margin_test(testpath,answerpath,Score):
doc1=Document(testpath)
doc2=Document(answerpath)
# 获取文档所有章节
sections1 = doc1.sections
sections2 = doc2.sections
# 获取章节对象的页边距等信息
sec01 = sections1[0]
sec02 = sections2[0]
"class Section(object):"
'''
官方解释:文档节,提供对节和页面设置的访问。
还提供对页眉和页脚的访问。
'''
if sec02.left_margin != sec01.left_margin:
Score=0
pass
if sec02.right_margin != sec01.right_margin:
Score=0
pass
if sec01.top_margin != sec01.top_margin:
Score=0
pass
if sec01.bottom_margin != sec01.bottom_margin:
Score=0
pass
return Score
# 基本的文字属性判定
def basic_test(title_run1,title_run2):
Score=1
# 字体
if title_run2.font.name != title_run1.font.name:
Score = 0
pass
# 字体颜色
if title_run2.font.color.rgb != title_run1.font.color.rgb:
Score = 0
pass
# 文字大小
if title_run2.font.size != title_run1.font.size:
Score = 0
pass
# 是否加粗
if title_run2.font.bold != title_run1.font.bold:
Score = 0
pass
# 是否高亮
if title_run2.font.highlight_color != title_run1.font.highlight_color:
Score = 0
pass
# 是否有下划线
if title_run2.font.underline != title_run1.font.underline:
Score = 0
pass
# 是否斜体
if title_run2.font.italic != title_run1.font.italic:
Score = 0
pass
return Score
# 标题
from docx import Document
import basic
# 文本检测
def text_test(testpath,answerpath,Score):
doc1=Document(testpath)
doc2=Document(answerpath)
# 获取文档所有段落对象
paragraphs1 = doc1.paragraphs
paragraphs2 = doc2.paragraphs
# 获取标题信息
title1 = paragraphs1[0]
title2 = paragraphs2[0]
# 标题内容判定
if title2.text != title1.text:
Score=0
pass
return Score
# 对齐方式
def aligment_test(testpath,answerpath,Score):
doc1=Document(testpath)
doc2=Document(answerpath)
# 获取文档所有段落对象
paragraphs1 = doc1.paragraphs
paragraphs2 = doc2.paragraphs
# 获取标题信息
title1 = paragraphs1[0]
title2 = paragraphs2[0]
# 标题对齐方式判定
if title2.paragraph_format.alignment != title1.paragraph_format.alignment:
Score=0
pass
return Score
# 文字基本属性
def basic_test(testpath,answerpath,Score):
doc1=Document(testpath)
doc2=Document(answerpath)
# 获取文档所有段落对象
paragraphs1 = doc1.paragraphs
paragraphs2 = doc2.paragraphs
# 获取标题信息
title1 = paragraphs1[0]
title2 = paragraphs2[0]
# 获取段落的 run 对象列表
runs1 = title1.runs
title_run1=runs1[0]
runs2 = title2.runs
title_run2=runs2[0]
if basic.basic_test(title_run1,title_run2) == 0:
return 0
return Score
# 页眉页脚
from docx import Document
import basic
def header_test(testpath,answerpath,Score):
doc1 = Document(testpath)
doc2 = Document(answerpath)
# 获取文档所有章节
sections1 = doc1.sections
sections2 = doc2.sections
# 获取章节对象的页边距等信息
sec01 = sections1[0]
sec02 = sections2[0]
# 偶数页页眉
head01even = sec01.even_page_header # 返回页眉对象
head02even = sec02.even_page_header # 返回页眉对象
head01even = head01even.paragraphs[0]
head02even = head02even.paragraphs[0]
# 奇数页页眉
head01odd = sec01.header # 返回页眉对象
head02odd = sec02.header # 返回页眉对象
head01odd = head01odd.paragraphs[0]
head02odd = head02odd.paragraphs[0]
# 页眉文字内容检测
if head01even.text != head02even.text:
Score =0
pass
if head01odd.text != head02odd.text:
Score = 0
pass
run1e=head01even.runs[0]
run2e=head02even.runs[0]
run1o=head01odd.runs[0]
run2o=head02odd.runs[0]
# 页眉文字字体内容检测
if basic.basic_test(run1e,run2e) == 0:
return 0
if basic.basic_test(run1o,run2o) == 0:
return 0
return Score
def footer_test(testpath, answerpath, Score):
doc1 = Document(testpath)
doc2 = Document(answerpath)
# 获取文档所有章节
sections1 = doc1.sections
sections2 = doc2.sections
# 获取章节对象的页边距等信息
sec01 = sections1[0]
sec02 = sections2[0]
# 偶数页页眉
foot01even = sec01.even_page_footer # 返回页眉对象
foot02even = sec02.even_page_footer # 返回页眉对象
foot01even = foot01even.paragraphs[0]
foot02even = foot02even.paragraphs[0]
# 奇数页页眉
foot01odd = sec01.footer # 返回页眉对象
foot02odd = sec02.footer # 返回页眉对象
foot01odd = foot01odd.paragraphs[0]
foot02odd = foot02odd.paragraphs[0]
# 页眉文字内容检测
if foot01even.text != foot02even.text:
Score = 0
pass
if foot01odd.text != foot02odd.text:
Score = 0
pass
run1e = foot01even.runs[0]
run2e = foot02even.runs[0]
run1o = foot01odd.runs[0]
run2o = foot02odd.runs[0]
# 页眉文字字体内容检测
if basic.basic_test(run1e, run2e) == 0:
return 0
if basic.basic_test(run1o, run2o) == 0:
return 0
return Score