pdf->txt
PP <[email protected]> reply-to [email protected] to python-cn <[email protected]> date Fri, May 15, 2009 at 01:49
如何用python将pdf文档转换成Txt文本?有没有相关的类库?google了很多内容,都是将文件转换成pdf的,没有反过来的吗?
Ubuntu
Jiahua Huang <[email protected]> reply-to [email protected] to [email protected] date Fri, May 15, 2009 at 11:22
在 Ubuntu 下, xpdf-utils 和 poppler-utils 都带有 pdftotext,
但是 xpdf-utils 带的 pdftotext 对中文支持不如 poppler-utils 带的 pdftotext。
所以 Ubuntu 推荐用 poppler-utils 附带的 pdftotext, 默认就中文没问题。
Win32
pong Chong <[email protected]> reply-to [email protected] to [email protected] date Sat, May 16, 2009 at 05:31
今天研究了下win32下的xpdf转换中文的pdf,发现还是挺方便的,总结了一下,拿给大家分享,也可以访问我的博客博客
我要转换的pdf是中文的,而且有很多。也想写一个脚本来一次性解决,但是马上我就遇到了问题。当使用命令:
pdftotext -layout -nopgbrk 1.pdf
转换的时候,发现转换的并不成功,输出的txt文本中会有乱码出现,而且有的文本还被截断了。于是我又到 官方网站去下载了中文的 Language support以及 中文字体支持,按照chinese-simplified目录下的README老老实实的配置xpdfrc文件。其中README中提到:
Place all of these files in a directory, typically: Unix - /usr/local/share/xpdf/chinese-simplified Win32 - C:\Program Files\xpdf\chinese-simplified Add the contents of the "add-to-xpdfrc" file to your system-wide xpdfrc config file, which is typically: Unix - /usr/local/etc/xpdfrc Win32 - C:\Program Files\xpdf\xpdfrc
在chinese-simplified目录中找到了add-to-xpdfrc,修改里面的路径为win32相对路径(我的ttf字体支持存放在xpdf根目录下):
#----- begin Chinese Simplified support package (2004-jul-27) cidToUnicode Adobe-GB1 chinese-simplified\Adobe-GB1.cidToUnicode unicodeMap ISO-2022-CN chinese-simplified\ISO-2022-CN.unicodeMap unicodeMap EUC-CN chinese-simplified\EUC-CN.unicodeMap unicodeMap GBK chinese-simplified\GBK.unicodeMap cMapDir Adobe-GB1 chinese-simplified\CMap toUnicodeDir chinese-simplified\CMap displayCIDFontTT Adobe-GB1 gkai00mp.ttf #----- end Chinese Simplified support package
这样,配置就完成了,在xpdf目录下新建一个xpdfrc文件,注意没有扩展名!将上面的文本拷贝进去。再执行
pdftotext -layout -nopgbrk 1.pdf
文本完美转换。 参数说明,
- -layout:保持原有版面
- -nopgbrk:不插入分页符号
剩下的工作就简单了,只需遍历目录下的pdf文档顺序生成就OK了。改天写好代码补上来。
- 文中资源下载
xpdf for win32:xpdf-3.02pl3-win32.zip
xpdf 中文支持库:xpdf-chinese-simplified.tar.gz
xpdf 中文支持字体:gkai00mp.ttf.gz
反馈
创建 by -- ZoomQuiet [2009-05-16 03:10:14]