从给定坐标提取 PDF 文本 [英] PDF text extraction from given coordinates

查看:27
本文介绍了从给定坐标提取 PDF 文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Ghostscript 从 PDF 的一部分(使用坐标)中提取文本.

I would like to extract text from a portion (using coordinates) of PDF using Ghostscript.

有人可以帮我吗?

推荐答案

是的,使用 Ghostscript,您可以从 PDF 中提取文本.但是不,它不是完成这项工作的最佳工具.不,你不能在部分"中做到这一点.(单页的一部分).您可以做什么:仅提取特定范围页面的文本.

Yes, with Ghostscript, you can extract text from PDFs. But no, it is not the best tool for the job. And no, you cannot do it in "portions" (parts of single pages). What you can do: extract the text of a certain range of pages only.

 gs 
   -dBATCH 
   -dNOPAUSE 
   -sDEVICE=txtwrite 
   -dFirstPage=3 
   -dLastPage=5 
   -sOutputFile=- 
   /path/to/your/pdf

这会将第 3-5 页中包含的所有文本输出到标准输出.如果要输出到文本文件,请使用

This will output all text contained on pages 3-5 to stdout. If you want output to a text file, use

   -sOutputFile=textfilename.txt


gs 更新:


gs Update:

Ghostscript 的最新版本在 txtwrite 设备和错误修复方面进行了重大改进.请参阅 最近的 Ghostscript 更改日志(在该页面上搜索 txtwrite)了解详情.

Recent versions of Ghostscript have seen major improvements in the txtwrite device and bug fixes. See recent Ghostscript changelogs (search for txtwrite on that page) for details.

这个要求你从Ghostscript Git 源代码库.您必须将 PDF 转换为 PostScript,然后在 PS 文件上运行此命令:

This one requires you to download the latest version of the file ps2ascii.ps from the Ghostscript Git source code repository. You'd have to convert your PDF to PostScript, then run this command on the PS file:

gs 
  -q 
  -dNODISPLAY 
  -P- 
  -dSAFER 
  -dDELAYBIND 
  -dWRITESYSTEMDICT 
  -dSIMPLE 
   /path/to/ps2ascii.ps 
   input.ps 
  -c quit

如果未定义 -dSIMPLE 参数,则每个输出行都包含一些关于使用的字体和字体大小的纯文本内容之外的附加信息.

If the -dSIMPLE parameter is not defined, each output line contains some additional info beyond the pure text content about fonts and fontsize used.

如果您用 -dCOMPLEX 替换该参数,您将获得有关所用颜色和图像的其他信息.

If you replace that parameter by -dCOMPLEX, you'll get additional infos about colors and images used.

阅读 ps2ascii.ps 中的注释以了解有关此实用程序的更多信息.使用起来不舒服,但对我来说它在我需要的大多数情况下都有效......

Read the comments inside the ps2ascii.ps to learn more about this utility. It's not comfortable to use, but for me it worked in most cases I needed it....

一种更舒适的文本提取方式:使用 pdftotext(适用于 Windows 以及 Linux/Unix 或 Mac OS X).此实用程序基于 Poppler 或 XPDF.这是您可以尝试的命令:

A more comfortable way to do text extraction: use pdftotext (available for Windows as well as Linux/Unix or Mac OS X). This utility is based either on Poppler or on XPDF. This is a command you could try:

 pdftotext 
   -f 13 
   -l 17 
   -layout 
   -opw supersecret 
   -upw secret 
   -eol unix 
   -nopgbrk 
   /path/to/your/pdf
   - |less

这将显示页面范围 13(f第一页)到 17(l最后一页),保留双密码保护的命名 PDF 文件的布局(使用用户和所有者密码 secretsupersecret),遵循 Unix EOL 约定,但不在 PDF 页面之间插入分页符,通过 less...

This will display the page range 13 (first page) to 17 (last page), preserve the layout of a double-password protected named PDF file (using user and owner passwords secret and supersecret), with Unix EOL convention, but without inserting pagebreaks between PDF pages, piped through less...

pdftotext -h 显示所有可用的命令行选项.

pdftotext -h displays all available commandline options.

当然,这两种工具都只适用于 PDF 的文本部分(如果有的话).哦,数学公式也不会很好用... ;-)

Of course, both tools only work for the text parts of PDFs (if they have any). Oh, and mathematical formula also won't work too well... ;-)

pdftotext 更新:

最近版本的 Poppler pdftotext 现在可以选择提取PDF 的一部分(使用坐标)" 页面,就像 OP 要求的那样.参数为:

Recent versions of Poppler's pdftotext have now options to extract "a portion (using coordinates) of PDF" pages, like the OP asked for. The parameters are:

  • -x : 裁剪区域左上角的 x 坐标
  • -y : 裁剪区域左上角的 y 坐标
  • -W :裁剪区域的宽度(以像素为单位)(默认为 0)
  • -H :裁剪区域的高度(以像素为单位)(默认为 0)

最好,如果与 -layout 参数一起使用.

Best, if used with the -layout parameter.

跨平台、开源的 MuPDF 应用程序(由开发 Ghostscript 的同一家公司制作)捆绑了一个命令行工具,mutool.要使用此工具从 PDF 中提取文本,请使用:

The cross-platform, open source MuPDF application (made by the same company that also develops Ghostscript) has bundled a command line tool, mutool. To extract text from a PDF with this tool, use:

mutool draw -F txt the.pdf

将提取的文本发送到 .使用 -o filename.txt 将其写入文件.

will emit the extracted text to <stdout>. Use -o filename.txt to write it into a file.

TET,来自 pdflib 系列产品可以在 PDF 文件(以及更多)中找到文本内容的 xy 坐标.TET 有一个命令行界面,它是我所知道的所有文本提取工具中最强大的.(它甚至可以处理连字...)引自他们的网站:

TET, the Text Extraction Toolkit from the pdflib family of products can find the x-y-coordinate of text content in a PDF file (and much more). TET has a commandline interface, and it's the most powerful of all text extraction tools I'm aware of. (It can even handle ligatures...) Quote from their website:

几何
TET 为文本提供精确的度量,例如页面上的位置、字形宽度和文本方向.页面上的特定区域可以排除或包含在文本提取中,例如忽略页眉和页脚或边距.

根据我的经验,虽然它没有你能想象到的最直接的 CLI 界面:在你习惯了它之后,它会做它承诺做的事情,对于你扔给它的大多数 PDF...

In my experience, while it's does not sport the most straight-forward CLI interface you can imagine: after you got used to it, it will do what it promises to do, for most PDFs you throw towards it...

还有更多选择:

  1. podofotxtextract(CLI 工具)来自 PoDoFo 项目(开源)
  2. calibre(通常是处理电子书的 GUI 程序,开源) 有一个命令行选项,可以从 PDF 中提取文本
  3. AbiWord(GUI 文字处理器,开源)可以导入PDF 并将其文件保存为 .txt:abiword --to=txt --to-name=output.txt input.pdf

这篇关于从给定坐标提取 PDF 文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