python docx runs_在Python中解析docx文件

我试图从多个docx文件中读取标题。令人恼火的是,这些标题没有可识别的段落样式。所有段落都有“正常”段落样式,所以我使用正则表达式。标题采用粗体格式,结构如下:

A.猫

B.狗

C.猪

D.福克斯

如果一个文件中有超过26个标题,那么这些标题前面会加上“AA.”、“BB.”等

我有下面的代码,除了前面有“D”的标题外,哪种代码可以打印两次。

[猫,狗,猪,狐狸,狐狸]import os

from docx import Document

import re

directory = input("Copy and paste the location of the files.\n").lower()

for file in os.listdir(directory):

document = Document(directory+file)

head1s = []

for paragraph in document.paragraphs:

heading = re.match(r'^[A-Z]+[.]\s', paragraph.text)

for run in paragraph.runs:

if run.bold:

if heading:

head1 = paragraph.text

head1 = head1.split('.')[1]

head1s.append(head1)

print(head1s)

有人能告诉我是不是代码有问题导致了这种情况的发生?据我所知,Word文件中这些特定标题的格式或结构没有什么独特之处。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值