convert_pdf.py 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076
  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 judge_b_table(self, lt_text_list):
  1075. # 先分行
  1076. lt_text_list.sort(key=lambda x: (x.bbox[1], x.bbox[0]))
  1077. lt_text_row_list = []
  1078. current_h = lt_text_list[0].bbox[1]
  1079. row = []
  1080. threshold = 2
  1081. for lt_text in lt_text_list:
  1082. bbox = lt_text.bbox
  1083. if current_h - threshold <= bbox[1] <= current_h + threshold:
  1084. row.append(lt_text)
  1085. else:
  1086. if row:
  1087. lt_text_row_list.append(row)
  1088. row = [lt_text]
  1089. current_h = lt_text.bbox[1]
  1090. if row:
  1091. lt_text_row_list.append(row)
  1092. # print('lt_text_row_list')
  1093. # for r in lt_text_row_list:
  1094. # print('r', [x.get_text() for x in r])
  1095. # 判断文本中间是否是空格,或一行文本中间有多个
  1096. is_b_table_flag = False
  1097. is_b_table_cnt = 3
  1098. tolerate_cnt = 2
  1099. t_cnt = 0
  1100. row_cnt = 0
  1101. for row in lt_text_row_list:
  1102. # 水印行跳过
  1103. if len(row) == 1 and len(row[0].get_text()[:-1]) == 1:
  1104. continue
  1105. if len(row) == 1:
  1106. text = row[0].get_text()
  1107. bbox = row[0].bbox
  1108. match = re.search('[ ]{3,}', text)
  1109. if match and re.search('[\u4e00-\u9fff]{2,}', text[:match.span()[0]]) \
  1110. and re.search('[\u4e00-\u9fff]{2,}', text[match.span()[1]:]):
  1111. row_cnt += 1
  1112. t_cnt = 0
  1113. else:
  1114. # 容忍
  1115. if t_cnt < tolerate_cnt:
  1116. t_cnt += 1
  1117. continue
  1118. row_cnt = 0
  1119. else:
  1120. row_cnt += 1
  1121. t_cnt = 0
  1122. if row_cnt >= is_b_table_cnt:
  1123. is_b_table_flag = True
  1124. break
  1125. log('pdf is_b_table_flag ' + str(is_b_table_flag))
  1126. return is_b_table_flag
  1127. def convert_page(self, page, page_no):
  1128. # pdf page.annots为None,不经过get_layout,直接ocr
  1129. # if page.annots is None:
  1130. # lt_image_list = []
  1131. # lt_text_list = []
  1132. # # 设置只有图片,可跳到ocr
  1133. # only_image = 1
  1134. # image_count = 1
  1135. # else:
  1136. layout = self.get_layout(page, page_no)
  1137. if self._doc.error_code is not None:
  1138. return
  1139. if judge_error_code(layout):
  1140. self._page.error_code = layout
  1141. return
  1142. # 判断该页的对象类型,并存储
  1143. # only_image = 1
  1144. # image_count = 0
  1145. lt_text_list = []
  1146. lt_image_list = []
  1147. for x in layout:
  1148. if isinstance(x, (LTTextBoxHorizontal, LTTextBoxVertical)):
  1149. # only_image = 0
  1150. lt_text_list.append(x)
  1151. if isinstance(x, LTFigure):
  1152. for y in x:
  1153. if isinstance(y, LTImage):
  1154. lt_image_list.append(y)
  1155. # image_count += 1
  1156. lt_text_list = self.delete_water_mark(lt_text_list, layout.bbox, 15)
  1157. log("convert_pdf page " + str(page_no))
  1158. log("len(lt_image_list), len(lt_text_list) " + str(len(lt_image_list)) + " " + str(len(lt_text_list)))
  1159. # 若只有文本且图片数为0,直接提取文字及表格
  1160. # if only_image == 0 and image_count == 0:
  1161. if len(lt_image_list) == 0 and len(lt_text_list) > 0:
  1162. # PDFPlumber
  1163. if self.has_init_pdf[3] == 0:
  1164. self.init_package("pdfplumber")
  1165. if self._doc.error_code is not None:
  1166. self._doc.error_code = None
  1167. log("init pdfplumber failed! try pymupdf...")
  1168. # 调用pdfplumber获取pdf图片报错,则使用pypdf2将pdf转html
  1169. page_image = self.get_page_image(page_no)
  1170. if judge_error_code(page_image):
  1171. self._page.error_code = page_image
  1172. else:
  1173. _image = _Image(page_image[1], page_image[0])
  1174. self._page.add_child(_image)
  1175. return
  1176. if not self.is_text_legal(lt_text_list, page_no):
  1177. return
  1178. # 根据text规律,判断该页是否可能有无边框表格
  1179. start_time = time.time()
  1180. if self.judge_b_table(lt_text_list):
  1181. page_image = self.get_page_image(page_no)
  1182. if judge_error_code(page_image):
  1183. self._page.error_code = page_image
  1184. else:
  1185. _image = _Image(page_image[1], page_image[0])
  1186. _image.is_from_pdf = True
  1187. _image.b_table_from_text = True
  1188. _image.b_table_text_obj_list = lt_text_list
  1189. _image.b_table_layout_size = (layout.width, layout.height)
  1190. self._page.add_child(_image)
  1191. log('convert_pdf judge_b_table set image cost: ' + str(time.time()-start_time))
  1192. try:
  1193. lt_line_list = self.get_page_lines(layout, page_no)
  1194. except:
  1195. traceback.print_exc()
  1196. lt_line_list = []
  1197. self._page.error_code = [-13]
  1198. try:
  1199. # lt_line_list = self.get_text_lines(page,page_no)
  1200. self.recognize_text(layout, page_no, lt_text_list, lt_line_list)
  1201. except:
  1202. traceback.print_exc()
  1203. self._page.error_code = [-8]
  1204. # 若该页图片数量过多,或无文本,则直接ocr整页识别
  1205. # elif image_count > 3 or only_image == 1:
  1206. elif len(lt_image_list) > 3 or len(lt_text_list) == 0:
  1207. page_image = self.get_page_image(page_no)
  1208. if judge_error_code(page_image):
  1209. self._page.error_code = page_image
  1210. else:
  1211. _image = _Image(page_image[1], page_image[0])
  1212. _image.is_from_pdf = True
  1213. self._page.add_child(_image)
  1214. # 正常读取该页对象
  1215. else:
  1216. # 图表对象
  1217. for image in lt_image_list:
  1218. try:
  1219. # print("pdf2text LTImage size", page_no, image.width, image.height)
  1220. image_stream = image.stream.get_data()
  1221. # 小的图忽略
  1222. if image.width <= 300 and image.height <= 300:
  1223. continue
  1224. # 查看提取的图片高宽,太大则用pdf输出图进行ocr识别
  1225. img_test = Image.open(io.BytesIO(image_stream))
  1226. # img_test.show()
  1227. if image.height >= 1000 and image.width >= 1000:
  1228. # print("pdf2text LTImage stream output size", img_test.size)
  1229. page_image = self.get_page_image(page_no)
  1230. if judge_error_code(page_image):
  1231. self._page.error_code = page_image
  1232. else:
  1233. _image = _Image(page_image[1], page_image[0])
  1234. _image.is_from_pdf = True
  1235. self._page.add_child(_image)
  1236. return
  1237. # 比较小的图则直接保存用ocr识别
  1238. else:
  1239. temp_path = self.unique_type_dir + 'page' + str(page_no) \
  1240. + '_lt' + str(lt_image_list.index(image)) + '.jpg'
  1241. img_test.save(temp_path)
  1242. with open(temp_path, "rb") as ff:
  1243. image_stream = ff.read()
  1244. _image = _Image(image_stream, temp_path, image.bbox)
  1245. self._page.add_child(_image)
  1246. except Exception:
  1247. log("pdf2text pdfminer read image in page " + str(page_no) +
  1248. " fail! use pymupdf read image...")
  1249. traceback.print_exc()
  1250. # pdf对象需反向排序
  1251. self._page.is_reverse = True
  1252. self.init_package("pdfplumber")
  1253. if not self.is_text_legal(lt_text_list, page_no):
  1254. return
  1255. # 根据text规律,判断该页是否可能有无边框表格
  1256. if self.judge_b_table(lt_text_list):
  1257. page_image = self.get_page_image(page_no)
  1258. if judge_error_code(page_image):
  1259. self._page.error_code = page_image
  1260. else:
  1261. _image = _Image(page_image[1], page_image[0])
  1262. _image.is_from_pdf = True
  1263. self._page.add_child(_image)
  1264. # lt_line_list = self.get_text_lines(page, page_no)
  1265. try:
  1266. lt_line_list = self.get_page_lines(layout, page_no)
  1267. except:
  1268. traceback.print_exc()
  1269. lt_line_list = []
  1270. self._page.error_code = [-13]
  1271. self.recognize_text(layout, page_no, lt_text_list, lt_line_list)
  1272. def get_layout(self, page, page_no):
  1273. log("get_layout")
  1274. if self.has_init_pdf[0] == 0:
  1275. self.init_package("pdfminer")
  1276. if self._doc.error_code is not None:
  1277. return
  1278. # 获取该页layout
  1279. start_time = time.time()
  1280. try:
  1281. if get_platform() == "Windows":
  1282. # origin_pdf_analyze = pdf_analyze.__wrapped__
  1283. # layout = origin_pdf_analyze(self.interpreter, page, self.device)
  1284. layout = pdf_analyze(self.interpreter, page, self.device, page_no)
  1285. else:
  1286. layout = pdf_analyze(self.interpreter, page, self.device, page_no)
  1287. except TimeoutError as e:
  1288. log("pdf2text pdfminer read pdf page " + str(page_no) + " time out! " + str(time.time() - start_time))
  1289. layout = [-4]
  1290. except Exception:
  1291. traceback.print_exc()
  1292. log("pdf2text pdfminer read pdf page " + str(page_no) + " error! continue...")
  1293. layout = [-3]
  1294. return layout
  1295. def get_page_image(self, page_no):
  1296. log("")
  1297. try:
  1298. if self.has_init_pdf[1] == 0:
  1299. self.init_package("PyMuPDF")
  1300. if self._doc.error_code is not None:
  1301. return
  1302. # save_dir = self.path.split(".")[-2] + "_" + self.path.split(".")[-1]
  1303. output = self.unique_type_dir + "page" + str(page_no) + ".png"
  1304. page = self.doc_pymupdf.loadPage(page_no)
  1305. rotate = int(0)
  1306. zoom_x = 2.
  1307. zoom_y = 2.
  1308. mat = fitz.Matrix(zoom_x, zoom_y).preRotate(rotate)
  1309. pix = page.getPixmap(matrix=mat, alpha=False)
  1310. pix.writePNG(output)
  1311. # 输出图片resize
  1312. self.resize_image(output)
  1313. with open(output, "rb") as f:
  1314. pdf_image = f.read()
  1315. return [output, pdf_image]
  1316. except ValueError as e:
  1317. traceback.print_exc()
  1318. if str(e) == "page not in document":
  1319. log("pdf2Image page not in document! continue... page " + str(page_no))
  1320. return [0]
  1321. elif "encrypted" in str(e):
  1322. log("pdf2Image document need password " + str(page_no))
  1323. return [-7]
  1324. except RuntimeError as e:
  1325. if "cannot find page" in str(e):
  1326. log("pdf2Image page {} not in document! continue... ".format(str(page_no)) + str(e))
  1327. return [0]
  1328. else:
  1329. traceback.print_exc()
  1330. return [-3]
  1331. def get_all_page_image(self):
  1332. log("")
  1333. if self.has_init_pdf[1] == 0:
  1334. self.init_package("PyMuPDF")
  1335. if self._doc.error_code is not None:
  1336. return
  1337. page_count = self.doc_pymupdf.page_count
  1338. for page_no in range(page_count):
  1339. # 限制pdf页数,只取前10页后10页
  1340. if page_count > 20:
  1341. if 10 <= page_no < page_count - 10:
  1342. continue
  1343. self._page = _Page(None, page_no)
  1344. page_image = self.get_page_image(page_no)
  1345. if judge_error_code(page_image):
  1346. self._page.error_code = page_image
  1347. else:
  1348. _image = _Image(page_image[1], page_image[0])
  1349. self._page.add_child(_image)
  1350. # 报错继续读后面页面
  1351. if self._doc.error_code is None and self._page.error_code is not None:
  1352. continue
  1353. self._doc.add_child(self._page)
  1354. def connect_table(self, html_list):
  1355. if not html_list:
  1356. return html_list
  1357. # 判断条件1:最后一个表格后有无非页码文本/第一个表格前有无文本
  1358. connect_flag_list = []
  1359. soup_list = []
  1360. for i, h in enumerate(html_list):
  1361. soup_list.append(BeautifulSoup(h, 'lxml'))
  1362. # 找最后一个表格
  1363. table_start1, table_end1 = None, None
  1364. # print('h', h)
  1365. match = re.finditer('<table', h)
  1366. for m in match:
  1367. table_start1 = m.span()[0]
  1368. if table_start1 is not None:
  1369. match = re.finditer('</table>', h[table_start1:])
  1370. for m in match:
  1371. table_end1 = m.span()[1] + table_start1
  1372. # 最后一个表格后有无除了页码外的内容
  1373. connect_flag1 = False
  1374. if table_end1 is not None:
  1375. match = re.search('[^-/第页0-9]*', re.sub('<div>|</div>', '', h[table_end1:]))
  1376. # print('match1', match.group())
  1377. if not match or match.group() == '':
  1378. connect_flag1 = True
  1379. # 找第一个表格
  1380. table_start2, table_end2 = None, None
  1381. match = re.finditer('<table', h)
  1382. for m in match:
  1383. table_start2 = m.span()[0]
  1384. break
  1385. # 第一个表格后有无内容
  1386. connect_flag2 = False
  1387. if table_start2 is not None and table_start2 == 0:
  1388. connect_flag2 = True
  1389. connect_flag_list.append([i, connect_flag2, connect_flag1])
  1390. # print('connect_flag_list', connect_flag_list)
  1391. # 根据条件1合并需连接页码,形成组
  1392. connect_pages_list = []
  1393. temp_list = []
  1394. for i, c in enumerate(connect_flag_list):
  1395. if temp_list and c[1]:
  1396. temp_list.append(c)
  1397. elif not temp_list and c[2]:
  1398. temp_list.append(c)
  1399. else:
  1400. if temp_list:
  1401. connect_pages_list.append(temp_list)
  1402. temp_list = []
  1403. connect_pages_list.append([c])
  1404. if temp_list:
  1405. connect_pages_list.append(temp_list)
  1406. # print('connect_pages_list', connect_pages_list)
  1407. # 判断条件2:判断组内列数是否相同
  1408. connect_pages_list2 = []
  1409. for c_list in connect_pages_list:
  1410. if len(c_list) == 1:
  1411. connect_pages_list2.append(c_list)
  1412. else:
  1413. col_cnt_list = []
  1414. for c in c_list:
  1415. soup = soup_list[c[0]]
  1416. table1 = soup.findAll('table')[-1]
  1417. table2 = soup.findAll('table')[0]
  1418. tr1 = table1.findAll('tr')
  1419. tr2 = table2.findAll('tr')
  1420. td1 = tr1[-1].findAll('td')
  1421. td2 = tr2[0].findAll('td')
  1422. col_cnt_list.append([len(td2), len(td1)])
  1423. new_c_list = [c_list[0]]
  1424. # print('col_cnt_list', col_cnt_list)
  1425. for i in range(len(col_cnt_list) - 1):
  1426. if col_cnt_list[i][1] != col_cnt_list[i + 1][0]:
  1427. connect_pages_list2.append(new_c_list)
  1428. new_c_list = [c_list[i + 1]]
  1429. else:
  1430. new_c_list.append(c_list[i + 1])
  1431. if new_c_list:
  1432. connect_pages_list2.append(new_c_list)
  1433. # print('connect_pages_list2', connect_pages_list2)
  1434. # 符合连接条件的拼接表格
  1435. new_html_list = []
  1436. for c_list in connect_pages_list2:
  1437. if len(c_list) == 1:
  1438. new_html_list.append(html_list[c_list[0][0]])
  1439. continue
  1440. new_html = ''
  1441. for c in c_list:
  1442. new_html += html_list[c[0]]
  1443. new_html = re.sub('</table>([-/第页0-9]|<div>|</div>)*<table border="1">', '<tr><td>#@#@#</td></tr>',
  1444. new_html)
  1445. soup = BeautifulSoup(new_html, 'lxml')
  1446. trs = soup.findAll('tr')
  1447. for i in range(len(trs)):
  1448. if trs[i].get_text() == '#@#@#':
  1449. td1 = trs[i - 1].findAll('td')
  1450. td2 = trs[i + 1].findAll('td')
  1451. if td2[0].get_text() == '':
  1452. for j in range(len(td1)):
  1453. td1[j].string = td1[j].get_text() + td2[j].get_text()
  1454. trs[i + 1].decompose()
  1455. trs[i].decompose()
  1456. new_html = str(soup)
  1457. new_html_list.append(new_html)
  1458. html_str = ''
  1459. for h in new_html_list:
  1460. html_str += h
  1461. return [html_str]
  1462. def get_html(self):
  1463. self.convert()
  1464. if self._doc.error_code is not None:
  1465. return self._doc.error_code
  1466. html = self._doc.get_html(return_list=True)
  1467. # 表格连接
  1468. try:
  1469. html = self.connect_table(html)
  1470. except:
  1471. traceback.print_exc()
  1472. return [-12]
  1473. return html
  1474. def delete_water_mark(self, lt_text_list, page_bbox, times=5):
  1475. # 删除过多重复字句,为水印
  1476. duplicate_dict = {}
  1477. for _obj in lt_text_list:
  1478. t = _obj.get_text()
  1479. if t in duplicate_dict.keys():
  1480. duplicate_dict[t][0] += 1
  1481. duplicate_dict[t][1].append(_obj)
  1482. else:
  1483. duplicate_dict[t] = [1, [_obj]]
  1484. delete_text = []
  1485. for t in duplicate_dict.keys():
  1486. if duplicate_dict[t][0] >= times:
  1487. obj_list = duplicate_dict[t][1]
  1488. obj_list.sort(key=lambda x: x.bbox[3])
  1489. obj_distance_h = abs(obj_list[-1].bbox[3] - obj_list[0].bbox[1])
  1490. obj_list.sort(key=lambda x: x.bbox[2])
  1491. obj_distance_w = abs(obj_list[-1].bbox[2] - obj_list[0].bbox[0])
  1492. if obj_distance_h >= abs(page_bbox[1] - page_bbox[3]) * 0.7 \
  1493. and obj_distance_w >= abs(page_bbox[0] - page_bbox[2]) * 0.7:
  1494. delete_text.append(t)
  1495. temp_text_list = []
  1496. for _obj in lt_text_list:
  1497. t = _obj.get_text()
  1498. if t not in delete_text:
  1499. temp_text_list.append(_obj)
  1500. return temp_text_list
  1501. def resize_image(self, img_path, max_size=2000):
  1502. _img = cv2.imread(img_path)
  1503. if _img.shape[0] <= max_size or _img.shape[1] <= max_size:
  1504. return
  1505. else:
  1506. resize_axis = 0 if _img.shape[0] >= _img.shape[1] else 1
  1507. ratio = max_size / _img.shape[resize_axis]
  1508. new_shape = [0, 0]
  1509. new_shape[resize_axis] = max_size
  1510. new_shape[1 - resize_axis] = int(_img.shape[1 - resize_axis] * ratio)
  1511. _img = cv2.resize(_img, (new_shape[1], new_shape[0]))
  1512. cv2.imwrite(img_path, _img)
  1513. def get_single_pdf(self, path, page_no):
  1514. log("into get_single_pdf")
  1515. try:
  1516. pdf_origin = copy.deepcopy(self.doc_pypdf2)
  1517. pdf_new = copy.deepcopy(self.doc_pypdf2_new)
  1518. pdf_new.addPage(pdf_origin.getPage(page_no))
  1519. path_new = path.split(".")[0] + "_split.pdf"
  1520. with open(path_new, "wb") as ff:
  1521. pdf_new.write(ff)
  1522. return path_new
  1523. except PyPDF2.utils.PdfReadError as e:
  1524. return [-3]
  1525. except Exception as e:
  1526. log("get_single_pdf error! page " + str(page_no))
  1527. return [-3]
  1528. def get_text_font():
  1529. def flags_decomposer(flags):
  1530. """Make font flags human readable."""
  1531. l = []
  1532. if flags & 2 ** 0:
  1533. l.append("superscript")
  1534. if flags & 2 ** 1:
  1535. l.append("italic")
  1536. if flags & 2 ** 2:
  1537. l.append("serifed")
  1538. else:
  1539. l.append("sans")
  1540. if flags & 2 ** 3:
  1541. l.append("monospaced")
  1542. else:
  1543. l.append("proportional")
  1544. if flags & 2 ** 4:
  1545. l.append("bold")
  1546. return ", ".join(l)
  1547. def get_underlined_textLines(page):
  1548. """
  1549. 获取某页pdf上的所有下划线文本信息
  1550. :param page: fitz中的一页
  1551. :return: list of tuples,每个tuple都是一个完整的下划线覆盖的整体:[(下划线句, 所在blk_no, 所在line_no), ...]
  1552. """
  1553. paths = page.get_drawings() # get drawings on the current page
  1554. # 获取该页内所有的height很小的bbox。因为下划线其实大多是这种矩形
  1555. # subselect things we may regard as lines
  1556. lines = []
  1557. for p in paths:
  1558. for item in p["items"]:
  1559. if item[0] == "l": # an actual line
  1560. p1, p2 = item[1:]
  1561. if p1.y == p2.y:
  1562. lines.append((p1, p2))
  1563. elif item[0] == "re": # a rectangle: check if height is small
  1564. r = item[1]
  1565. if r.width > r.height and r.height <= 2:
  1566. lines.append((r.tl, r.tr)) # take top left / right points
  1567. # 获取该页的`max_lineheight`,用于下面比较距离使用
  1568. blocks = page.get_text("dict", flags=11)["blocks"]
  1569. max_lineheight = 0
  1570. for b in blocks:
  1571. for l in b["lines"]:
  1572. bbox = fitz.Rect(l["bbox"])
  1573. if bbox.height > max_lineheight:
  1574. max_lineheight = bbox.height
  1575. underlined_res = []
  1576. # 开始对下划线内容进行查询
  1577. # make a list of words
  1578. words = page.get_text("words")
  1579. # if underlined, the bottom left / right of a word
  1580. # should not be too far away from left / right end of some line:
  1581. for wdx, w in enumerate(words): # w[4] is the actual word string
  1582. r = fitz.Rect(w[:4]) # first 4 items are the word bbox
  1583. for p1, p2 in lines: # check distances for start / end points
  1584. if abs(r.bl - p1) <= max_lineheight: # 当前word的左下满足下划线左下
  1585. if abs(r.br - p2) <= max_lineheight: # 当前word的右下满足下划线右下(单个词,无空格)
  1586. print(f"Word '{w[4]}' is underlined! Its block-line number is {w[-3], w[-2]}")
  1587. underlined_res.append((w[4], w[-3], w[-2])) # 分别是(下划线词,所在blk_no,所在line_no)
  1588. break # don't check more lines
  1589. else: # 继续寻找同line右侧的有缘人,因为有些下划线覆盖的词包含多个词,多个词之间有空格
  1590. curr_line_num = w[-2] # line nunmber
  1591. for right_wdx in range(wdx + 1, len(words), 1):
  1592. _next_w = words[right_wdx]
  1593. if _next_w[-2] != curr_line_num: # 当前遍历到的右侧word已经不是当前行的了(跨行是不行的)
  1594. break
  1595. _r_right = fitz.Rect(_next_w[:4]) # 获取当前同行右侧某word的方框4点
  1596. if abs(_r_right.br - p2) <= max_lineheight: # 用此word右下点和p2(目标下划线右上点)算距离,距离要小于max_lineheight
  1597. print(
  1598. f"Word '{' '.join([_one_word[4] for _one_word in words[wdx:right_wdx + 1]])}' is underlined! " +
  1599. f"Its block-line number is {w[-3], w[-2]}")
  1600. underlined_res.append(
  1601. (' '.join([_one_word[4] for _one_word in words[wdx:right_wdx + 1]]),
  1602. w[-3], w[-2])
  1603. ) # 分别是(下划线词,所在blk_no,所在line_no)
  1604. break # don't check more lines
  1605. return underlined_res
  1606. _p = r'C:\Users\Administrator\Desktop\test_pdf\error2-2.pdf'
  1607. doc_pymupdf = read_pymupdf(_p)
  1608. page = doc_pymupdf[0]
  1609. blocks = page.get_text("dict", flags=11)["blocks"]
  1610. for b in blocks: # iterate through the text blocks
  1611. for l in b["lines"]: # iterate through the text lines
  1612. for s in l["spans"]: # iterate through the text spans
  1613. print("")
  1614. font_properties = "Font: '%s' (%s), size %g, color #%06x" % (
  1615. s["font"], # font name
  1616. flags_decomposer(s["flags"]), # readable font flags
  1617. s["size"], # font size
  1618. s["color"], # font color
  1619. )
  1620. print(s)
  1621. print("Text: '%s'" % s["text"]) # simple print of text
  1622. print(font_properties)
  1623. get_underlined_textLines(page)
  1624. # 以下为现成pdf单页解析接口
  1625. class ParseSentence:
  1626. def __init__(self, bbox, fontname, fontsize, _text, _title, title_text, _pattern, title_degree, is_outline,
  1627. outline_location, page_no):
  1628. (x0, y0, x1, y1) = bbox
  1629. self.x0 = x0
  1630. self.y0 = y0
  1631. self.x1 = x1
  1632. self.y1 = y1
  1633. self.bbox = bbox
  1634. self.fontname = fontname
  1635. self.fontsize = fontsize
  1636. self.text = _text
  1637. self.title = _title
  1638. self.title_text = title_text
  1639. self.groups = _pattern
  1640. self.title_degree = title_degree
  1641. self.is_outline = is_outline
  1642. self.outline_location = outline_location
  1643. self.page_no = page_no
  1644. def __repr__(self):
  1645. return "%s,%s,%s,%d,%s" % (self.text, self.title, self.is_outline, self.outline_location, str(self.bbox))
  1646. class ParseUtils:
  1647. @staticmethod
  1648. def getFontinfo(_page):
  1649. for _obj in _page._objs:
  1650. if isinstance(_obj, (LTTextBoxHorizontal, LTTextBoxVertical)):
  1651. for textline in _obj._objs:
  1652. done = False
  1653. for lchar in textline._objs:
  1654. if isinstance(lchar, (LTChar)):
  1655. _obj.fontname = lchar.fontname
  1656. _obj.fontsize = lchar.size
  1657. done = True
  1658. break
  1659. if done:
  1660. break
  1661. @staticmethod
  1662. def recognize_sentences(list_textbox, filter_objs, page_bbox, page_no,
  1663. remove_space=True, sourceP_LB=True):
  1664. list_textbox.sort(key=lambda x: x.bbox[0])
  1665. list_textbox.sort(key=lambda x: x.bbox[3], reverse=sourceP_LB)
  1666. cluster_textbox = []
  1667. for _textbox in list_textbox:
  1668. if _textbox in filter_objs:
  1669. continue
  1670. _find = False
  1671. for _ct in cluster_textbox:
  1672. if abs(_ct["y"] - _textbox.bbox[1]) < 5:
  1673. _find = True
  1674. _ct["textbox"].append(_textbox)
  1675. if not _find:
  1676. cluster_textbox.append({"y": _textbox.bbox[1], "textbox": [_textbox]})
  1677. cluster_textbox.sort(key=lambda x: x["y"], reverse=sourceP_LB)
  1678. list_sentences = []
  1679. for _line in cluster_textbox:
  1680. _textboxs = _line["textbox"]
  1681. _textboxs.sort(key=lambda x: x.bbox[0])
  1682. _linetext = _textboxs[0].get_text()
  1683. for _i in range(1, len(_textboxs)):
  1684. if abs(_textboxs[_i].bbox[0] - _textboxs[_i - 1].bbox[2]) > 60:
  1685. if _linetext[-1] not in (",", ",", "。", ".", "、", ";"):
  1686. _linetext += "=,="
  1687. _linetext += _textboxs[_i].get_text()
  1688. _linetext = re.sub("[\s\r\n]", "", _linetext)
  1689. _bbox = (_textboxs[0].bbox[0], _textboxs[0].bbox[1],
  1690. _textboxs[-1].bbox[2], _textboxs[-1].bbox[3])
  1691. _title = None
  1692. _pattern_groups = None
  1693. title_text = ""
  1694. if not _title:
  1695. _groups = ParseUtils.find_title_by_pattern(_textboxs[0].get_text())
  1696. if _groups:
  1697. _title = _groups[0][0]
  1698. title_text = _groups[0][1]
  1699. _pattern_groups = _groups
  1700. if not _title:
  1701. _groups = ParseUtils.find_title_by_pattern(_linetext)
  1702. if _groups:
  1703. _title = _groups[0][0]
  1704. title_text = _groups[0][1]
  1705. _pattern_groups = _groups
  1706. if not _title:
  1707. _title = ParseUtils.rec_incenter(_bbox, page_bbox)
  1708. title_degree = 2
  1709. if not _title:
  1710. _linetext = _linetext.replace("=,=", ",")
  1711. else:
  1712. _linetext = _linetext.replace("=,=", "")
  1713. title_degree = int(_title.split("_")[1])
  1714. # 页码
  1715. if ParseUtils.rec_incenter(_bbox, page_bbox) and re.search("^\d+$", _linetext) is not None:
  1716. continue
  1717. if _linetext == "" or re.search("^,+$", _linetext) is not None:
  1718. continue
  1719. is_outline = False
  1720. outline_location = -1
  1721. _search = re.search("(?P<text>.+?)\.{5,}(?P<nums>\d+)$", _linetext)
  1722. if _search is not None:
  1723. is_outline = True
  1724. _linetext = _search.group("text")
  1725. outline_location = int(_search.group("nums"))
  1726. list_sentences.append(
  1727. ParseSentence(_bbox, _textboxs[-1].__dict__.get("fontname"), _textboxs[-1].__dict__.get("fontsize"),
  1728. _linetext, _title, title_text, _pattern_groups, title_degree, is_outline,
  1729. outline_location, page_no))
  1730. # for _sen in list_sentences:
  1731. # print(_sen.__dict__)
  1732. return list_sentences
  1733. @staticmethod
  1734. def find_title_by_pattern(_text,
  1735. _pattern="(?P<title_1>(?P<title_1_index_0_0>^第?)(?P<title_1_index_1_1>[一二三四五六七八九十ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)(?P<title_1_index_2_0>[、章]))|" \
  1736. "(?P<title_3>^(?P<title_3_index_0_1>[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+))|" \
  1737. "(?P<title_4>^(?P<title_4_index_0_0>第?)(?P<title_4_index_1_1>[一二三四五六七八九十]+)(?P<title_4_index_2_0>[节]))|" \
  1738. "(?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\-]))|" \
  1739. "(?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\-]))|" \
  1740. "(?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\-]))|" \
  1741. "(?P<title_6>^(?P<title_6_index_0_1>\d{1,2})(?P<title_6_index_1_0>[\..、\s\-]))|" \
  1742. "(?P<title_15>^(?P<title_15_index_0_0>(?)(?P<title_15_index_1_1>\d{1,2})(?P<title_15_index_2_0>)))|" \
  1743. "(?P<title_17>^(?P<title_17_index_0_0>(?)(?P<title_17_index_1_1>[a-zA-Z]+)(?P<title_17_index_2_0>)))|"
  1744. "(?P<title_19>^(?P<title_19_index_0_0>(?)(?P<title_19_index_1_1>[一二三四五六七八九十ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]+)(?P<title_19_index_2_0>)))|" \
  1745. ):
  1746. _se = re.search(_pattern, _text)
  1747. groups = []
  1748. if _se is not None:
  1749. _gd = _se.groupdict()
  1750. for k, v in _gd.items():
  1751. if v is not None:
  1752. groups.append((k, v))
  1753. if len(groups):
  1754. groups.sort(key=lambda x: x[0])
  1755. return groups
  1756. return None
  1757. @staticmethod
  1758. def rec_incenter(o_bbox, p_bbox):
  1759. p_width = p_bbox[2] - p_bbox[0]
  1760. l_space = (o_bbox[0] - p_bbox[0]) / p_width
  1761. r_space = (p_bbox[2] - o_bbox[2]) / p_width
  1762. if abs((l_space - r_space)) < 0.1 and l_space > 0.2:
  1763. return "title_2"
  1764. @staticmethod
  1765. def is_first_title(_title):
  1766. if _title is None:
  1767. return False
  1768. if re.search("^\d+$", _title) is not None:
  1769. if int(_title) == 1:
  1770. return True
  1771. return False
  1772. if re.search("^[一二三四五六七八九十百]+$", _title) is not None:
  1773. if _title == "一":
  1774. return True
  1775. return False
  1776. if re.search("^[a-z]+$", _title) is not None:
  1777. if _title == "a":
  1778. return True
  1779. return False
  1780. if re.search("^[A-Z]+$", _title) is not None:
  1781. if _title == "A":
  1782. return True
  1783. return False
  1784. if re.search("^[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]$", _title) is not None:
  1785. if _title == "Ⅰ":
  1786. return True
  1787. return False
  1788. return False
  1789. @staticmethod
  1790. def get_next_title(_title):
  1791. if re.search("^\d+$", _title) is not None:
  1792. return str(int(_title) + 1)
  1793. if re.search("^[一二三四五六七八九十百]+$", _title) is not None:
  1794. _next_title = ParseUtils.make_increase(['一', '二', '三', '四', '五', '六', '七', '八', '九', '十'],
  1795. re.sub("[十百]", '', _title))
  1796. _next_title = list(_next_title)
  1797. _next_title.reverse()
  1798. if _next_title[-1] != "十":
  1799. if len(_next_title) >= 2:
  1800. _next_title.insert(-1, '十')
  1801. if len(_next_title) >= 4:
  1802. _next_title.insert(-3, '百')
  1803. if _title[0] == "十":
  1804. if _next_title == "十":
  1805. _next_title = ["二", "十"]
  1806. _next_title.insert(0, "十")
  1807. _next_title = "".join(_next_title)
  1808. return _next_title
  1809. if re.search("^[a-z]+$", _title) is not None:
  1810. _next_title = ParseUtils.make_increase([chr(i + ord('a')) for i in range(26)], _title)
  1811. _next_title = list(_next_title)
  1812. _next_title.reverse()
  1813. return "".join(_next_title)
  1814. if re.search("^[A-Z]+$", _title) is not None:
  1815. _next_title = ParseUtils.make_increase([chr(i + ord('A')) for i in range(26)], _title)
  1816. _next_title = list(_next_title)
  1817. _next_title.reverse()
  1818. return "".join(_next_title)
  1819. if re.search("^[ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩⅪⅫ]$", _title) is not None:
  1820. _sort = ["Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ", "Ⅹ", "Ⅺ", "Ⅻ"]
  1821. _index = _sort.index(_title)
  1822. if _index < len(_sort) - 1:
  1823. return _sort[_index + 1]
  1824. return None
  1825. @staticmethod
  1826. def make_increase(_sort, _title, _add=1):
  1827. if len(_title) == 0 and _add == 0:
  1828. return ""
  1829. if len(_title) == 0 and _add == 1:
  1830. return _sort[0]
  1831. _index = _sort.index(_title[-1])
  1832. next_index = (_index + _add) % len(_sort)
  1833. next_chr = _sort[next_index]
  1834. if _index == len(_sort) - 1:
  1835. _add = 1
  1836. else:
  1837. _add = 0
  1838. return next_chr + ParseUtils.make_increase(_sort, _title[:-1], _add)
  1839. @staticmethod
  1840. def rec_serial(_text, o_bbox, p_bbox, fontname, _pattern="(?P<title_1>^[一二三四五六七八九十]+[、])|" \
  1841. "(?P<title_2>^\d+[\.、\s])|" \
  1842. "(?P<title_3>^\d+\.\d+[\.、\s])|" \
  1843. "(?P<title_4>^\d+\.\d+\.\d+[\.、\s])|" \
  1844. "(?P<title_5>^\d+\.\d+\.\d+\.\d+[\.、\s])"):
  1845. # todo :recog the serial of the sentence
  1846. _se = re.search(_pattern, _text)
  1847. if _se is not None:
  1848. _gd = _se.groupdict()
  1849. for k, v in _gd.items():
  1850. if v is not None:
  1851. return k
  1852. return None
  1853. if __name__ == '__main__':
  1854. # get_text_font()
  1855. PDFConvert(r"C:/Users/Administrator/Downloads/1651896704621.pdf", "C:/Users/Administrator/Downloads/1").get_html()
  1856. # print(b'\x10')