2016-10-11 3 views
2

最近、私はpypdfocrをダウンロードしましたが、ドキュメンテーションにはライブラリとしてpypdfocrを呼び出す方法の例はありません。だから誰も私がただ一つのファイルを変換するために呼び出すのを助けることができますか?私はちょうどターミナルコマンドを見つけました:pypdfocr関数をpythonスクリプトで使用するにはどうすればいいですか?

$ pypdfocr filename.pdf 

答えて

1

ソースコードを探しているのなら、それは通常あなたのpythonインストールのsite-packageディレクトリの下にあります。さらに、IDE(Pycharm)を使用している場合は、ディレクトリとファイルを見つけるのに役立ちます。 https://github.com/virantha/pypdfocr/blob/master/pypdfocr/pypdfocr.py このファイルにはpppdfocrクラスタイプがあります。これは、再利用が可能で、おそらくコマンドラインの処理を行います。そのクラスで

、開発者に対しは、解析する引数の多くを入れている:

def get_options(self, argv): 
    """ 
     Parse the command-line options and set the following object properties: 
     :param argv: usually just sys.argv[1:] 
     :returns: Nothing 
     :ivar debug: Enable logging debug statements 
     :ivar verbose: Enable verbose logging 
     :ivar enable_filing: Whether to enable post-OCR filing of PDFs 
     :ivar pdf_filename: Filename for single conversion mode 
     :ivar watch_dir: Directory to watch for files to convert 
     :ivar config: Dict of the config file 
     :ivar watch: Whether folder watching mode is turned on 
     :ivar enable_evernote: Enable filing to evernote 
    """ 
    p = argparse.ArgumentParser(description = "Convert scanned PDFs into their OCR equivalent. Depends on GhostScript and Tesseract-OCR being installed.", 
      epilog = "PyPDFOCR version %s (Copyright 2013 Virantha Ekanayake)" % __version__, 
      ) 

    p.add_argument('-d', '--debug', action='store_true', 
     default=False, dest='debug', help='Turn on debugging') 

    p.add_argument('-v', '--verbose', action='store_true', 
     default=False, dest='verbose', help='Turn on verbose mode') 

    p.add_argument('-m', '--mail', action='store_true', 
     default=False, dest='mail', help='Send email after conversion') 

    p.add_argument('-l', '--lang', 
     default='eng', dest='lang', help='Language(default eng)') 


    p.add_argument('--preprocess', action='store_true', 
      default=False, dest='preprocess', help='Enable preprocessing. Not really useful now with improved Tesseract 3.04+') 

    p.add_argument('--skip-preprocess', action='store_true', 
      default=False, dest='skip_preprocess', help='DEPRECATED: always skips now.') 

    #--------- 
    # Single or watch mode 
    #-------- 
    single_or_watch_group = p.add_mutually_exclusive_group(required=True) 
    # Positional argument for single file conversion 
    single_or_watch_group.add_argument("pdf_filename", nargs="?", help="Scanned pdf file to OCR") 
    # Watch directory for watch mode 
    single_or_watch_group.add_argument('-w', '--watch', 
     dest='watch_dir', help='Watch given directory and run ocr automatically until terminated') 

    #----------- 
    # Filing options 
    #---------- 
    filing_group = p.add_argument_group(title="Filing optinos") 
    filing_group.add_argument('-f', '--file', action='store_true', 
     default=False, dest='enable_filing', help='Enable filing of converted PDFs') 
    #filing_group.add_argument('-c', '--config', type = argparse.FileType('r'), 
    filing_group.add_argument('-c', '--config', type = lambda x: open_file_with_timeout(p,x), 
     dest='configfile', help='Configuration file for defaults and PDF filing') 
    filing_group.add_argument('-e', '--evernote', action='store_true', 
     default=False, dest='enable_evernote', help='Enable filing to Evernote') 
    filing_group.add_argument('-n', action='store_true', 
     default=False, dest='match_using_filename', help='Use filename to match if contents did not match anything, before filing to default folder') 


    # Add flow option to single mode extract_images,preprocess,ocr,write 

    args = p.parse_args(argv) 

をあなたはそれがこのように、パーサのに渡されるこれらの引数のいずれかを使用できます。

import pypdfocr 

obj = pypdfocr.pypdfocr.pypdfocr() 
obj.get_options([]) # this makes it takes default, but you could add CLI option to it. Other option might be [-v] or [-d,-v] 
`----> 3 OBJ = pypdfocr.pypdfocr() 4 OBJを:

は、私はあなたが助けエリックのために:)

+0

おかげで!私はこのように実行しようとなった平均時間で理解してこのヘルプを願っています。 get_options([]) はAttributeError:「モジュール」オブジェクトが属性が「 – tumbleweed

+1

私の悪いpypdfocr''ない、私はあなたが3を必要とするという事実を見逃している: package_name.module_name.class_name() pypdfocr.pypdfocr.pypdfocr() –

+0

はこれがために失敗しました私は 'import pypdfocr'ステップで –

関連する問題