我想解析一个pdf文件,为此我正在使用pdftotext
实用程序将pdf文件转换为文本文件,现在我想从文本文件中删除页码、页眉和页脚。
我正在使用以下语法转换pdf文件:
pdftotext -layout input.pdf output.txt
有人能帮我解决这个问题吗?
我想解析一个pdf文件,为此我正在使用pdftotext
实用程序将pdf文件转换为文本文件,现在我想从文本文件中删除页码、页眉和页脚。
我正在使用以下语法转换pdf文件:
pdftotext -layout input.pdf output.txt
有人能帮我解决这个问题吗?
你需要使用参数-H -W -y -x 进行裁剪,至少需要使用 -H -W -y 参数。
例如:
pdftotext -y 80 -H 650 -W 1000 -nopgbrk -eol unix example.pdf
-y 80 -> crop 80 pixels after the top of file (remove header);
-H 650 -> crop 650 pixels after the -y has cropped (remove footer);
-W 1000 -> hight value to crop nothing (need especify something);
您需要调整每个PDF的-y和-H,有时需要减小-y并增加-H以适应页眉和页脚;
搜索显示页面编号或页眉、页脚的模式!例如,当我使用pdftotext将pdf文件转换为文本时,我意识到数字页面在文本中是独立存在的,因此我使用正则表达式进行替换,如下所示:
for root, dirs, files in os.walk(src, topdown=False):
for name in files:
if name.endswith('.txt'):
with open(os.path.join(root, name), "r") as fin:
data = fin.read()
new_text = re.sub(r'\n\d+\n\s','',data,re.DOTALL)
pdfplumber
的解决方案,这是一个更好的实用工具,并允许对页面进行完全控制。 - Raghav Gupta