python 代码段的标志_Pythondocx:在段落中标识分页符

这篇博客探讨了在使用Python docx库处理文档时,如何有效地识别和处理段落中的分页符。作者按段落迭代文档,将段落文本拆分成句子,并在句子中搜索错误。遇到错误时,代码会进行替换操作,并尝试获取替换发生时的分页符位置。问题在于,`run.page_break == WD_BREAK`似乎无法有效识别分页符。作者寻求解决如何在段落中识别分页符,特别是软分页符的方法。
摘要由CSDN通过智能技术生成

我按段落迭代文档,然后按.(带空格的点)将每个段落文本拆分成句子。我把段落文本分成句子In,这样做比在整个段落文本中搜索更有效。在

然后代码在句子的每个单词中搜索错误,从纠错数据库中提取错误。下面是一个简化代码:from docx.enum.text import WD_BREAK

for paragraph in document.paragraphs:

sentences = paragraph.text.split('. ')

for sentence in sentences:

words=sentence.split(' ')

for word in words:

for error in error_dictionary:

if error in word:

# (A) make simple replacement

word = word.replace(error, correction, 1)

# (B) alternative replacement based on runs

for run in paragraph.runs:

if error in run.text:

run.text = run.text.replace(error, correction, 1)

# here we may fetch page break attribute and knowing current number

# find out at what page the replacement has taken place

if run.page_break== WD_BREAK:

current_page_number +&

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值