利用 python-docx 处理word文档

最近背单词时整理了一份词根词缀,用word记录的,但是记的很紊乱,于是想进行一次排序。搜索发现有个叫 python-docx 的轮子,粗略的翻了下文档,用了几个基本的api完成了这次的需求。
倒是因为python3的sort和cmp的变化折腾了很久… python3的sort中的cmp参数删除了,如果要用旧版cmp自定义排序的话要利用functools 库的 cmp_to_key。

节选了其中一部分,作为参考格式,按照前缀- 后缀- 其他词根的顺序来排序:

ab- = away, off; intensive	远离;脱离;加强语气
ad- (as-) (al-) = to, towards, intensive	朝着;靠近;加强语气
co- (com-) = together; intensive	一起;全;加强语气
de- = not; removal; down	表否定;除去;向下
-able = 构成adj;能…的,易…的
-age = 构成名词;表状态和行为等
-fy = 构成动词;使…
-ic = 构成形容词
-ure = 构成抽象名词
bev = to drink	喝
bulg = leather bag	皮包
ced = cess = to go	走,去
cis = to cut, to kill	砍,杀
concil = meeting	会议
crit = to judge	评论,判断

代码实现:

from docx import Document
from functools import cmp_to_key


# 排序方法
def cmp(a, b):
    a = a.split()[0]
    b = b.split()[0]
    # 前缀
    if a[-1] == '-' and b[-1] != '-':
        return -1
    if a[-1] != '-' and b[-1] == '-':
        return 1
    # 后缀
    if a[0] == '-' and b[0] != '-':
        return -1
    if a[0] != '-' and b[0] == '-':
        return 1
    # 其他词根
    if a < b:
        return -1
    if a > b:
        return 1
    return 0


src = "C:\\Users\\11018\\Desktop\\词根词缀.docx"
file = Document(src)
strList = []

for i in file.paragraphs:
    strList.append(i.text)

strList.sort(key=cmp_to_key(cmp))

for i, x in enumerate(strList):
    print(x)
    file.paragraphs[i].text = x

file.save(src)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值