1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import random,xlrd,os from docx import Document from docx.shared import Inches,Pt from docx.enum.text import WD_ALIGN_PARAGRAPH,WD_LINE_SPACING from docx.oxml.ns import qn from docx.shared import Inches def get_xlsx(sheet): "获取指定Excel数据" excel_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'question.xls') file = xlrd.open_workbook(excel_path) list = [] sheet = file.sheet_by_name(sheet) row_value1 =sheet.row_values(0) nrows = sheet.nrows ncols = sheet.ncols for i in range(1, nrows): row = sheet.row_values(i) dict = {} for j in range(0, ncols): if row_value1[j] == '序号': dict[row_value1[j]] = int(row[j]) else: dict[row_value1[j]] = row[j] list.append(dict) return list def router_docx(choice1,choice2,choice3,choice4,paper_path,name): "生成试题及答案" docx1 = Document() docx2 = Document() docx1.styles['Normal'].font.name = '宋体' docx1.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体') docx1.styles['Normal'].font.size = Pt(11) docx1.styles['Normal'].paragraph_format.space_before = Pt(0) docx1.styles['Normal'].paragraph_format.space_after = Pt(0) docx1.styles['Normal'].paragraph_format.line_spacing_rule = WD_LINE_SPACING.ONE_POINT_FIVE sec = docx1.sections[0] sec.left_margin = Inches(1) sec.right_margin = Inches(1) sec.top_margin = Inches(0.5) sec.bottom_margin = Inches(0.5) p=docx1.add_paragraph() run = p.add_run('试题(%s)' % name) run.font.name = '微软雅黑' run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') run.font.size = Pt(18) p.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER docx1.add_paragraph('【说明】') docx1.add_paragraph('1.笔试时间为60分钟。') docx1.add_paragraph('2.请将答案写在答题卡上,且不允许在试题卷上做任何涂写和标记。') q=docx2.add_paragraph() run = q.add_run('答案(%s)' % name) run.font.name = '微软雅黑' run._element.rPr.rFonts.set(qn('w:eastAsia'), '微软雅黑') run.font.size = Pt(18) q.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER p1 = docx1.add_paragraph() p1.paragraph_format.space_before = Pt(12) docx2.add_paragraph('一、选择题') run = p1.add_run('一、选择题(每题3分共45分)') run.bold = True list1=random.sample(range(0,len(choice1)-1),15) x=1 for y in list1: docx1.add_paragraph(str(x)+'、'+choice1[y]['题目']) docx1.add_paragraph(choice1[y]['选项A']) docx1.add_paragraph(choice1[y]['选项B']) docx1.add_paragraph(choice1[y]['选项C']) p11=docx1.add_paragraph(choice1[y]['选项D']) p11.paragraph_format.space_after = Pt(12) docx2.add_paragraph(str(x)+'、'+choice1[y]['答案']) x+=1 p2 = docx1.add_paragraph() p2.paragraph_format.space_before = Pt(12) docx2.add_paragraph('二、填空题') run = p2.add_run('二、填空题(每题3分,共15分)') run.bold = True list2 = random.sample(range(0, len(choice2)-1), 5) i = 1 for j in list2: docx1.add_paragraph(str(i) + '、' + choice2[j]['题目']) docx2.add_paragraph(str(i) + '、' + str(choice2[j]['答案'])) i += 1 p3 = docx1.add_paragraph() p3.paragraph_format.space_before = Pt(12) docx2.add_paragraph('三、简答题') run = p3.add_run('三、简答题(每题10分,共20分)') run.bold = True list3 = random.sample(range(0, len(choice3)-1), 2) n = 1 for m in list3: docx1.add_paragraph(str(n) + '、' + choice3[m]['题目']) docx1.add_paragraph('\r') docx2.add_paragraph(str(n) + '、' + choice3[m]['答案']) n += 1 p4 = docx1.add_paragraph() p4.paragraph_format.space_before = Pt(12) docx2.add_paragraph('四、综合题') run = p4.add_run('四、综合题(共20分)') run.bold = True list4 = random.randint(0, len(choice4)-1) docx1.add_paragraph('1、' + choice4[list4]['题目']) docx2.add_paragraph(choice4[list4]['答案']) docx1.save(os.path.join(paper_path, '试题(%s).docx' % name)) docx2.save(os.path.join(paper_path, '答案(%s).docx' % name)) def main(): paper_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), '试卷') if not os.path.exists(paper_path): os.mkdir(paper_path) choice1 = get_xlsx('选择题') choice2 = get_xlsx('填空题') choice3 = get_xlsx('简答题') choice4 = get_xlsx('综合题') for i in range(1,4): router_docx(choice1, choice2, choice3,choice4, paper_path, i) if __name__ == "__main__": main()
|