convert_pdf.py 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. import copy
  2. import inspect
  3. import io
  4. import logging
  5. import os
  6. import re
  7. import sys
  8. from bs4 import BeautifulSoup
  9. sys.path.append(os.path.dirname(__file__) + "/../")
  10. from pdfplumber import PDF
  11. from pdfplumber.table import TableFinder
  12. from pdfplumber.page import Page as pdfPage
  13. from format_convert.convert_tree import _Document, _Page, _Image, _Sentence, _Table
  14. import time
  15. import pdfminer
  16. import math
  17. from scipy.stats import linregress
  18. from matplotlib import pyplot as plt
  19. from shapely.geometry import LineString, Point
  20. from format_convert import timeout_decorator
  21. from PIL import Image
  22. from format_convert.convert_image import image_process
  23. from format_convert.convert_need_interface import from_ocr_interface, from_office_interface
  24. import traceback
  25. import cv2
  26. import PyPDF2
  27. from PyPDF2 import PdfFileReader, PdfFileWriter
  28. from pdfminer.pdfparser import PDFParser
  29. from pdfminer.pdfdocument import PDFDocument
  30. from pdfminer.pdfpage import PDFPage
  31. from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
  32. from pdfminer.converter import PDFPageAggregator
  33. from pdfminer.layout import LTTextBoxHorizontal, LAParams, LTFigure, LTImage, LTCurve, LTText, LTChar, LTRect, \
  34. LTTextBoxVertical, LTLine, LTTextContainer
  35. from format_convert.utils import judge_error_code, add_div, get_platform, get_html_p, string_similarity, LineTable, \
  36. get_logger, log, memory_decorator, draw_lines_plt, get_garble_code, line_is_cross
  37. import fitz
  38. from format_convert.wrapt_timeout_decorator import timeout
  39. @memory_decorator
  40. def pdf2Image(path, save_dir):
  41. log("into pdf2Image")
  42. try:
  43. try:
  44. doc = fitz.open(path)
  45. except Exception as e:
  46. log("pdf format error!")
  47. # print("pdf format error!", e)
  48. return [-3]
  49. # output_image_list = []
  50. output_image_dict = {}
  51. page_count = doc.page_count
  52. for page_no in range(page_count):
  53. # 限制pdf页数,只取前10页后10页
  54. if page_count > 20:
  55. if 10 <= page_no < page_count - 10:
  56. # log("pdf2Image: pdf pages count " + str(doc.page_count)
  57. # + ", only get 70 pages")
  58. continue
  59. try:
  60. page = doc.loadPage(page_no)
  61. output = save_dir + "_page" + str(page_no) + ".png"
  62. rotate = int(0)
  63. # 每个尺寸的缩放系数为1.3,这将为我们生成分辨率提高2.6的图像。
  64. # 此处若是不做设置,默认图片大小为:792X612, dpi=96
  65. # (1.33333333 --> 1056x816) (2 --> 1584x1224)
  66. # (1.183, 2.28 --> 1920x1080)
  67. zoom_x = 3.
  68. zoom_y = 3.
  69. # mat = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
  70. mat = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
  71. pix = page.getPixmap(matrix=mat, alpha=False)
  72. pix.writePNG(output)
  73. pdf_image = cv2.imread(output)
  74. print("pdf_image", page_no, pdf_image.shape)
  75. # output_image_list.append([page_no, output])
  76. output_image_dict[int(page_no)] = output
  77. except ValueError as e:
  78. traceback.print_exc()
  79. if str(e) == "page not in document":
  80. log("pdf2Image page not in document! continue..." + str(page_no))
  81. continue
  82. elif "encrypted" in str(e):
  83. log("pdf2Image document need password " + str(page_no))
  84. return [-7]
  85. except RuntimeError as e:
  86. if "cannot find page" in str(e):
  87. log("pdf2Image page {} not in document! continue... ".format(str(page_no)) + str(e))
  88. continue
  89. else:
  90. traceback.print_exc()
  91. return [-3]
  92. return [output_image_dict]
  93. except Exception as e:
  94. log("pdf2Image error!")
  95. print("pdf2Image", traceback.print_exc())
  96. return [-1]
  97. @timeout(10, timeout_exception=TimeoutError)
  98. def pdf_analyze(interpreter, page, device, page_no):
  99. log("into pdf_analyze")
  100. pdf_time = time.time()
  101. # print("pdf_analyze interpreter process...")
  102. interpreter.process_page(page)
  103. # print("pdf_analyze device get_result...")
  104. layout = device.get_result()
  105. log("pdf2text page " + str(page_no) + " read time " + str(time.time() - pdf_time))
  106. return layout
  107. @memory_decorator
  108. def pdf2text(path, unique_type_dir):
  109. log("into pdf2text")
  110. try:
  111. # pymupdf pdf to image
  112. save_dir = path.split(".")[-2] + "_" + path.split(".")[-1]
  113. output_image_dict = pdf2Image(path, save_dir)
  114. if judge_error_code(output_image_dict):
  115. return output_image_dict
  116. output_image_dict = output_image_dict[0]
  117. output_image_no_list = list(output_image_dict.keys())
  118. output_image_no_list.sort(key=lambda x: x)
  119. # 获取每页pdf提取的文字、表格的列数、轮廓点、是否含表格、页码
  120. # page_info_list = []
  121. page_info_dict = {}
  122. has_table_dict = {}
  123. no_table_dict = {}
  124. for page_no in output_image_no_list:
  125. img_path = output_image_dict.get(page_no)
  126. print("pdf page", page_no, "in total", output_image_no_list[-1])
  127. # 读不出来的跳过
  128. try:
  129. img = cv2.imread(img_path)
  130. img_size = img.shape
  131. except:
  132. log("pdf2text read image in page fail! continue...")
  133. continue
  134. # 每张图片处理
  135. text, column_list, outline_points, is_table = image_process(img, img_path, use_ocr=False)
  136. if judge_error_code(text):
  137. return text
  138. # page_info_list.append([text, column_list, outline_points, is_table,
  139. # page_no, img_size])
  140. page_info = [text, column_list, outline_points, is_table, img_size]
  141. page_info_dict[int(page_no)] = page_info
  142. # 包含table的和不包含table的
  143. if is_table:
  144. has_table_dict[int(page_no)] = page_info
  145. else:
  146. no_table_dict[int(page_no)] = page_info
  147. has_table_no_list = list(has_table_dict.keys())
  148. has_table_no_list.sort(key=lambda x: x)
  149. page_no_list = list(page_info_dict.keys())
  150. page_no_list.sort(key=lambda x: x)
  151. # 页码表格连接
  152. table_connect_list, connect_text_list = page_table_connect(has_table_dict)
  153. if judge_error_code(table_connect_list):
  154. return table_connect_list
  155. # 连接的页码
  156. table_connect_page_no_list = []
  157. for area in connect_text_list:
  158. table_connect_page_no_list.append(area[1])
  159. print("pdf2text table_connect_list", table_connect_list)
  160. print("connect_text_list", connect_text_list)
  161. # pdfminer 方式
  162. try:
  163. fp = open(path, 'rb')
  164. # 用文件对象创建一个PDF文档分析器
  165. parser = PDFParser(fp)
  166. # 创建一个PDF文档
  167. doc = PDFDocument(parser)
  168. # 连接分析器,与文档对象
  169. rsrcmgr = PDFResourceManager()
  170. device = PDFPageAggregator(rsrcmgr, laparams=LAParams())
  171. interpreter = PDFPageInterpreter(rsrcmgr, device)
  172. # 判断是否能读pdf
  173. for page in PDFPage.create_pages(doc):
  174. break
  175. except pdfminer.psparser.PSEOF as e:
  176. # pdfminer 读不了空白页的对象,直接使用pymupdf转换出的图片进行ocr识别
  177. log("pdf2text " + str(e) + " use ocr read pdf!")
  178. text_list = []
  179. for page_no in page_no_list:
  180. log("pdf2text ocr page_no " + str(page_no))
  181. page_info = page_info_dict.get(page_no)
  182. # 表格
  183. if page_info[3]:
  184. # 判断表格是否跨页连接
  185. area_no = 0
  186. jump_page = 0
  187. for area in table_connect_list:
  188. if page_no in area:
  189. # 只记录一次text
  190. if page_no == area[0]:
  191. image_text = connect_text_list[area_no][0]
  192. text_list.append([image_text, page_no, 0])
  193. jump_page = 1
  194. area_no += 1
  195. # 是连接页的跳过后面步骤
  196. if jump_page:
  197. continue
  198. # 直接取text
  199. image_text = page_info_dict.get(page_no)[0]
  200. text_list.append([image_text, page_no, 0])
  201. # 非表格
  202. else:
  203. with open(output_image_dict.get(page_no), "rb") as ff:
  204. image_stream = ff.read()
  205. image_text = from_ocr_interface(image_stream)
  206. text_list.append([image_text, page_no, 0])
  207. text_list.sort(key=lambda z: z[1])
  208. text = ""
  209. for t in text_list:
  210. text += t[0]
  211. return [text]
  212. except Exception as e:
  213. log("pdf format error!")
  214. traceback.print_exc()
  215. return [-3]
  216. text_list = []
  217. page_no = 0
  218. pages = PDFPage.create_pages(doc)
  219. pages = list(pages)
  220. page_count = len(pages)
  221. for page in pages:
  222. log("pdf2text pymupdf page_no " + str(page_no))
  223. # 限制pdf页数,只取前100页
  224. # if page_no >= 70:
  225. # log("pdf2text: pdf pages only get 70 pages")
  226. # break
  227. if page_count > 20:
  228. if 10 <= page_no < page_count - 10:
  229. page_no += 1
  230. continue
  231. # 判断页码在含表格页码中,直接拿已生成的text
  232. if page_no in has_table_no_list:
  233. # 判断表格是否跨页连接
  234. area_no = 0
  235. jump_page = 0
  236. for area in table_connect_list:
  237. if page_no in area:
  238. # 只记录一次text
  239. if page_no == area[0]:
  240. image_text = connect_text_list[area_no][0]
  241. text_list.append([image_text, page_no, 0])
  242. jump_page = 1
  243. area_no += 1
  244. # 是连接页的跳过后面步骤
  245. if jump_page:
  246. page_no += 1
  247. continue
  248. # 直接取text
  249. image_text = has_table_dict.get(page_no)[0]
  250. text_list.append([image_text, page_no, 0])
  251. page_no += 1
  252. continue
  253. # 不含表格的解析pdf
  254. else:
  255. if get_platform() == "Windows":
  256. try:
  257. interpreter.process_page(page)
  258. layout = device.get_result()
  259. except Exception:
  260. log("pdf2text pdfminer read pdf page error! continue...")
  261. continue
  262. else:
  263. # 设置超时时间
  264. try:
  265. # 解析pdf中的不含表格的页
  266. if get_platform() == "Windows":
  267. origin_pdf_analyze = pdf_analyze.__wrapped__
  268. layout = origin_pdf_analyze(interpreter, page, device)
  269. else:
  270. layout = pdf_analyze(interpreter, page, device, page_no)
  271. except TimeoutError as e:
  272. log("pdf2text pdfminer read pdf page time out!")
  273. return [-4]
  274. except Exception:
  275. log("pdf2text pdfminer read pdf page error! continue...")
  276. continue
  277. # 判断该页有没有文字对象,没有则有可能是有水印
  278. only_image = 1
  279. image_count = 0
  280. for x in layout:
  281. if isinstance(x, LTTextBoxHorizontal):
  282. only_image = 0
  283. if isinstance(x, LTFigure):
  284. image_count += 1
  285. # 如果该页图片数量过多,直接ocr整页识别
  286. log("pdf2text image_count " + str(image_count))
  287. if image_count >= 3:
  288. image_text = page_info_dict.get(page_no)[0]
  289. if image_text is None:
  290. with open(output_image_dict.get(page_no), "rb") as ff:
  291. image_stream = ff.read()
  292. image_text = from_ocr_interface(image_stream)
  293. if judge_error_code(image_text):
  294. return image_text
  295. page_info_dict[page_no][0] = image_text
  296. text_list.append([image_text, page_no, 0])
  297. page_no += 1
  298. continue
  299. order_list = []
  300. for x in layout:
  301. # 该对象是否是ocr识别
  302. ocr_flag = 0
  303. if get_platform() == "Windows":
  304. # print("x", page_no, x)
  305. print()
  306. if isinstance(x, LTTextBoxHorizontal):
  307. image_text = x.get_text()
  308. # 无法识别编码,用ocr
  309. if re.search('[(]cid:[0-9]+[)]', image_text):
  310. print(re.search('[(]cid:[0-9]+[)]', image_text))
  311. image_text = page_info_dict.get(page_no)[0]
  312. if image_text is None:
  313. with open(output_image_dict.get(page_no), "rb") as ff:
  314. image_stream = ff.read()
  315. image_text = from_ocr_interface(image_stream)
  316. if judge_error_code(image_text):
  317. return image_text
  318. page_info_dict[page_no][0] = image_text
  319. image_text = add_div(image_text)
  320. # order_list.append([image_text, page_no, x.bbox[1]])
  321. order_list = [[image_text, page_no, x.bbox[1]]]
  322. break
  323. else:
  324. image_text = add_div(image_text)
  325. order_list.append([image_text, page_no, x.bbox[1]])
  326. continue
  327. if isinstance(x, LTFigure):
  328. for image in x:
  329. if isinstance(image, LTImage):
  330. try:
  331. print("pdf2text LTImage size", page_no, image.width, image.height)
  332. image_stream = image.stream.get_data()
  333. # 小的图忽略
  334. if image.width <= 300 and image.height <= 300:
  335. continue
  336. # 有些水印导致pdf分割、读取报错
  337. # if image.width <= 200 and image.height<=200:
  338. # continue
  339. # img_test = Image.open(io.BytesIO(image_stream))
  340. # img_test.save('temp/LTImage.jpg')
  341. # 查看提取的图片高宽,太大则抛错用pdf输出图进行ocr识别
  342. img_test = Image.open(io.BytesIO(image_stream))
  343. if img_test.size[1] > 2000 or img_test.size[0] > 1500:
  344. print("pdf2text LTImage stream output size", img_test.size)
  345. raise Exception
  346. # 比较小的图则直接保存用ocr识别
  347. else:
  348. img_test.save('temp/LTImage.jpg')
  349. with open('temp/LTImage.jpg', "rb") as ff:
  350. image_stream = ff.read()
  351. image_text = from_ocr_interface(image_stream)
  352. if judge_error_code(image_text):
  353. return image_text
  354. # except pdfminer.pdftypes.PDFNotImplementedError:
  355. # with open(output_image_list[page_no], "rb") as ff:
  356. # image_stream = ff.read()
  357. except Exception:
  358. log("pdf2text pdfminer read image in page " + str(page_no) +
  359. " fail! use pymupdf read image...")
  360. # print(traceback.print_exc())
  361. image_text = page_info_dict.get(page_no)[0]
  362. if image_text is None:
  363. with open(output_image_dict.get(page_no), "rb") as ff:
  364. image_stream = ff.read()
  365. image_text = from_ocr_interface(image_stream)
  366. if judge_error_code(image_text):
  367. return image_text
  368. page_info_dict[page_no][0] = image_text
  369. ocr_flag = 1
  370. # 判断只拿到了水印图: 无文字输出且只有图片对象
  371. if image_text == "" and only_image:
  372. # 拆出该页pdf
  373. try:
  374. log("pdf2text guess pdf has watermark")
  375. split_path = get_single_pdf(path, page_no)
  376. except:
  377. # 如果拆分抛异常,则大概率不是水印图,用ocr识别图片
  378. log("pdf2text guess pdf has no watermark")
  379. image_text = page_info_dict.get(page_no)[0]
  380. if image_text is None:
  381. with open(output_image_dict.get(page_no), "rb") as ff:
  382. image_stream = ff.read()
  383. image_text = from_ocr_interface(image_stream)
  384. order_list.append([image_text, page_no, -1])
  385. page_info_dict[page_no][0] = image_text
  386. ocr_flag = 1
  387. continue
  388. if judge_error_code(split_path):
  389. return split_path
  390. # 调用office格式转换
  391. file_path = from_office_interface(split_path, unique_type_dir, 'html', 3)
  392. # if file_path == [-3]:
  393. # return [-3]
  394. if judge_error_code(file_path):
  395. return file_path
  396. # 获取html文本
  397. image_text = get_html_p(file_path)
  398. if judge_error_code(image_text):
  399. return image_text
  400. if get_platform() == "Windows":
  401. print("image_text", page_no, x.bbox[1], image_text)
  402. with open("temp" + str(x.bbox[0]) + ".jpg", "wb") as ff:
  403. ff.write(image_stream)
  404. image_text = add_div(image_text)
  405. if ocr_flag:
  406. order_list.append([image_text, page_no, -1])
  407. else:
  408. order_list.append([image_text, page_no, x.bbox[1]])
  409. order_list.sort(key=lambda z: z[2], reverse=True)
  410. # 有ocr参与识别
  411. if order_list[-1][2] == -1:
  412. ocr_order_list = [order_list[-1]]
  413. not_ocr_order_list = []
  414. not_ocr_text = ""
  415. # 去重,因读取失败而重复获取
  416. for order in order_list:
  417. if order[2] != -1:
  418. not_ocr_order_list.append(order)
  419. not_ocr_text += order[0]
  420. if string_similarity(ocr_order_list[0][0], not_ocr_text) >= 0.85:
  421. order_list = not_ocr_order_list
  422. else:
  423. order_list = ocr_order_list
  424. for order in order_list:
  425. text_list.append(order)
  426. page_no += 1
  427. text = ""
  428. for t in text_list:
  429. # text += add_div(t[0])
  430. if t[0] is not None:
  431. text += t[0]
  432. return [text]
  433. except UnicodeDecodeError as e:
  434. log("pdf2text pdfminer create pages failed! " + str(e))
  435. return [-3]
  436. except Exception as e:
  437. log("pdf2text error!")
  438. traceback.print_exc()
  439. return [-1]
  440. def get_single_pdf(path, page_no):
  441. log("into get_single_pdf")
  442. try:
  443. # print("path, ", path)
  444. pdf_origin = PdfFileReader(path, strict=False)
  445. pdf_new = PdfFileWriter()
  446. pdf_new.addPage(pdf_origin.getPage(page_no))
  447. path_new = path.split(".")[0] + "_split.pdf"
  448. with open(path_new, "wb") as ff:
  449. pdf_new.write(ff)
  450. return path_new
  451. except PyPDF2.utils.PdfReadError as e:
  452. raise e
  453. except Exception as e:
  454. log("get_single_pdf error! page " + str(page_no))
  455. traceback.print_exc()
  456. raise e
  457. def page_table_connect(has_table_dict):
  458. log("into page_table_connect")
  459. if not has_table_dict:
  460. return [], []
  461. try:
  462. # 判断是否有页码的表格相连
  463. table_connect_list = []
  464. temp_list = []
  465. # 离图片顶部或底部距离,页面高度的1/7
  466. threshold = 7
  467. page_no_list = list(has_table_dict.keys())
  468. page_no_list.sort(key=lambda x: x)
  469. for i in range(1, len(page_no_list)):
  470. page_info = has_table_dict.get(page_no_list[i])
  471. last_page_info = has_table_dict.get(page_no_list[i - 1])
  472. # 页码需相连
  473. if page_no_list[i] - page_no_list[i - 1] == 1:
  474. # 上一页最后一个区域的列数和下一页第一个区域列数都为0,且相等
  475. if not last_page_info[1][-1] and not page_info[1][0] and \
  476. last_page_info[1][-1] == page_info[1][0]:
  477. # 上一页的轮廓点要离底部一定距离内,下一页的轮廓点要离顶部一定距离内
  478. if last_page_info[4][0] - last_page_info[2][-1][1][1] \
  479. <= int(last_page_info[4][0] / threshold) \
  480. and page_info[2][0][0][1] - 0 \
  481. <= int(page_info[4][0] / threshold):
  482. temp_list.append(page_no_list[i - 1])
  483. temp_list.append(page_no_list[i])
  484. continue
  485. # 条件不符合的,存储之前保存的连接页码
  486. if len(temp_list) > 1:
  487. temp_list = list(set(temp_list))
  488. temp_list.sort(key=lambda x: x)
  489. table_connect_list.append(temp_list)
  490. temp_list = []
  491. if len(temp_list) > 1:
  492. temp_list = list(set(temp_list))
  493. temp_list.sort(key=lambda x: x)
  494. table_connect_list.append(temp_list)
  495. temp_list = []
  496. # 连接两页内容
  497. connect_text_list = []
  498. for area in table_connect_list:
  499. first_page_no = area[0]
  500. area_page_text = str(has_table_dict.get(first_page_no)[0])
  501. for i in range(1, len(area)):
  502. current_page_no = area[i]
  503. current_page_text = str(has_table_dict.get(current_page_no)[0])
  504. # 连接两个table
  505. table_prefix = re.finditer('<table border="1">', current_page_text)
  506. index_list = []
  507. for t in table_prefix:
  508. index_list.append(t.span())
  509. delete_index = index_list[0]
  510. current_page_text = current_page_text[:delete_index[0]] \
  511. + current_page_text[delete_index[1]:]
  512. table_suffix = re.finditer('</table>', area_page_text)
  513. index_list = []
  514. for t in table_suffix:
  515. index_list.append(t.span())
  516. delete_index = index_list[-1]
  517. area_page_text = area_page_text[:delete_index[0]] \
  518. + area_page_text[delete_index[1]:]
  519. area_page_text = area_page_text + current_page_text
  520. connect_text_list.append([area_page_text, area])
  521. return table_connect_list, connect_text_list
  522. except Exception as e:
  523. # print("page_table_connect", e)
  524. log("page_table_connect error!")
  525. traceback.print_exc()
  526. return [-1], [-1]
  527. @timeout(30, timeout_exception=TimeoutError)
  528. def read_pdf(path, package_name, packages):
  529. log(package_name)
  530. laparams = LAParams(line_overlap=0.01,
  531. char_margin=0.3,
  532. line_margin=0.01,
  533. word_margin=0.01,
  534. boxes_flow=0.1, )
  535. if package_name == packages[0]:
  536. fp = open(path, 'rb')
  537. parser = PDFParser(fp)
  538. doc_pdfminer = PDFDocument(parser)
  539. rsrcmgr = PDFResourceManager()
  540. device = PDFPageAggregator(rsrcmgr, laparams=laparams)
  541. interpreter = PDFPageInterpreter(rsrcmgr, device)
  542. return doc_pdfminer, device, interpreter
  543. elif package_name == packages[1]:
  544. doc_pymupdf = fitz.open(path)
  545. return doc_pymupdf
  546. elif package_name == packages[2]:
  547. doc_pypdf2 = PdfFileReader(path, strict=False)
  548. doc_pypdf2_new = PdfFileWriter()
  549. return doc_pypdf2, doc_pypdf2_new
  550. elif package_name == packages[3]:
  551. fp = open(path, 'rb')
  552. lt = LineTable()
  553. doc_top = 0
  554. doc_pdfplumber = read_pdfplumber(fp, laparams)
  555. return lt, doc_top, doc_pdfplumber
  556. @timeout(25, timeout_exception=TimeoutError)
  557. def read_pdfminer(path, laparams):
  558. fp = open(path, 'rb')
  559. parser = PDFParser(fp)
  560. doc_pdfminer = PDFDocument(parser)
  561. rsrcmgr = PDFResourceManager()
  562. device = PDFPageAggregator(rsrcmgr, laparams=laparams)
  563. interpreter = PDFPageInterpreter(rsrcmgr, device)
  564. return doc_pdfminer, device, interpreter
  565. @timeout(15, timeout_exception=TimeoutError)
  566. def read_pymupdf(path):
  567. return fitz.open(path)
  568. @timeout(15, timeout_exception=TimeoutError)
  569. def read_pypdf2(path):
  570. doc_pypdf2 = PdfFileReader(path, strict=False)
  571. doc_pypdf2_new = PdfFileWriter()
  572. return doc_pypdf2, doc_pypdf2_new
  573. @timeout(25, timeout_exception=TimeoutError, use_signals=False)
  574. def read_pdfplumber(path, laparams):
  575. fp = open(path, 'rb')
  576. lt = LineTable()
  577. doc_top = 0
  578. doc_pdfplumber = PDF(fp, laparams=laparams.__dict__)
  579. return lt, doc_top, doc_pdfplumber
  580. class PDFConvert:
  581. def __init__(self, path, unique_type_dir):
  582. self._doc = _Document(path)
  583. self.path = path
  584. self.unique_type_dir = unique_type_dir
  585. if not os.path.exists(self.unique_type_dir):
  586. os.mkdir(self.unique_type_dir)
  587. self.packages = ["pdfminer", "PyMuPDF", "PyPDF2", "pdfplumber"]
  588. self.has_init_pdf = [0] * len(self.packages)
  589. @memory_decorator
  590. def init_package(self, package_name):
  591. # 各个包初始化
  592. try:
  593. laparams = LAParams(line_overlap=0.01,
  594. char_margin=0.3,
  595. line_margin=0.01,
  596. word_margin=0.01,
  597. boxes_flow=0.1, )
  598. if package_name == self.packages[0]:
  599. # fp = open(self.path, 'rb')
  600. # parser = PDFParser(fp)
  601. # self.doc_pdfminer = PDFDocument(parser)
  602. # rsrcmgr = PDFResourceManager()
  603. # self.laparams = LAParams(line_overlap=0.01,
  604. # char_margin=0.3,
  605. # line_margin=0.01,
  606. # word_margin=0.01,
  607. # boxes_flow=0.1,)
  608. # self.device = PDFPageAggregator(rsrcmgr, laparams=self.laparams)
  609. # self.interpreter = PDFPageInterpreter(rsrcmgr, self.device)
  610. self.doc_pdfminer, self.device, self.interpreter = read_pdfminer(self.path, laparams)
  611. self.has_init_pdf[0] = 1
  612. elif package_name == self.packages[1]:
  613. self.doc_pymupdf = read_pymupdf(self.path)
  614. self.has_init_pdf[1] = 1
  615. elif package_name == self.packages[2]:
  616. # self.doc_pypdf2 = PdfFileReader(self.path, strict=False)
  617. # self.doc_pypdf2_new = PdfFileWriter()
  618. self.doc_pypdf2, self.doc_pypdf2_new = read_pypdf2(self.path)
  619. self.has_init_pdf[2] = 1
  620. elif package_name == self.packages[3]:
  621. # self.fp = open(self.path, 'rb')
  622. # self.lt = LineTable()
  623. # self.doc_top = 0
  624. # self.doc_pdfplumber = PDF(self.fp, laparams=self.laparams.__dict__)
  625. self.lt, self.doc_top, self.doc_pdfplumber = read_pdfplumber(self.path, laparams)
  626. self.has_init_pdf[3] = 0
  627. else:
  628. log("Only Support Packages " + str(self.packages))
  629. raise Exception
  630. except Exception as e:
  631. log(package_name + " cannot open pdf!")
  632. traceback.print_exc()
  633. self._doc.error_code = [-3]
  634. def convert(self):
  635. if self.has_init_pdf[0] == 0:
  636. self.init_package("pdfminer")
  637. if self._doc.error_code is not None:
  638. self._doc.error_code = None
  639. # pdfminer读不了直接转成图片识别
  640. self.get_all_page_image()
  641. return
  642. # 判断是否能读pdf
  643. try:
  644. pages = PDFPage.create_pages(self.doc_pdfminer)
  645. for page in pages:
  646. break
  647. pages = list(pages)
  648. # except pdfminer.psparser.PSEOF as e:
  649. except:
  650. # pdfminer 读不了空白页的对象,直接使用pymupdf转换出的图片进行ocr识别
  651. log("pdf2text pdfminer read failed! read by pymupdf!")
  652. traceback.print_exc()
  653. try:
  654. self.get_all_page_image()
  655. return
  656. except:
  657. traceback.print_exc()
  658. log("pdf2text use pymupdf read failed!")
  659. self._doc.error_code = [-3]
  660. return
  661. # 每一页进行处理
  662. pages = PDFPage.create_pages(self.doc_pdfminer)
  663. pages = list(pages)
  664. page_count = len(pages)
  665. page_no = 0
  666. for page in pages:
  667. # 限制pdf页数,只取前后各10页
  668. if page_count > 20:
  669. if 10 <= page_no < page_count - 10:
  670. page_no += 1
  671. continue
  672. self._page = _Page(page, page_no)
  673. # 解析单页
  674. self.convert_page(page, page_no)
  675. # print("+"*30, page.resources)
  676. if self._doc.error_code is None and self._page.error_code is not None:
  677. if self._page.error_code[0] in [-4, -3, 0]:
  678. page_no += 1
  679. continue
  680. else:
  681. self._doc.error_code = self._page.error_code
  682. break
  683. self._doc.add_child(self._page)
  684. page_no += 1
  685. def clean_text(self, _text):
  686. return re.sub("\s", "", _text)
  687. def get_text_lines(self, page, page_no):
  688. lt_line_list = []
  689. page_plumber = pdfPage(self.doc_pdfplumber, page, page_number=page_no, initial_doctop=self.doc_top)
  690. self.doc_top += page_plumber.height
  691. table_finder = TableFinder(page_plumber)
  692. all_width_zero = True
  693. for _edge in table_finder.get_edges():
  694. if _edge.get('linewidth') and _edge.get('linewidth') > 0:
  695. all_width_zero = False
  696. break
  697. for _edge in table_finder.get_edges():
  698. # print(_edge)
  699. if _edge.get('linewidth', 0.1) > 0 or all_width_zero:
  700. lt_line_list.append(LTLine(1, (float(_edge["x0"]), float(_edge["y0"])),
  701. (float(_edge["x1"]), float(_edge["y1"]))))
  702. log("pdf page %s has %s lines" % (str(page_no), str(len(lt_line_list))))
  703. return lt_line_list
  704. def get_page_lines(self, layout, page_no):
  705. def _plot(_line_list, mode=1):
  706. for _line in _line_list:
  707. if mode == 1:
  708. x0, y0, x1, y1 = _line.__dict__.get("bbox")
  709. elif mode == 2:
  710. x0, y0, x1, y1 = _line
  711. plt.plot([x0, x1], [y0, y1])
  712. plt.show()
  713. return
  714. def is_cross(A, B, C, D):
  715. if A[0] == B[0] == C[0] == D[0]:
  716. if A[1] <= C[1] <= B[1] or A[1] <= D[1] <= B[1] \
  717. or C[1] <= A[1] <= D[1] or C[1] <= B[1] <= D[1]:
  718. return True
  719. if A[1] == B[1] == C[1] == D[1]:
  720. if A[0] <= C[0] <= B[0] or A[0] <= D[0] <= B[0] \
  721. or C[0] <= A[0] <= D[0] or C[0] <= B[0] <= D[0]:
  722. return True
  723. line1 = LineString([A, B])
  724. line2 = LineString([C, D])
  725. int_pt = line1.intersection(line2)
  726. try:
  727. point_of_intersection = int_pt.x, int_pt.y
  728. return True
  729. except:
  730. return False
  731. def calculate_k(bbox):
  732. x = [bbox[0], bbox[2]]
  733. y = [bbox[1], bbox[3]]
  734. slope, intercept, r_value, p_value, std_err = linregress(x, y)
  735. # print('k', slope)
  736. if math.isnan(slope):
  737. slope = 0
  738. return slope
  739. def line_iou(line1, line2, axis=0):
  740. inter = min(line1[1][axis], line2[1][axis]) - max(line1[0][axis], line2[0][axis])
  741. # union = max(line1[1][axis], line2[1][axis]) - min(line1[0][axis], line2[0][axis])
  742. union = min(abs(line1[0][axis] - line1[1][axis]), abs(line2[0][axis] - line2[1][axis]))
  743. if union in [0, 0.]:
  744. iou = 0.
  745. else:
  746. iou = inter / union
  747. return iou
  748. def get_cross_line(_line_list, threshold=1, cross_times=0):
  749. # 根据是否有交点判断表格线
  750. _cross_line_list = []
  751. for line1 in _line_list:
  752. if line1 in _cross_line_list:
  753. continue
  754. if abs(line1[2] - line1[0]) > abs(line1[3] - line1[1]):
  755. p1 = [max(0, line1[0] - threshold), line1[1]]
  756. p2 = [min(line1[2] + threshold, page_w), line1[3]]
  757. else:
  758. p1 = [line1[0], max(0, line1[1] - threshold)]
  759. p2 = [line1[2], min(line1[3] + threshold, page_h)]
  760. line1 = [p1[0], p1[1], p2[0], p2[1]]
  761. _times = 0
  762. for line2 in _line_list:
  763. if abs(line2[2] - line2[0]) > abs(line2[3] - line2[1]):
  764. p3 = [max(0, line2[0] - threshold), line2[1]]
  765. p4 = [min(line2[2] + threshold, page_w), line2[3]]
  766. else:
  767. p3 = [line2[0], max(0, line2[1] - threshold)]
  768. p4 = [line2[2], min(line2[3] + threshold, page_h)]
  769. line2 = [p3[0], p3[1], p4[0], p4[1]]
  770. if line1 == line2:
  771. continue
  772. if is_cross(p1, p2, p3, p4):
  773. _times += 1
  774. if _times >= cross_times:
  775. _cross_line_list += [line1]
  776. break
  777. return _cross_line_list
  778. def repair_bias_line(_line_list):
  779. temp_list = []
  780. for line in _line_list:
  781. x0, y0, x1, y1 = line
  782. _y = min(y0, y1)
  783. _x = min(x0, x1)
  784. if abs(x0 - x1) > abs(y0 - y1):
  785. temp_list.append([x0, _y, x1, _y])
  786. else:
  787. temp_list.append([_x, y0, _x, y1])
  788. _line_list = temp_list
  789. return _line_list
  790. def repair_col_line(_straight_list, _bias_list, threshold=2, min_width=7):
  791. if not _straight_list or not _bias_list:
  792. print('add_col_bias_line empty', len(_straight_list), len(_bias_list))
  793. return []
  794. # 分列
  795. _straight_list.sort(key=lambda x: (x[0], x[1]))
  796. cols = []
  797. col = []
  798. current_w = _straight_list[0][0]
  799. for line in _straight_list:
  800. if abs(line[0] - line[2]) > abs(line[1] - line[3]):
  801. continue
  802. if min(line[0], line[2]) - threshold <= current_w <= max(line[0], line[2]) + threshold:
  803. col.append(line)
  804. else:
  805. if col:
  806. cols.append(col)
  807. col = [line]
  808. current_w = line[0]
  809. if col:
  810. cols.append(col)
  811. # 补充col
  812. new_list = []
  813. for line in bias_line_list:
  814. if abs(line[0] - line[2]) > abs(line[1] - line[3]):
  815. continue
  816. for col in cols:
  817. w = col[0][0]
  818. if w - threshold <= line[0] <= w + threshold or w - threshold <= line[2] <= w + threshold:
  819. new_list.append([w, line[1] - 3, w, line[3] + 3])
  820. new_list += _straight_list
  821. # 去重
  822. new_list = [str(x) for x in new_list]
  823. new_list = list(set(new_list))
  824. new_list = [eval(x) for x in new_list]
  825. # 分列
  826. new_list.sort(key=lambda x: (x[0], x[1]))
  827. cols = []
  828. col = []
  829. current_w = new_list[0][0]
  830. for line in new_list:
  831. if abs(line[0] - line[2]) > abs(line[1] - line[3]):
  832. continue
  833. if min(line[0], line[2]) - threshold <= current_w <= max(line[0], line[2]) + threshold:
  834. col.append(line)
  835. else:
  836. if col:
  837. cols.append(col)
  838. col = [line]
  839. current_w = line[0]
  840. if col:
  841. cols.append(col)
  842. # 删除col
  843. for col1 in cols:
  844. for col2 in cols:
  845. if col1 == col2 or abs(col1[0][0] - col2[0][0]) > min_width:
  846. continue
  847. col1_len, col2_len = 0, 0
  848. for c in col1:
  849. col1_len += abs(c[1] - c[3])
  850. for c in col2:
  851. col2_len += abs(c[1] - c[3])
  852. if col1_len > col2_len * 3:
  853. for c in col2:
  854. if c in new_list:
  855. new_list.remove(c)
  856. if col2_len > col1_len * 3:
  857. for c in col1:
  858. if c in new_list:
  859. new_list.remove(c)
  860. return new_list
  861. def merge_line(_line_list, threshold=2):
  862. new_line_list = []
  863. # 分列
  864. _line_list.sort(key=lambda x: (x[0], x[1]))
  865. cols = []
  866. col = [_line_list[0]]
  867. current_w = _line_list[0][0]
  868. for line in _line_list:
  869. if abs(line[0] - line[2]) > abs(line[1] - line[3]):
  870. continue
  871. if min(line[0], line[2]) - threshold <= current_w <= max(line[0], line[2]) + threshold \
  872. and is_cross(line[0:2], line[2:4], col[-1][0:2], col[-1][2:4]):
  873. col.append(line)
  874. else:
  875. if col:
  876. cols.append(col)
  877. col = [line]
  878. current_w = line[0]
  879. if col:
  880. cols.append(col)
  881. for col in cols:
  882. temp_c = col[0]
  883. col_w = col[0][0]
  884. for i in range(len(col) - 1):
  885. c = col[i]
  886. next_c = col[i + 1]
  887. if is_cross(c[0:2], c[2:4], next_c[0:2], next_c[2:4]):
  888. temp_c = [col_w, min(temp_c[1], c[1], c[3], next_c[1], next_c[3]), col_w,
  889. max(temp_c[3], c[1], c[3], next_c[1], next_c[3])]
  890. else:
  891. new_line_list.append(temp_c)
  892. temp_c = next_c
  893. if not new_line_list or (new_line_list and new_line_list[-1] != temp_c):
  894. new_line_list.append(temp_c)
  895. # 分行
  896. _line_list.sort(key=lambda x: (x[1], x[0]))
  897. rows = []
  898. row = []
  899. current_h = _line_list[0][1]
  900. for line in _line_list:
  901. if abs(line[0] - line[2]) < abs(line[1] - line[3]):
  902. continue
  903. if min(line[1], line[3]) - threshold <= current_h <= max(line[1], line[3]) + threshold:
  904. row.append(line)
  905. else:
  906. if row:
  907. rows.append(row)
  908. row = [line]
  909. current_h = line[1]
  910. if row:
  911. rows.append(row)
  912. for row in rows:
  913. temp_r = row[0]
  914. row_h = row[0][1]
  915. for i in range(len(row) - 1):
  916. r = row[i]
  917. next_r = row[i + 1]
  918. # if is_cross(r[0:2], r[2:4], next_r[0:2], next_r[2:4]):
  919. if line_iou([r[0:2], r[2:4]], [next_r[0:2], next_r[2:4]], axis=0):
  920. temp_r = [min(temp_r[0], r[0], r[2], next_r[0], next_r[2]), row_h,
  921. max(temp_r[2], r[0], r[2], next_r[0], next_r[2]), row_h]
  922. else:
  923. new_line_list.append(temp_r)
  924. temp_r = next_r
  925. if not new_line_list or (new_line_list and new_line_list[-1] != temp_r):
  926. new_line_list.append(temp_r)
  927. return new_line_list
  928. def remove_outline_no_cross(_line_list):
  929. row_list = []
  930. col_list = []
  931. for line in _line_list:
  932. # 存所有行
  933. if abs(line[0] - line[2]) > abs(line[1] - line[3]):
  934. row_list.append(line)
  935. # 存所有列
  936. if abs(line[0] - line[2]) < abs(line[1] - line[3]):
  937. col_list.append(line)
  938. if not col_list:
  939. return _line_list
  940. # 左右两条边框
  941. col_list.sort(key=lambda x: (x[0], x[1]))
  942. left_col = col_list[0]
  943. right_col = col_list[-1]
  944. # 判断有交点但中间区域无交点
  945. compare_list = []
  946. for col in [left_col, right_col]:
  947. add_h = abs(col[1]-col[3]) / 8
  948. center_area = [col[1]+add_h, col[3]-add_h]
  949. cross_cnt = 0
  950. center_cross_cnt = 0
  951. center_row_cnt = 0
  952. for row in row_list:
  953. if is_cross(row[0:2], row[2:4], col[0:2], col[2:4]):
  954. if center_area[0] <= row[1] <= center_area[1]:
  955. center_cross_cnt += 1
  956. else:
  957. cross_cnt += 1
  958. else:
  959. if center_area[0] <= row[1] <= center_area[1]:
  960. center_row_cnt += 1
  961. compare_list.append([cross_cnt, center_cross_cnt, center_row_cnt])
  962. _flag = True
  963. for c in compare_list:
  964. if c[0] >= 2 and c[1] == 0 and c[2] >= 2:
  965. continue
  966. _flag = False
  967. print('compare_list', compare_list)
  968. if _flag and compare_list[0][1] == compare_list[1][1] \
  969. and compare_list[0][2] == compare_list[1][2]:
  970. for col in [left_col, right_col]:
  971. if col in _line_list:
  972. _line_list.remove(col)
  973. return _line_list
  974. log('into get_page_lines')
  975. page_h = layout.height
  976. page_w = layout.width
  977. element_list = []
  978. line_list = []
  979. bias_line_list = []
  980. text_bbox_list = []
  981. for element in layout:
  982. if isinstance(element, LTTextContainer):
  983. text_bbox_list.append(element.bbox)
  984. # 只取这三种类型的bbox
  985. if isinstance(element, (LTRect, LTCurve, LTLine)):
  986. element_list.append(element)
  987. if element.height > 0.5 and element.width > 0.5:
  988. # print('element.height, element.width', element.height, element.width)
  989. k = calculate_k(element.bbox)
  990. if 1.73 / 3 < abs(k) < 1.73:
  991. continue
  992. else:
  993. bias_line_list.append(element.bbox)
  994. continue
  995. line_list.append(element.bbox)
  996. if not line_list and not bias_line_list:
  997. return []
  998. # 是否使用斜线来生成表格
  999. if len(line_list) < 6 and len(bias_line_list) > len(line_list) * 2:
  1000. # print('use bias line')
  1001. # bias_line_list += add_col_bias_line(line_list, bias_line_list)
  1002. line_list = bias_line_list
  1003. # 去重
  1004. line_list = [str(x) for x in line_list]
  1005. line_list = list(set(line_list))
  1006. line_list = [eval(x) for x in line_list]
  1007. # 根据是否有交点判断表格线
  1008. cross_line_list = get_cross_line(line_list, threshold=2, cross_times=1)
  1009. if not cross_line_list:
  1010. return []
  1011. # 斜线校正
  1012. if cross_line_list:
  1013. cross_line_list = repair_bias_line(cross_line_list)
  1014. # 修复竖线
  1015. if bias_line_list:
  1016. cross_line_list = repair_col_line(cross_line_list, bias_line_list)
  1017. # 根据是否有交点判断表格线
  1018. cross_line_list = get_cross_line(cross_line_list, threshold=1, cross_times=1)
  1019. # 合并线条
  1020. cross_line_list = merge_line(cross_line_list)
  1021. # 删除最外层嵌套边框
  1022. cross_line_list = remove_outline_no_cross(cross_line_list)
  1023. # show
  1024. # print('len(cross_line_list)', len(cross_line_list))
  1025. # _plot(line_list, mode=2)
  1026. # _plot(cross_line_list, mode=2)
  1027. lt_line_list = []
  1028. for line in cross_line_list:
  1029. lt_line_list.append(LTLine(1, (float(line[0]), float(line[1])),
  1030. (float(line[2]), float(line[3]))))
  1031. log("pdf page %s has %s lines" % (str(page_no), str(len(lt_line_list))))
  1032. return lt_line_list
  1033. def recognize_text(self, layout, page_no, lt_text_list, lt_line_list):
  1034. list_tables, filter_objs, _ = self.lt.recognize_table(lt_text_list, lt_line_list)
  1035. self._page.in_table_objs = filter_objs
  1036. # print("=======text_len:%d:filter_len:%d"%(len(lt_text_list),len(filter_objs)))
  1037. for table in list_tables:
  1038. _table = _Table(table["table"], table["bbox"])
  1039. # self._page.children.append(_table)
  1040. self._page.add_child(_table)
  1041. list_sentences = ParseUtils.recognize_sentences(lt_text_list, filter_objs,
  1042. layout.bbox, page_no)
  1043. for sentence in list_sentences:
  1044. _sen = _Sentence(sentence.text, sentence.bbox)
  1045. self._page.add_child(_sen)
  1046. # pdf对象需反向排序
  1047. self._page.is_reverse = True
  1048. def is_text_legal(self, lt_text_list, page_no):
  1049. # 无法识别pdf字符编码,整页用ocr
  1050. text_temp = ""
  1051. for _t in lt_text_list:
  1052. text_temp += _t.get_text()
  1053. if re.search('[(]cid:[0-9]+[)]', text_temp):
  1054. log("text has cid! try pymupdf...")
  1055. page_image = self.get_page_image(page_no)
  1056. if judge_error_code(page_image):
  1057. self._page.error_code = page_image
  1058. else:
  1059. _image = _Image(page_image[1], page_image[0])
  1060. self._page.add_child(_image)
  1061. return False
  1062. match1 = re.findall(get_garble_code(), text_temp)
  1063. # match2 = re.search('[\u4e00-\u9fa5]', text_temp)
  1064. if len(match1) > 3 and len(text_temp) > 10:
  1065. log("pdf garbled code! try pymupdf... " + text_temp[:20])
  1066. page_image = self.get_page_image(page_no)
  1067. if judge_error_code(page_image):
  1068. self._page.error_code = page_image
  1069. else:
  1070. _image = _Image(page_image[1], page_image[0])
  1071. self._page.add_child(_image)
  1072. return False
  1073. return True
  1074. def convert_page(self, page, page_no):
  1075. # pdf page.annots为None,不经过get_layout,直接ocr
  1076. # if page.annots is None:
  1077. # lt_image_list = []
  1078. # lt_text_list = []
  1079. # # 设置只有图片,可跳到ocr
  1080. # only_image = 1
  1081. # image_count = 1
  1082. # else:
  1083. layout = self.get_layout(page, page_no)
  1084. if self._doc.error_code is not None:
  1085. return
  1086. if judge_error_code(layout):
  1087. self._page.error_code = layout
  1088. return
  1089. # 判断该页的对象类型,并存储
  1090. # only_image = 1
  1091. # image_count = 0
  1092. lt_text_list = []
  1093. lt_image_list = []
  1094. for x in layout:
  1095. if isinstance(x, (LTTextBoxHorizontal, LTTextBoxVertical)):
  1096. # only_image = 0
  1097. lt_text_list.append(x)
  1098. if isinstance(x, LTFigure):
  1099. for y in x:
  1100. if isinstance(y, LTImage):
  1101. lt_image_list.append(y)
  1102. # image_count += 1
  1103. lt_text_list = self.delete_water_mark(lt_text_list, layout.bbox, 15)
  1104. log("convert_pdf page " + str(page_no))
  1105. log("len(lt_image_list), len(lt_text_list) " + str(len(lt_image_list)) + " " + str(len(lt_text_list)))
  1106. # 若只有文本且图片数为0,直接提取文字及表格
  1107. # if only_image == 0 and image_count == 0:
  1108. if len(lt_image_list) == 0 and len(lt_text_list) > 0:
  1109. # PDFPlumber
  1110. if self.has_init_pdf[3] == 0:
  1111. self.init_package("pdfplumber")
  1112. if self._doc.error_code is not None:
  1113. self._doc.error_code = None
  1114. log("init pdfplumber failed! try pymupdf...")
  1115. # 调用pdfplumber获取pdf图片报错,则使用pypdf2将pdf转html
  1116. page_image = self.get_page_image(page_no)
  1117. if judge_error_code(page_image):
  1118. self._page.error_code = page_image
  1119. else:
  1120. _image = _Image(page_image[1], page_image[0])
  1121. self._page.add_child(_image)
  1122. return
  1123. if not self.is_text_legal(lt_text_list, page_no):
  1124. return
  1125. try:
  1126. lt_line_list = self.get_page_lines(layout, page_no)
  1127. except:
  1128. traceback.print_exc()
  1129. lt_line_list = []
  1130. self._page.error_code = [-13]
  1131. try:
  1132. # lt_line_list = self.get_text_lines(page,page_no)
  1133. self.recognize_text(layout, page_no, lt_text_list, lt_line_list)
  1134. except:
  1135. traceback.print_exc()
  1136. self._page.error_code = [-8]
  1137. # 若该页图片数量过多,或无文本,则直接ocr整页识别
  1138. # elif image_count > 3 or only_image == 1:
  1139. elif len(lt_image_list) > 3 or len(lt_text_list) == 0:
  1140. page_image = self.get_page_image(page_no)
  1141. if judge_error_code(page_image):
  1142. self._page.error_code = page_image
  1143. else:
  1144. _image = _Image(page_image[1], page_image[0])
  1145. _image.is_from_pdf = True
  1146. self._page.add_child(_image)
  1147. # 正常读取该页对象
  1148. else:
  1149. # 图表对象
  1150. for image in lt_image_list:
  1151. try:
  1152. # print("pdf2text LTImage size", page_no, image.width, image.height)
  1153. image_stream = image.stream.get_data()
  1154. # 小的图忽略
  1155. if image.width <= 300 and image.height <= 300:
  1156. continue
  1157. # 查看提取的图片高宽,太大则用pdf输出图进行ocr识别
  1158. img_test = Image.open(io.BytesIO(image_stream))
  1159. # img_test.show()
  1160. if image.height >= 1000 and image.width >= 1000:
  1161. # print("pdf2text LTImage stream output size", img_test.size)
  1162. page_image = self.get_page_image(page_no)
  1163. if judge_error_code(page_image):
  1164. self._page.error_code = page_image
  1165. else:
  1166. _image = _Image(page_image[1], page_image[0])
  1167. _image.is_from_pdf = True
  1168. self._page.add_child(_image)
  1169. return
  1170. # 比较小的图则直接保存用ocr识别
  1171. else:
  1172. temp_path = self.unique_type_dir + 'page' + str(page_no) \
  1173. + '_lt' + str(lt_image_list.index(image)) + '.jpg'
  1174. img_test.save(temp_path)
  1175. with open(temp_path, "rb") as ff:
  1176. image_stream = ff.read()
  1177. _image = _Image(image_stream, temp_path, image.bbox)
  1178. self._page.add_child(_image)
  1179. except Exception:
  1180. log("pdf2text pdfminer read image in page " + str(page_no) +
  1181. " fail! use pymupdf read image...")
  1182. traceback.print_exc()
  1183. # pdf对象需反向排序
  1184. self._page.is_reverse = True
  1185. self.init_package("pdfplumber")
  1186. if not self.is_text_legal(lt_text_list, page_no):
  1187. return
  1188. # lt_line_list = self.get_text_lines(page, page_no)
  1189. try:
  1190. lt_line_list = self.get_page_lines(layout, page_no)
  1191. except:
  1192. traceback.print_exc()
  1193. lt_line_list = []
  1194. self._page.error_code = [-13]
  1195. self.recognize_text(layout, page_no, lt_text_list, lt_line_list)
  1196. def get_layout(self, page, page_no):
  1197. log("get_layout")
  1198. if self.has_init_pdf[0] == 0:
  1199. self.init_package("pdfminer")
  1200. if self._doc.error_code is not None:
  1201. return
  1202. # 获取该页layout
  1203. start_time = time.time()
  1204. try:
  1205. if get_platform() == "Windows":
  1206. # origin_pdf_analyze = pdf_analyze.__wrapped__
  1207. # layout = origin_pdf_analyze(self.interpreter, page, self.device)
  1208. layout = pdf_analyze(self.interpreter, page, self.device, page_no)
  1209. else:
  1210. layout = pdf_analyze(self.interpreter, page, self.device, page_no)
  1211. except TimeoutError as e:
  1212. log("pdf2text pdfminer read pdf page " + str(page_no) + " time out! " + str(time.time() - start_time))
  1213. layout = [-4]
  1214. except Exception:
  1215. traceback.print_exc()
  1216. log("pdf2text pdfminer read pdf page " + str(page_no) + " error! continue...")
  1217. layout = [-3]
  1218. return layout
  1219. def get_page_image(self, page_no):
  1220. log("")
  1221. try:
  1222. if self.has_init_pdf[1] == 0:
  1223. self.init_package("PyMuPDF")
  1224. if self._doc.error_code is not None:
  1225. return
  1226. # save_dir = self.path.split(".")[-2] + "_" + self.path.split(".")[-1]
  1227. output = self.unique_type_dir + "page" + str(page_no) + ".png"
  1228. page = self.doc_pymupdf.loadPage(page_no)
  1229. rotate = int(0)
  1230. zoom_x = 2.
  1231. zoom_y = 2.
  1232. mat = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
  1233. pix = page.getPixmap(matrix=mat, alpha=False)
  1234. pix.writePNG(output)
  1235. # 输出图片resize
  1236. self.resize_image(output)
  1237. with open(output, "rb") as f:
  1238. pdf_image = f.read()
  1239. return [output, pdf_image]
  1240. except ValueError as e:
  1241. traceback.print_exc()
  1242. if str(e) == "page not in document":
  1243. log("pdf2Image page not in document! continue... page " + str(page_no))
  1244. return [0]
  1245. elif "encrypted" in str(e):
  1246. log("pdf2Image document need password " + str(page_no))
  1247. return [-7]
  1248. except RuntimeError as e:
  1249. if "cannot find page" in str(e):
  1250. log("pdf2Image page {} not in document! continue... ".format(str(page_no)) + str(e))
  1251. return [0]
  1252. else:
  1253. traceback.print_exc()
  1254. return [-3]
  1255. def get_all_page_image(self):
  1256. log("")
  1257. if self.has_init_pdf[1] == 0:
  1258. self.init_package("PyMuPDF")
  1259. if self._doc.error_code is not None:
  1260. return
  1261. page_count = self.doc_pymupdf.page_count
  1262. for page_no in range(page_count):
  1263. # 限制pdf页数,只取前10页后10页
  1264. if page_count > 20:
  1265. if 10 <= page_no < page_count - 10:
  1266. continue
  1267. self._page = _Page(None, page_no)
  1268. page_image = self.get_page_image(page_no)
  1269. if judge_error_code(page_image):
  1270. self._page.error_code = page_image
  1271. else:
  1272. _image = _Image(page_image[1], page_image[0])
  1273. self._page.add_child(_image)
  1274. # 报错继续读后面页面
  1275. if self._doc.error_code is None and self._page.error_code is not None:
  1276. continue
  1277. self._doc.add_child(self._page)
  1278. def connect_table(self, html_list):
  1279. if not html_list:
  1280. return html_list
  1281. # 判断条件1:最后一个表格后有无非页码文本/第一个表格前有无文本
  1282. connect_flag_list = []
  1283. soup_list = []
  1284. for i, h in enumerate(html_list):
  1285. soup_list.append(BeautifulSoup(h, 'lxml'))
  1286. # 找最后一个表格
  1287. table_start1, table_end1 = None, None
  1288. # print('h', h)
  1289. match = re.finditer('<table', h)
  1290. for m in match:
  1291. table_start1 = m.span()[0]
  1292. if table_start1 is not None:
  1293. match = re.finditer('</table>', h[table_start1:])
  1294. for m in match:
  1295. table_end1 = m.span()[1] + table_start1
  1296. # 最后一个表格后有无除了页码外的内容
  1297. connect_flag1 = False
  1298. if table_end1 is not None:
  1299. match = re.search('[^-/第页0-9]*', re.sub('<div>|</div>', '', h[table_end1:]))
  1300. # print('match1', match.group())
  1301. if not match or match.group() == '':
  1302. connect_flag1 = True
  1303. # 找第一个表格
  1304. table_start2, table_end2 = None, None
  1305. match = re.finditer('<table', h)
  1306. for m in match:
  1307. table_start2 = m.span()[0]
  1308. break
  1309. # 第一个表格后有无内容
  1310. connect_flag2 = False
  1311. if table_start2 is not None and table_start2 == 0:
  1312. connect_flag2 = True
  1313. connect_flag_list.append([i, connect_flag2, connect_flag1])
  1314. # print('connect_flag_list', connect_flag_list)
  1315. # 根据条件1合并需连接页码,形成组
  1316. connect_pages_list = []
  1317. temp_list = []
  1318. for i, c in enumerate(connect_flag_list):
  1319. if temp_list and c[1]:
  1320. temp_list.append(c)
  1321. elif not temp_list and c[2]:
  1322. temp_list.append(c)
  1323. else:
  1324. if temp_list:
  1325. connect_pages_list.append(temp_list)
  1326. temp_list = []
  1327. connect_pages_list.append([c])
  1328. if temp_list:
  1329. connect_pages_list.append(temp_list)
  1330. # print('connect_pages_list', connect_pages_list)
  1331. # 判断条件2:判断组内列数是否相同
  1332. connect_pages_list2 = []
  1333. for c_list in connect_pages_list:
  1334. if len(c_list) == 1:
  1335. connect_pages_list2.append(c_list)
  1336. else:
  1337. col_cnt_list = []
  1338. for c in c_list:
  1339. soup = soup_list[c[0]]
  1340. table1 = soup.findAll('table')[-1]
  1341. table2 = soup.findAll('table')[0]
  1342. tr1 = table1.findAll('tr')
  1343. tr2 = table2.findAll('tr')
  1344. td1 = tr1[-1].findAll('td')
  1345. td2 = tr2[0].findAll('td')
  1346. col_cnt_list.append([len(td2), len(td1)])
  1347. new_c_list = [c_list[0]]
  1348. # print('col_cnt_list', col_cnt_list)
  1349. for i in range(len(col_cnt_list) - 1):
  1350. if col_cnt_list[i][1] != col_cnt_list[i + 1][0]:
  1351. connect_pages_list2.append(new_c_list)
  1352. new_c_list = [c_list[i + 1]]
  1353. else:
  1354. new_c_list.append(c_list[i + 1])
  1355. if new_c_list:
  1356. connect_pages_list2.append(new_c_list)
  1357. # print('connect_pages_list2', connect_pages_list2)
  1358. # 符合连接条件的拼接表格
  1359. new_html_list = []
  1360. for c_list in connect_pages_list2:
  1361. if len(c_list) == 1:
  1362. new_html_list.append(html_list[c_list[0][0]])
  1363. continue
  1364. new_html = ''
  1365. for c in c_list:
  1366. new_html += html_list[c[0]]
  1367. new_html = re.sub('</table>([-/第页0-9]|<div>|</div>)*<table border="1">', '<tr><td>#@#@#</td></tr>',
  1368. new_html)
  1369. soup = BeautifulSoup(new_html, 'lxml')
  1370. trs = soup.findAll('tr')
  1371. for i in range(len(trs)):
  1372. if trs[i].get_text() == '#@#@#':
  1373. td1 = trs[i - 1].findAll('td')
  1374. td2 = trs[i + 1].findAll('td')
  1375. if td2[0].get_text() == '':
  1376. for j in range(len(td1)):
  1377. td1[j].string = td1[j].get_text() + td2[j].get_text()
  1378. trs[i + 1].decompose()
  1379. trs[i].decompose()
  1380. new_html = str(soup)
  1381. new_html_list.append(new_html)
  1382. html_str = ''
  1383. for h in new_html_list:
  1384. html_str += h
  1385. return [html_str]
  1386. def get_html(self):
  1387. self.convert()
  1388. if self._doc.error_code is not None:
  1389. return self._doc.error_code
  1390. html = self._doc.get_html(return_list=True)
  1391. # 表格连接
  1392. try:
  1393. html = self.connect_table(html)
  1394. except:
  1395. traceback.print_exc()
  1396. return [-12]
  1397. return html
  1398. def delete_water_mark(self, lt_text_list, page_bbox, times=5):
  1399. # 删除过多重复字句,为水印
  1400. duplicate_dict = {}
  1401. for _obj in lt_text_list:
  1402. t = _obj.get_text()
  1403. if t in duplicate_dict.keys():
  1404. duplicate_dict[t][0] += 1
  1405. duplicate_dict[t][1].append(_obj)
  1406. else:
  1407. duplicate_dict[t] = [1, [_obj]]
  1408. delete_text = []
  1409. for t in duplicate_dict.keys():
  1410. if duplicate_dict[t][0] >= times:
  1411. obj_list = duplicate_dict[t][1]
  1412. obj_list.sort(key=lambda x: x.bbox[3])
  1413. obj_distance_h = abs(obj_list[-1].bbox[3] - obj_list[0].bbox[1])
  1414. obj_list.sort(key=lambda x: x.bbox[2])
  1415. obj_distance_w = abs(obj_list[-1].bbox[2] - obj_list[0].bbox[0])
  1416. if obj_distance_h >= abs(page_bbox[1] - page_bbox[3]) * 0.7 \
  1417. and obj_distance_w >= abs(page_bbox[0] - page_bbox[2]) * 0.7:
  1418. delete_text.append(t)
  1419. temp_text_list = []
  1420. for _obj in lt_text_list:
  1421. t = _obj.get_text()
  1422. if t not in delete_text:
  1423. temp_text_list.append(_obj)
  1424. return temp_text_list
  1425. def resize_image(self, img_path, max_size=2000):
  1426. _img = cv2.imread(img_path)
  1427. if _img.shape[0] <= max_size or _img.shape[1] <= max_size:
  1428. return
  1429. else:
  1430. resize_axis = 0 if _img.shape[0] >= _img.shape[1] else 1
  1431. ratio = max_size / _img.shape[resize_axis]
  1432. new_shape = [0, 0]
  1433. new_shape[resize_axis] = max_size
  1434. new_shape[1 - resize_axis] = int(_img.shape[1 - resize_axis] * ratio)
  1435. _img = cv2.resize(_img, (new_shape[1], new_shape[0]))
  1436. cv2.imwrite(img_path, _img)
  1437. def get_single_pdf(self, path, page_no):
  1438. log("into get_single_pdf")
  1439. try:
  1440. pdf_origin = copy.deepcopy(self.doc_pypdf2)
  1441. pdf_new = copy.deepcopy(self.doc_pypdf2_new)
  1442. pdf_new.addPage(pdf_origin.getPage(page_no))
  1443. path_new = path.split(".")[0] + "_split.pdf"
  1444. with open(path_new, "wb") as ff:
  1445. pdf_new.write(ff)
  1446. return path_new
  1447. except PyPDF2.utils.PdfReadError as e:
  1448. return [-3]
  1449. except Exception as e:
  1450. log("get_single_pdf error! page " + str(page_no))
  1451. return [-3]
  1452. def get_text_font():
  1453. def flags_decomposer(flags):
  1454. """Make font flags human readable."""
  1455. l = []
  1456. if flags & 2 ** 0:
  1457. l.append("superscript")
  1458. if flags & 2 ** 1:
  1459. l.append("italic")
  1460. if flags & 2 ** 2:
  1461. l.append("serifed")
  1462. else:
  1463. l.append("sans")
  1464. if flags & 2 ** 3:
  1465. l.append("monospaced")
  1466. else:
  1467. l.append("proportional")
  1468. if flags & 2 ** 4:
  1469. l.append("bold")
  1470. return ", ".join(l)
  1471. def get_underlined_textLines(page):
  1472. """
  1473. 获取某页pdf上的所有下划线文本信息
  1474. :param page: fitz中的一页
  1475. :return: list of tuples,每个tuple都是一个完整的下划线覆盖的整体:[(下划线句, 所在blk_no, 所在line_no), ...]
  1476. """
  1477. paths = page.get_drawings() # get drawings on the current page
  1478. # 获取该页内所有的height很小的bbox。因为下划线其实大多是这种矩形
  1479. # subselect things we may regard as lines
  1480. lines = []
  1481. for p in paths:
  1482. for item in p["items"]:
  1483. if item[0] == "l": # an actual line
  1484. p1, p2 = item[1:]
  1485. if p1.y == p2.y:
  1486. lines.append((p1, p2))
  1487. elif item[0] == "re": # a rectangle: check if height is small
  1488. r = item[1]
  1489. if r.width > r.height and r.height <= 2:
  1490. lines.append((r.tl, r.tr)) # take top left / right points
  1491. # 获取该页的`max_lineheight`,用于下面比较距离使用
  1492. blocks = page.get_text("dict", flags=11)["blocks"]
  1493. max_lineheight = 0
  1494. for b in blocks:
  1495. for l in b["lines"]:
  1496. bbox = fitz.Rect(l["bbox"])
  1497. if bbox.height > max_lineheight:
  1498. max_lineheight = bbox.height
  1499. underlined_res = []
  1500. # 开始对下划线内容进行查询
  1501. # make a list of words
  1502. words = page.get_text("words")
  1503. # if underlined, the bottom left / right of a word
  1504. # should not be too far away from left / right end of some line:
  1505. for wdx, w in enumerate(words): # w[4] is the actual word string
  1506. r = fitz.Rect(w[:4]) # first 4 items are the word bbox
  1507. for p1, p2 in lines: # check distances for start / end points
  1508. if abs(r.bl - p1) <= max_lineheight: # 当前word的左下满足下划线左下
  1509. if abs(r.br - p2) <= max_lineheight: # 当前word的右下满足下划线右下(单个词,无空格)
  1510. print(f"Word '{w[4]}' is underlined! Its block-line number is {w[-3], w[-2]}")
  1511. underlined_res.append((w[4], w[-3], w[-2])) # 分别是(下划线词,所在blk_no,所在line_no)
  1512. break # don't check more lines
  1513. else: # 继续寻找同line右侧的有缘人,因为有些下划线覆盖的词包含多个词,多个词之间有空格
  1514. curr_line_num = w[-2] # line nunmber
  1515. for right_wdx in range(wdx + 1, len(words), 1):
  1516. _next_w = words[right_wdx]
  1517. if _next_w[-2] != curr_line_num: # 当前遍历到的右侧word已经不是当前行的了(跨行是不行的)
  1518. break
  1519. _r_right = fitz.Rect(_next_w[:4]) # 获取当前同行右侧某word的方框4点
  1520. if abs(_r_right.br - p2) <= max_lineheight: # 用此word右下点和p2(目标下划线右上点)算距离,距离要小于max_lineheight
  1521. print(
  1522. f"Word '{' '.join([_one_word[4] for _one_word in words[wdx:right_wdx + 1]])}' is underlined! " +
  1523. f"Its block-line number is {w[-3], w[-2]}")
  1524. underlined_res.append(
  1525. (' '.join([_one_word[4] for _one_word in words[wdx:right_wdx + 1]]),
  1526. w[-3], w[-2])
  1527. ) # 分别是(下划线词,所在blk_no,所在line_no)
  1528. break # don't check more lines
  1529. return underlined_res
  1530. _p = r'C:\Users\Administrator\Desktop\test_pdf\error2-2.pdf'
  1531. doc_pymupdf = read_pymupdf(_p)
  1532. page = doc_pymupdf[0]
  1533. blocks = page.get_text("dict", flags=11)["blocks"]
  1534. for b in blocks: # iterate through the text blocks
  1535. for l in b["lines"]: # iterate through the text lines
  1536. for s in l["spans"]: # iterate through the text spans
  1537. print("")
  1538. font_properties = "Font: '%s' (%s), size %g, color #%06x" % (
  1539. s["font"], # font name
  1540. flags_decomposer(s["flags"]), # readable font flags
  1541. s["size"], # font size
  1542. s["color"], # font color
  1543. )
  1544. print(s)
  1545. print("Text: '%s'" % s["text"]) # simple print of text
  1546. print(font_properties)
  1547. get_underlined_textLines(page)
  1548. # 以下为现成pdf单页解析接口
  1549. class ParseSentence:
  1550. def __init__(self, bbox, fontname, fontsize, _text, _title, title_text, _pattern, title_degree, is_outline,
  1551. outline_location, page_no):
  1552. (x0, y0, x1, y1) = bbox
  1553. self.x0 = x0
  1554. self.y0 = y0
  1555. self.x1 = x1
  1556. self.y1 = y1
  1557. self.bbox = bbox
  1558. self.fontname = fontname
  1559. self.fontsize = fontsize
  1560. self.text = _text
  1561. self.title = _title
  1562. self.title_text = title_text
  1563. self.groups = _pattern
  1564. self.title_degree = title_degree
  1565. self.is_outline = is_outline
  1566. self.outline_location = outline_location
  1567. self.page_no = page_no
  1568. def __repr__(self):
  1569. return "%s,%s,%s,%d,%s" % (self.text, self.title, self.is_outline, self.outline_location, str(self.bbox))
  1570. class ParseUtils:
  1571. @staticmethod
  1572. def getFontinfo(_page):
  1573. for _obj in _page._objs:
  1574. if isinstance(_obj, (LTTextBoxHorizontal, LTTextBoxVertical)):
  1575. for textline in _obj._objs:
  1576. done = False
  1577. for lchar in textline._objs:
  1578. if isinstance(lchar, (LTChar)):
  1579. _obj.fontname = lchar.fontname
  1580. _obj.fontsize = lchar.size
  1581. done = True
  1582. break
  1583. if done:
  1584. break
  1585. @staticmethod
  1586. def recognize_sentences(list_textbox, filter_objs, page_bbox, page_no,
  1587. remove_space=True, sourceP_LB=True):
  1588. list_textbox.sort(key=lambda x: x.bbox[0])
  1589. list_textbox.sort(key=lambda x: x.bbox[3], reverse=sourceP_LB)
  1590. cluster_textbox = []
  1591. for _textbox in list_textbox:
  1592. if _textbox in filter_objs:
  1593. continue
  1594. _find = False
  1595. for _ct in cluster_textbox:
  1596. if abs(_ct["y"] - _textbox.bbox[1]) < 5:
  1597. _find = True
  1598. _ct["textbox"].append(_textbox)
  1599. if not _find:
  1600. cluster_textbox.append({"y": _textbox.bbox[1], "textbox": [_textbox]})
  1601. cluster_textbox.sort(key=lambda x: x["y"], reverse=sourceP_LB)
  1602. list_sentences = []
  1603. for _line in cluster_textbox:
  1604. _textboxs = _line["textbox"]
  1605. _textboxs.sort(key=lambda x: x.bbox[0])
  1606. _linetext = _textboxs[0].get_text()
  1607. for _i in range(1, len(_textboxs)):
  1608. if abs(_textboxs[_i].bbox[0] - _textboxs[_i - 1].bbox[2]) > 60:
  1609. if _linetext[-1] not in (",", ",", "。", ".", "、", ";"):
  1610. _linetext += "=,="
  1611. _linetext += _textboxs[_i].get_text()
  1612. _linetext = re.sub("[\s\r\n]", "", _linetext)
  1613. _bbox = (_textboxs[0].bbox[0], _textboxs[0].bbox[1],
  1614. _textboxs[-1].bbox[2], _textboxs[-1].bbox[3])
  1615. _title = None
  1616. _pattern_groups = None
  1617. title_text = ""
  1618. if not _title:
  1619. _groups = ParseUtils.find_title_by_pattern(_textboxs[0].get_text())
  1620. if _groups:
  1621. _title = _groups[0][0]
  1622. title_text = _groups[0][1]
  1623. _pattern_groups = _groups
  1624. if not _title:
  1625. _groups = ParseUtils.find_title_by_pattern(_linetext)
  1626. if _groups:
  1627. _title = _groups[0][0]
  1628. title_text = _groups[0][1]
  1629. _pattern_groups = _groups
  1630. if not _title:
  1631. _title = ParseUtils.rec_incenter(_bbox, page_bbox)
  1632. title_degree = 2
  1633. if not _title:
  1634. _linetext = _linetext.replace("=,=", ",")
  1635. else:
  1636. _linetext = _linetext.replace("=,=", "")
  1637. title_degree = int(_title.split("_")[1])
  1638. # 页码
  1639. if ParseUtils.rec_incenter(_bbox, page_bbox) and re.search("^\d+$", _linetext) is not None:
  1640. continue
  1641. if _linetext == "" or re.search("^,+$", _linetext) is not None:
  1642. continue
  1643. is_outline = False
  1644. outline_location = -1
  1645. _search = re.search("(?P<text>.+?)\.{5,}(?P<nums>\d+)$", _linetext)
  1646. if _search is not None:
  1647. is_outline = True
  1648. _linetext = _search.group("text")
  1649. outline_location = int(_search.group("nums"))
  1650. list_sentences.append(
  1651. ParseSentence(_bbox, _textboxs[-1].__dict__.get("fontname"), _textboxs[-1].__dict__.get("fontsize"),
  1652. _linetext, _title, title_text, _pattern_groups, title_degree, is_outline,
  1653. outline_location, page_no))
  1654. # for _sen in list_sentences:
  1655. # print(_sen.__dict__)
  1656. return list_sentences
  1657. @staticmethod
  1658. def find_title_by_pattern(_text,
  1659. _pattern="(?P<title_1>(?P<title_1_index_0_0>^第?)(?P<title_1_index_1_1>[一二三四五六七八九十ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)(?P<title_1_index_2_0>[、章]))|" \
  1660. "(?P<title_3>^(?P<title_3_index_0_1>[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+))|" \
  1661. "(?P<title_4>^(?P<title_4_index_0_0>第?)(?P<title_4_index_1_1>[一二三四五六七八九十]+)(?P<title_4_index_2_0>[节]))|" \
  1662. "(?P<title_11>^(?P<title_11_index_0_0>\d{1,2}[\..、\s\-]\d{1,2}[\..、\s\-]\d{1,2}[\..、\s\-])(?P<title_11_index_1_1>\d{1,2})(?P<title_11_index_2_0>[\..、\s\-]))|" \
  1663. "(?P<title_10>^(?P<title_10_index_0_0>\d{1,2}[\..、\s\-]\d{1,2}[\..、\s\-])(?P<title_10_index_1_1>\d{1,2})(?P<title_10_index_2_0>[\..、\s\-]))|" \
  1664. "(?P<title_7>^(?P<title_7_index_0_0>\d{1,2}[\..、\s\-])(?P<title_7_index_1_1>\d{1,2})(?P<title_7_index_2_0>[\..、\s\-]))|" \
  1665. "(?P<title_6>^(?P<title_6_index_0_1>\d{1,2})(?P<title_6_index_1_0>[\..、\s\-]))|" \
  1666. "(?P<title_15>^(?P<title_15_index_0_0>(?)(?P<title_15_index_1_1>\d{1,2})(?P<title_15_index_2_0>)))|" \
  1667. "(?P<title_17>^(?P<title_17_index_0_0>(?)(?P<title_17_index_1_1>[a-zA-Z]+)(?P<title_17_index_2_0>)))|"
  1668. "(?P<title_19>^(?P<title_19_index_0_0>(?)(?P<title_19_index_1_1>[一二三四五六七八九十ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)(?P<title_19_index_2_0>)))|" \
  1669. ):
  1670. _se = re.search(_pattern, _text)
  1671. groups = []
  1672. if _se is not None:
  1673. _gd = _se.groupdict()
  1674. for k, v in _gd.items():
  1675. if v is not None:
  1676. groups.append((k, v))
  1677. if len(groups):
  1678. groups.sort(key=lambda x: x[0])
  1679. return groups
  1680. return None
  1681. @staticmethod
  1682. def rec_incenter(o_bbox, p_bbox):
  1683. p_width = p_bbox[2] - p_bbox[0]
  1684. l_space = (o_bbox[0] - p_bbox[0]) / p_width
  1685. r_space = (p_bbox[2] - o_bbox[2]) / p_width
  1686. if abs((l_space - r_space)) < 0.1 and l_space > 0.2:
  1687. return "title_2"
  1688. @staticmethod
  1689. def is_first_title(_title):
  1690. if _title is None:
  1691. return False
  1692. if re.search("^\d+$", _title) is not None:
  1693. if int(_title) == 1:
  1694. return True
  1695. return False
  1696. if re.search("^[一二三四五六七八九十百]+$", _title) is not None:
  1697. if _title == "一":
  1698. return True
  1699. return False
  1700. if re.search("^[a-z]+$", _title) is not None:
  1701. if _title == "a":
  1702. return True
  1703. return False
  1704. if re.search("^[A-Z]+$", _title) is not None:
  1705. if _title == "A":
  1706. return True
  1707. return False
  1708. if re.search("^[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]$", _title) is not None:
  1709. if _title == "Ⅰ":
  1710. return True
  1711. return False
  1712. return False
  1713. @staticmethod
  1714. def get_next_title(_title):
  1715. if re.search("^\d+$", _title) is not None:
  1716. return str(int(_title) + 1)
  1717. if re.search("^[一二三四五六七八九十百]+$", _title) is not None:
  1718. _next_title = ParseUtils.make_increase(['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'],
  1719. re.sub("[十百]", '', _title))
  1720. _next_title = list(_next_title)
  1721. _next_title.reverse()
  1722. if _next_title[-1] != "十":
  1723. if len(_next_title) >= 2:
  1724. _next_title.insert(-1, '十')
  1725. if len(_next_title) >= 4:
  1726. _next_title.insert(-3, '百')
  1727. if _title[0] == "十":
  1728. if _next_title == "十":
  1729. _next_title = ["二", "十"]
  1730. _next_title.insert(0, "十")
  1731. _next_title = "".join(_next_title)
  1732. return _next_title
  1733. if re.search("^[a-z]+$", _title) is not None:
  1734. _next_title = ParseUtils.make_increase([chr(i + ord('a')) for i in range(26)], _title)
  1735. _next_title = list(_next_title)
  1736. _next_title.reverse()
  1737. return "".join(_next_title)
  1738. if re.search("^[A-Z]+$", _title) is not None:
  1739. _next_title = ParseUtils.make_increase([chr(i + ord('A')) for i in range(26)], _title)
  1740. _next_title = list(_next_title)
  1741. _next_title.reverse()
  1742. return "".join(_next_title)
  1743. if re.search("^[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]$", _title) is not None:
  1744. _sort = ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"]
  1745. _index = _sort.index(_title)
  1746. if _index < len(_sort) - 1:
  1747. return _sort[_index + 1]
  1748. return None
  1749. @staticmethod
  1750. def make_increase(_sort, _title, _add=1):
  1751. if len(_title) == 0 and _add == 0:
  1752. return ""
  1753. if len(_title) == 0 and _add == 1:
  1754. return _sort[0]
  1755. _index = _sort.index(_title[-1])
  1756. next_index = (_index + _add) % len(_sort)
  1757. next_chr = _sort[next_index]
  1758. if _index == len(_sort) - 1:
  1759. _add = 1
  1760. else:
  1761. _add = 0
  1762. return next_chr + ParseUtils.make_increase(_sort, _title[:-1], _add)
  1763. @staticmethod
  1764. def rec_serial(_text, o_bbox, p_bbox, fontname, _pattern="(?P<title_1>^[一二三四五六七八九十]+[、])|" \
  1765. "(?P<title_2>^\d+[\.、\s])|" \
  1766. "(?P<title_3>^\d+\.\d+[\.、\s])|" \
  1767. "(?P<title_4>^\d+\.\d+\.\d+[\.、\s])|" \
  1768. "(?P<title_5>^\d+\.\d+\.\d+\.\d+[\.、\s])"):
  1769. # todo :recog the serial of the sentence
  1770. _se = re.search(_pattern, _text)
  1771. if _se is not None:
  1772. _gd = _se.groupdict()
  1773. for k, v in _gd.items():
  1774. if v is not None:
  1775. return k
  1776. return None
  1777. if __name__ == '__main__':
  1778. # get_text_font()
  1779. PDFConvert(r"C:/Users/Administrator/Downloads/1651896704621.pdf", "C:/Users/Administrator/Downloads/1").get_html()
  1780. # print(b'\x10')