前提・実現したいこと
PDFファイルに記載された文字に下線が引かれているデータを抽出したい。引かれているのといないので区別したい。
※PDF編集で入れた?罫線のようです。どのように記載したかは不明。
pythonでPDFの文字解析を行い、そのデータに下線が引かれているか確認し区別するアプリを作っています。
pythonモジュールpdfminer3kで解析を行いました。
該当のソースコード
Anaconda prompt Scripts> pdf2txt.py data.pdf > text2.csv Scripts> py >>> import csv >>> example_file = open('text2.csv') >>> example_reader = csv.reader(example_file) >>> example_data = list(example_reader) >>> example_data[5]
csvの5行目にある住所に下線がPDFでは引かれていたのですが、抽出すると以下の様にただの文言になっていました。
発生している問題
\u3000大阪市倍野区\u3000\u3000\u3000\u3000\u3000
試したこと
エクセルで文字に罫線を引きPDF出力
上記解析を試しました。
’\x0c’という罫線情報らしきものは抽出できましたが、実際の一番上の画像のような罫線下線とは違いますので意味がないと思い、途方に暮れています。
補足情報(FW/ツールのバージョンなど)
win10
python3.6.0
Anaconda3
anaconda-script.py Command line client (version 1.6.0)
もし何かアドバイスいい案などご掲示頂ければ幸いです。
よろしくお願い致します。
###pdf2txt.py
python
1#!C:\ProgramData\Anaconda3\python.exe 2import sys 3import io 4import getopt 5 6from pdfminer.pdfinterp import PDFResourceManager, process_pdf 7from pdfminer.pdfdevice import TagExtractor 8from pdfminer.converter import XMLConverter, HTMLConverter, TextConverter 9from pdfminer.layout import LAParams 10from pdfminer.utils import set_debug_logging 11 12def main(argv): 13 def usage(): 14 print(('usage: %s [-d] [-p pagenos] [-m maxpages] [-P password] [-o output] [-C] ' 15 '[-n] [-A] [-V] [-M char_margin] [-L line_margin] [-W word_margin] [-F boxes_flow] ' 16 '[-Y layout_mode] [-O output_dir] [-t text|html|xml|tag] [-c codec] [-s scale] file ...' % argv[0])) 17 return 100 18 try: 19 (opts, args) = getopt.getopt(argv[1:], 'dp:m:P:o:CnAVM:L:W:F:Y:O:t:c:s:') 20 except getopt.GetoptError: 21 return usage() 22 if not args: return usage() 23 debug = False 24 # input option 25 password = '' 26 pagenos = set() 27 maxpages = 0 28 # output option 29 outfile = None 30 outtype = None 31 outdir = None 32 layoutmode = 'normal' 33 codec = 'utf-8' 34 pageno = 1 35 scale = 1 36 caching = True 37 showpageno = True 38 laparams = LAParams() 39 for (k, v) in opts: 40 if k == '-d': debug = True 41 elif k == '-p': pagenos.update( int(x)-1 for x in v.split(',') ) 42 elif k == '-m': maxpages = int(v) 43 elif k == '-P': password = v 44 elif k == '-o': outfile = v 45 elif k == '-C': caching = False 46 elif k == '-n': laparams = None 47 elif k == '-A': laparams.all_texts = True 48 elif k == '-V': laparams.detect_vertical = True 49 elif k == '-M': laparams.char_margin = float(v) 50 elif k == '-L': laparams.line_margin = float(v) 51 elif k == '-W': laparams.word_margin = float(v) 52 elif k == '-F': laparams.boxes_flow = float(v) 53 elif k == '-Y': layoutmode = v 54 elif k == '-O': outdir = v 55 elif k == '-t': outtype = v 56 elif k == '-c': codec = v 57 elif k == '-s': scale = float(v) 58 59 if debug: 60 set_debug_logging() 61 rsrcmgr = PDFResourceManager(caching=caching) 62 if not outtype: 63 outtype = 'text' 64 if outfile: 65 if outfile.endswith('.htm') or outfile.endswith('.html'): 66 outtype = 'html' 67 elif outfile.endswith('.xml'): 68 outtype = 'xml' 69 elif outfile.endswith('.tag'): 70 outtype = 'tag' 71 if outfile: 72 outfp = io.open(outfile, 'wt', encoding=codec, errors='ignore') 73 close_outfp = True 74 else: 75 outfp = sys.stdout 76 close_outfp = False 77 if outtype == 'text': 78 device = TextConverter(rsrcmgr, outfp, laparams=laparams) 79 elif outtype == 'xml': 80 device = XMLConverter(rsrcmgr, outfp, laparams=laparams, outdir=outdir) 81 elif outtype == 'html': 82 device = HTMLConverter(rsrcmgr, outfp, scale=scale, layoutmode=layoutmode, 83 laparams=laparams, outdir=outdir, debug=debug) 84 elif outtype == 'tag': 85 device = TagExtractor(rsrcmgr, outfp) 86 else: 87 return usage() 88 for fname in args: 89 fp = io.open(fname, 'rb') 90 process_pdf(rsrcmgr, device, fp, pagenos, maxpages=maxpages, password=password, 91 caching=caching, check_extractable=True) 92 fp.close() 93 device.close() 94 if close_outfp: 95 outfp.close() 96 97if __name__ == '__main__': 98 sys.exit(main(sys.argv)) 99
回答2件
あなたの回答
tips
プレビュー