我有一个 pdf 文件,正在使用 pymupdf 使用以下语法阅读。
import fitz # this is pymupdf
with fitz.open('file.pdf') as doc:
text = ""
for page in doc:
text += page.getText()
有没有办法在阅读时忽略页眉和页脚?
我尝试将 pdf 转换为 docx,因为删除标题更容易,但是当我将其转换为 docx 时,我正在处理的 pdf 文件会重新格式化。
pymupdf 在读取过程中有什么方法可以做到这一点吗?
文档中有一个页面专门讨论此问题。
来源:https://github.com/pymupdf/PyMuPDF-Utilities/tree/master/textbox-extraction#2-pageget_textboxrect
适用于大多数 pdf 库的通用解决方案是
正如官方文档所说,你可以使用
clip
参数来做到这一点:
doc = fitz.open(fname)
page = doc[0]
rect = page.rect
height = 50
clip = fitz.Rect(0, height, rect.width, rect.height-height)
text = page.get_text(clip=clip)