爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 14886|回复: 5

[经验总结] pdf表格转csv(camelot工具的安装)

[复制链接]

新浪微博达人勋

发表于 2019-1-12 19:31:47 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
本帖最后由 葫芦爷儿 于 2019-1-12 19:59 编辑

今天,一个同学有许多pdf的表格需要提取成excel。直接复制出来是格式是乱的,于是我尝试找工具来解决。
最终的思路是这样的 1 安装camelot工具  2 读取pdf导出csv。

其中我是在macOS 10.12上做的,在安装camelot遇到一些问题,在此记录
1 macos的JDK安装  
参考网址 https://jingyan.baidu.com/article/7f766daffd99354101e1d095.html

2 安装好后 安装camelot
参考网址 https://github.com/socialcopsdev/camelot

2.1 安装 opencv
pip或conda都可以
pip install opencv
conda  install opencv

2.2 安装依赖环境 ghostscript
这里,我尝试了很多办法,最终是手动安装,然后修改程序
  • 下载代码  根据自己电脑下载,比如 Ghostscript 9.26 for Linux x86 (64 bit)   http://ghostscript.com/download/
  • 下载工具包  下载 Ghostscript 9.26 Source
  • 根据手册,将解压后的文件放到 Ghostscript 9.26 Source目录下
  • ./configure  --prefix=/usr/local/share/ghostscript/9.26/   这里CC= CPP=建议参考(下面各系统安装 ghostscript参考网址的说明)
  • make so

注意,brew install的版本过低,之前的url路径不存在了,会出现安装失败
安装里提到了 "The only difference compared to other Unix-like systems is that on OS X the resulting shared library is created with the ".dylib" file name extension, instead of the more usual ".so"."
macos上是.dylib,这在2.3步是要用到的。

参考网址:https://camelot-py.readthedocs.io/en/master/user/install-deps.html
各系统安装 ghostscript参考网址:https://www.ghostscript.com/doc/9.22/Make.htm
参考 ghostscript官网:https://www.ghostscript.com


2.3 修改代码,制定路径
由于报错文件是
  1.   File "/Users/hulu/anaconda3/envs/pdf/lib/python3.6/site-packages/camelot/ext/ghostscript/_gsprint.py", line 240, in <module>
  2.     raise RuntimeError('Please make sure that Ghostscript is installed')
  3. RuntimeError: Please make sure that Ghostscript is installed
复制代码


找到 libgs.dylib 的路径。
修改文件_gsprint.py,将报错位置进行修改
换成制定路径,

  1. #libgs = ctypes.util.find_library('gs')
  2. libgs = '/usr/local/share/ghostscript/9.26/bin/libgs.dylib'
复制代码


参考:https://www.ghostscript.com/doc/9.22/Make.htm



