Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python to generate word Test Paper in an examination system

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "how to use python to generate word test papers in an examination system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Preparation condition

1. The excel information of the test questions is stored in the excel file named data.xls

two。 Install module information that python depends on

Pip install xlrdpip install python-docx

Tip: the following is the main body of this article. The following examples are available for reference.

Coding implementation #! / bin/bash env pythonimport xlrdimport randomfrom docx import Documentfrom docx.shared import Pt RGBColorfrom docx.enum.text import WD_ALIGN_PARAGRAPH# opens exceldata = xlrd.open_workbook ('data.xls') # to get worksheet sheet = data.sheet_by_index (0) class Question: passdef create_question (): question_list = [] for i in range (sheet.nrows): if I > 2: # create the question class question = Question () question.ID = sheet.cell (I 0). Value # add question information question.subject = sheet.cell (iMagazine 1). Value # add question type question.question_type = sheet.cell (iMagazine 2). Value # add question option question.option = [] question.option.append (sheet.cell (I) 3) .value) # A question.option.append (sheet.cell (I, 4) .value) # B question.option.append (sheet.cell (I, 5) .value) # C question.option.append (sheet.cell (I, 6) .value) # D # add the score question.score = sheet.cell (I 7). Value question_list.append (question) # randomly scrambles the test questions and returns random.shuffle (question_list) return question_listdef create_papper (file_name,paper_name) Question_list): # create a document object document = Document () # set the location information of the header section = document.sections [0] header = section.header p1 = header.paragraphs [0] p1.text = paper_name # set footer information footer = section.footer p2 = footer.paragraphs [0] p2.text = 'internal test questions It is forbidden to divulge the basic information of'# writing test paper titile = document.add_heading (paper_name Level=1) # set alignment titile.alignment = WD_ALIGN_PARAGRAPH.CENTER # add a paragraph p3 = document.add_paragraph () p3.add_run ('name: _ _') p3.add_run ('class: _ _') p3.alignment = WD_ALIGN_PARAGRAPH.CENTER # write test information for I Question in enumerate (question_list): subject_paragraph = document.add_paragraph () # add a paragraph run = subject_paragraph.add_run (str (iTun1) + str (question.subject)) # add topic information run.bold = True # set bold subject_paragraph.add_run ('[% s] score'% str (question.score)) # disrupt the order of options Random.shuffle (question.option) for index Option in enumerate (question.option): document.add_paragraph (('ABCD') [index] + str (option)) # Save test questions document.save (file_name) returnif _ name__ =' _ _ main__': question_list = create_question () # generate 100copies of test paper for item in range (1100): create_papper ('2021' + str (item) + 'set of internal examination questions .docx' Internal examination in the first quarter of 2021, question_list) print ('over'), "how to use python to generate word papers in an examination system" is here. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report