吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4582|回复: 31
收起左侧

[Python 转载] 用python做的一个简易word转excel题库

  [复制链接]
baitianbait 发表于 2022-5-12 18:03
最近因为一个题库小程序的需要,在做开发的时候,小程序做好后,题库需要输入数据库,就用python做了一个word转excel的小案例;
运行结果是这样的: 1652349008(1).png
原版word图:
1652349602(1).png
下面附上代码:
import re
import xlrd
import pandas as pd
from docx import Document
from collections import OrderedDict
import collections
doc = Document("E:\睿丰凌通\建靠题库/1111.docx")
sections = doc.sections
paragraphs = doc.paragraphs

black_char = re.compile("\..*")  #题目匹配
title_rule = re.compile("\..*")  #题目匹配
option_rule1 = re.compile("\d")#选项匹配
option_rule = re.compile("[ABCDE]\.\w.*")#选项匹配
option_rule_search = re.compile("[ABCDE]\..*")#选项匹配
daan_rule_search = re.compile("【答案】.*")

question_type2data = OrderedDict()
title2options = OrderedDict()

for paragraph in doc.paragraphs[0:55]:
    # print(paragraph.text)
    titlerule = re.search(r'\..*', paragraph.text, re.M | re.I)
    optionrule = re.search(r'[ABCDE]\.\w.*', paragraph.text, re.M | re.I)
    if titlerule:
        line = titlerule.group()
        options = title2options.setdefault(line, [])
        # print("题目", title)
    elif optionrule:
        optionrule = re.findall(r'[ABCDE]\.\w*', optionrule.group(), re.M | re.I)
        # print("选项",optionrule)
        options.extend(optionrule)
        # print("选项",options)
        # title2options = question_type2data.setdefault(line, OrderedDict())
        # print(title2options.items())
        # print("题目", title2options)
    # title2options = question_type2data.setdefault(line,line)

result = []
# max_options_len = 0
# for title2options,options in title2options.items():
for title,options in title2options.items():
  # print(options)
  result.append([title,*options])
  print("题目", result)
    # options_len = len(options)
    # if options_len > max_options_len:
    #     max_options_len = options_len
df = pd.DataFrame(result, columns=[
            "题目", "选项A","选项B","选项C","选项D"])
        # 题型可以简化下,去掉选择两个字
# df['题型'] = df['题型'].str.replace("选择", "")
df.to_excel("result.xlsx", index=False)

免费评分

参与人数 1吾爱币 +1 热心值 +1 收起 理由
yjn866y + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| baitianbait 发表于 2022-7-27 15:47
wkdxz 发表于 2022-7-20 11:56
楼主能不能提供你的Word文件测试下,我试了其他文档 出现错误

在本贴提供
studentguo 发表于 2022-5-13 18:54
hlq0514 发表于 2022-5-13 21:46
ab123 发表于 2022-5-14 15:51
收藏了,稍加改动就能转换各种格式的题库了。
yks1985 发表于 2022-5-14 16:11
先收藏,日后有用了直接复制黏贴
浑浑噩噩的人呐 发表于 2022-5-19 12:32
大老厉害啊,但是不会python,头疼
cnljm 发表于 2022-7-12 00:38
请问这个可以做成可视化的吗?谢谢分享
yjn866y 发表于 2022-7-13 17:25
测试了一下,加个分
瓦匠 发表于 2022-7-13 22:17
学习一下。
gpff 发表于 2022-7-14 06:34
好东西,好东西
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-29 16:39

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表