3 调用程序,转换pdf
例如
  1. import camelot
  2. tables = camelot.read_pdf('{0}.pdf'.format(file_name), pages='all')
  3. tables.export('{0}.csv'.format(file_name), f='csv', compress=True
复制代码


具体使用看git上的的例子
以上是安装使用的步骤!!!!
=================== 分 割 线  ===========================

下面是报错的尝试!!!! 顺利的话,就别看了!!!!
报错记录:
a 报错1
No module named 'cv2'
参考2.1 安装opencv

b 报错2
  1.   File "/Users/yaochenyang/anaconda3/envs/pdf/lib/python3.6/site-packages/camelot/ext/ghostscript/_gsprint.py", line 240, in <module>
  2.     raise RuntimeError('Please make sure that Ghostscript is installed')
  3. RuntimeError: Please make sure that Ghostscript is installed
复制代码


刚开始我尝试了用conda install,但是conda安装Ghostscript会把opencv降低到3.4.1,而opencv-python>=3.4.2.17
  1.     blas:      1.1-openblas                         conda-forge --> 1.0-mkl                https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  2.     hdf5:      1.10.4-nompi_h5598ddc_1105           conda-forge --> 1.10.2-hc401514_3      conda-forge                                            
  3.     libnetcdf: 4.6.2-h2c3f975_1                     conda-forge --> 4.6.1-h34af652_200     conda-forge                                            
  4.     netcdf4:   1.4.2-py36hf4e5497_1                 conda-forge --> 1.4.1-py36ha292673_200 conda-forge                                            
  5.     numpy:     1.15.4-py36_blas_openblashb06ca3d_0  conda-forge [blas_openblas] --> 1.14.2-py36ha9ae307_0  https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
  6.     opencv:    3.4.4-py36_blas_openblash21f6aec_203 conda-forge [blas_openblas] --> 3.4.1-py36h6fd60c2_1   
复制代码



参考  https://stackoverflow.com/questions/17653569/unable-to-load-library-gs-libgs-so-cannot-open-shared-object-file-no-such-f
参考:https://github.com/socialcopsdev/camelot/issues/184

下面是我其中的尝试
b1  
pip install ghostscript https://pypi.org/project/ghostscript/
conda brew
mac上安装 https://pages.uoregon.edu/koch/
然而并没有libgs.so文件。

b2
安装到本地,然后修改程序制定路径,最终采用了第二条建议
参考网址:https://stackoverflow.com/questions/6848903/python-ghostscript-runtimeerror-can-not-find-ghostscript-library-libgs
但此时没有安好ghostscript ,仍然在尝试

b3
因为发现缺少的是libgs,于是直接发现有 pip install libgs
但是因为这还是没有 libgs.os文件,是我还不太清楚一些库安装好了,也是没用。

b4
找到linux上安装的方法,因为macos相似,就看了看
参考网址:http://www.linuxfromscratch.org/blfs/view/8.3/pst/gs.html

b5  顺的b4
找到了类似的安装,比b4简洁一些
参考网址:https://stackoverflow.com/questions/17653569/unable-to-load-library-gs-libgs-so-cannot-open-shared-object-file-no-such-f/22721446


b6 最后回到了2.3步骤的方法


------------------------------------------------------------------------------------------------------------------------------------------------------------------------
下面是一个简单的例子,其实git上的test里也有,安装成功后可以尝试。
数据和程序放到了测试例子.zip
数据是两份pdf,均来自网络
调用函数
  1. #!usr/bin/env python
  2. #-*- coding:utf-8 _*-
  3. """
  4. @author:葫芦爷儿
  5. @file: ex_camelot.py
  6. @time: 2019-01-12 12:13
  7. """
  8. import camelot

  9. def pdf2scv(file_name):
  10.     file_url = file_name
  11.     tables = camelot.read_pdf('{0}.pdf'.format(file_url), pages='all')
  12.     tables.export('{0}.csv'.format(file_url), f='csv', compress=True)


  13. if __name__=="__main__":
  14.     for index in range(2013, 2015):
  15.         file_name = 'statistics{:}'.format(index)
  16.         pdf2scv(file_name)
复制代码





测试例子.zip

530.33 KB, 下载次数: 3, 下载积分: 金钱 -5

密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2019-1-13 09:26:37 | 显示全部楼层
感谢楼主分享{:eb502:}
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2019-1-13 11:16:23 | 显示全部楼层
感谢我的同门在windows上试了一下,很顺利。下面是他的记录

一、        安装python camelot模块
Win7(系统为win7)上直接
  1. conda install -c conda-forge camelot-py
复制代码

参考网址:https://github.com/socialcopsdev/camelot
二、        安装ghostscript脚本语言
Windows上安装直接下载运行脚本,点击运行安装即可。
下载网址:https://www.ghostscript.com/download/gsdnld.html
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2019-1-13 12:35:55 | 显示全部楼层
{:5_213:}
密码修改失败请联系微信:mofangbao
回复

使用道具 举报

新浪微博达人勋

发表于 2019-1-15 00:46:21 | 显示全部楼层
数量不多的话直接可以用OCR识别再复制到表格就可以吧,现在的识别软件对电脑字体识别率很高。
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2019-1-15 11:47:28 | 显示全部楼层
不错 python用炉火纯青
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表