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

Convert word documents (multiple choice questions) to excl tables

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

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

Word to excl form

Colleagues are doing a batch work, which is to transfer the topics and options from the word document to the xml document.

Example:

Turn to:

There are many tools and examples to convert various documents on the Internet, but few of them convert fixed formats.

Because before this is also unknown, the boss directly asked to write a script, instinctively feel that this script is not very easy to write, there is danger. But there is no reason not to do it if you are afraid, and you will not do it as well.

First of all, determine the idea of transformation.

1, looked up a lot of information, to excl is to use json files to dictionary to excl or dictionaries, tuples, lists to json and then to excl. In the final analysis, it is to transfer the dictionary to excl. The good news is that the question can be treated as key, and the options and answers as values. This fits very well with the conversion process.

2. Then the next problem becomes how to convert the content of an word document into a dictionary. In fact, it is also very simple, and then a little bit of analysis will be OK.

Environmental issues:

Python3: in the writing process, due to the emergence of Chinese, will encounter coding problems, and python3 will optimize the coding very well, so choose python3. (if you have garbled code or incorrect coding, please consider the problem of coding conversion right or wrong.)

Operating system: linux (deepin)

A difficult and simple process of writing

1. Convert the word document to dictionary form

(1) read the contents of the word document. The module, python-docx, is given in python.

Download module:

Sudo pip3 install python-docx

The method of this module we need to use is very simple, just need to read and output.

Import docxfile = docx.Document (". / Cryptography contest exercise .docx")

Document () is used to open a document

(2) cycle out the content of the document and add the content to the dictionary

From the word document, I found that starting from the sixth line, there are no six actions with a question plus an answer. So I came up with the idea of looping out each topic first, and then adding the contents of each topic in a loop into the dictionary. And because each key pair has multiple values, I create a list of options.

A = {} file = docx.Document (". / Cryptography contest exercise .docx") for l in range (1meme61): B = [] for idx Para in enumerate (file.illustrography [6 * lvl]): if idx==0: key=para.text elif idx==1: v1=para.text b.append (v1) elif idx==2: v2=para.text b.append (v2) elif idx==3: v3=para.text b.append (v3) elif idx==4 : v4=para.text b.append (v4) elif idx==5: v5=para.text b.append (v5) a.update ({key:b})

The enumerate () function is used to combine a traverable data object (such as a list, tuple, or string) into an index sequence, listing both the data and the data subscript, which is commonly used in the for loop. (Baidu got, to put it bluntly, give each line a line number)

2. Convert dictionaries to excl files

(1) first create an excl file

Python also provides a module for processing tables, xlwt, which can be downloaded directly.

Sudo pip3 install xlwt

Create a table

Book = xlwt.Workbook () # create the excl file sheet = book.add_sheet ('sheet1') # create a table title = [' topic', 'Achilles Magistrate'] # each column heading for col in range (len (title)): # add the headings to the table one by one sheet.write (0Cocol) Title [col]) row=1 # sets the line number

(2) transfer from serious to excl

When everything is ready, the next step is to add the contents of the dictionary to the created excl one by one to OK

For k in a: data=a [k] data.insert (0Magnek) # add the sequence number for index in range (len (data)) in the first column: # write each line sheet.write (row,index,data [index]) row + = 1book.save ('TimuTest.xls') # Save excl file name 3, execute the command to get the desired excl table

Python3 docxToexcl.py

4. The whole script code #! / usr/bin/python3#coding:utf-8import docximport reimport jsonimport xlwta= {} file = docx.Document (". / Cryptography contest exercise .docx") for l in range (1Magne 61): B = [] for idx Para in enumerate (file.illustrography [6 * lvl]): if idx==0: key=para.text elif idx==1: v1=para.text b.append (v1) elif idx==2: v2=para.text b.append (v2) elif idx==3: v3=para.text b.append (v3) elif idx==4 : v4=para.text b.append (v4) elif idx==5: v5=para.text b.append (v5) a.update ({key:b}) book = xlwt.Workbook () sheet = book.add_sheet ('sheet1') title = [' topic' For col in range (len (title)): sheet.write (0LECHING [col]) row=1for k in a: data=a [k] data.insert (0PMK) for index in range (len (data)): sheet.write (row,index,data [index]) row + = 1book.save ('test.xls') summary:

In fact, I am not satisfied with writing this script, because when I write it, I only consider the format of the word document I am testing. Since it is in batch, the format of the document is not necessarily the same. So it is difficult to convert a document in another format into the desired excl table. The biggest problem is that if there is a blank line in the document, the script will still count in, and if every six lines loop in turn, the result will be different. The script needs to be improved, and the way to feel better now is to use the regular matching option. Post it when you have a chance to write it!

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

Network Security

Wechat

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

12
Report