PDFの文字列をテキストに
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  | 
						from pdfminer.pdfinterp import PDFResourceManager from pdfminer.converter import TextConverter from pdfminer.pdfinterp import PDFPageInterpreter from pdfminer.pdfpage import PDFPage from pdfminer.layout import LAParams from io import StringIO # 標準組込み関数open()でモード指定をbinaryでFileオブジェクトを取得 fp = open("files/Training-Manual-for-Harmonized-Tariff-Schedule-Hts-Classification.pdf", 'rb') # 出力先をPythonコンソールするためにIOストリームを取得 outfp = StringIO() # 各種テキスト抽出に必要なPdfminer.sixのオブジェクトを取得する処理 rmgr = PDFResourceManager() # PDFResourceManagerオブジェクトの取得 lprms = LAParams()          # LAParamsオブジェクトの取得 device = TextConverter(rmgr, outfp, laparams=lprms)    # TextConverterオブジェクトの取得 iprtr = PDFPageInterpreter(rmgr, device) # PDFPageInterpreterオブジェクトの取得 # PDFファイルから1ページずつ解析(テキスト抽出)処理する for page in PDFPage.get_pages(fp):     iprtr.process_page(page) text = outfp.getvalue()  # Pythonコンソールへの出力内容を取得 outfp.close()  # I/Oストリームを閉じる device.close() # TextConverterオブジェクトの解放 fp.close()     #  Fileストリームを閉じる print(text)  # Jupyterの出力ボックスに表示する  | 
					
PDFの表をエクセルに変換
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  | 
						# Import Module  import os import tabula import pandas as pd filename= "files/Seniority List 2018 19.pdf" #pdfのページ数を取得 amount = (len(tabula.read_pdf(filename, pages = 'all'))) #pdfのページ数だけまわす for i in range(amount):     df = tabula.read_pdf(filename, pages = 'all')[i]     ### Convert into Excel File     df.to_excel('files/excel' + str(i) + '.xlsx') #アウトプットされたファイルの名前をリストに入れる     filename= "excel" filelist = os.listdir("files") filelist = [f for f in filelist if filename in f] # エクセルを一つずつpandasデータとして取得 each_file = [] for file in range(len(filelist)):     #エクセルファイルの数だけforを回し、それぞれをリスト変数に入れていく     each_file.append(pd.read_excel('files/excel' + str(file) + '.xlsx', header=None)) #リストを結合 df = pd.concat(each_file) df.to_excel('files/total.xlsx', index=False)  | 
					
PDFの画像を抽出
| 
					 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44  | 
						# PRG1:ライブラリ設定 import fitz import os # PRG2:画像の保存先フォルダを設定 filename = 'files/dai1.pdf' dir_name = filename.split('.')[0] img_dir = os.path.join(os.getcwd(),dir_name)  if os.path.isdir(img_dir) == False:     os.mkdir(img_dir) # PRG3:PDFファイルを読み込む doc = fitz.open(filename) # PRG4:画像情報を格納するリストを作成 images = [] # PRG5:1ページずつ画像データを取得 for page in range(len(doc)):     images.append(doc[page].get_images()) # PRG6:ページ内の画像情報を順番に処理 for pageNo, image in enumerate(images):     # PRG7:ページ内の画像情報を処理する     if image != []:         for i in range(len(image)):             # PRG8:画像情報の取得             xref = image[i][0]             smask = image[i][1]             if image[i][8] == 'FlateDecode':                 ext = 'png'             elif image[i][8] == 'DCTDecode':                 ext = 'jpeg'             # PRG9:マスク情報の取得と画像の再構築             pix = fitz.Pixmap(doc.extract_image(xref)["image"])             if smask > 0:                 mask = fitz.Pixmap(doc.extract_image(smask)["image"])                 pix = fitz.Pixmap(pix, 0)                  pix = fitz.Pixmap(pix, mask)             # PRG10:画像を保存             img_name = os.path.join(img_dir, f'image{pageNo+1}_{i}.{ext}')             pix.save(img_name)  | 
					
コメントを残す