pdf2word.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. # copyright (c) 2022 PaddlePaddle Authors. All Rights Reserve.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import sys
  15. import tarfile
  16. import os
  17. import time
  18. import datetime
  19. import functools
  20. import cv2
  21. import platform
  22. import numpy as np
  23. import fitz
  24. from PIL import Image
  25. from pdf2docx.converter import Converter
  26. from qtpy.QtWidgets import QApplication, QWidget, QPushButton, QProgressBar, \
  27. QGridLayout, QMessageBox, QLabel, QFileDialog, QCheckBox
  28. from qtpy.QtCore import Signal, QThread, QObject
  29. from qtpy.QtGui import QImage, QPixmap, QIcon
  30. file = os.path.dirname(os.path.abspath(__file__))
  31. root = os.path.abspath(os.path.join(file, '../../'))
  32. sys.path.append(file)
  33. sys.path.insert(0, root)
  34. from ppstructure.predict_system import StructureSystem, save_structure_res
  35. from ppstructure.utility import parse_args, draw_structure_result
  36. from ppocr.utils.network import download_with_progressbar
  37. from ppstructure.recovery.recovery_to_doc import sorted_layout_boxes, convert_info_docx
  38. # from ScreenShotWidget import ScreenShotWidget
  39. __APPNAME__ = "pdf2word"
  40. __VERSION__ = "0.2.2"
  41. URLs_EN = {
  42. # 下载超英文轻量级PP-OCRv3模型的检测模型并解压
  43. "en_PP-OCRv3_det_infer":
  44. "https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar",
  45. # 下载英文轻量级PP-OCRv3模型的识别模型并解压
  46. "en_PP-OCRv3_rec_infer":
  47. "https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar",
  48. # 下载超轻量级英文表格英文模型并解压
  49. "en_ppstructure_mobile_v2.0_SLANet_infer":
  50. "https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/en_ppstructure_mobile_v2.0_SLANet_infer.tar",
  51. # 英文版面分析模型
  52. "picodet_lcnet_x1_0_fgd_layout_infer":
  53. "https://paddleocr.bj.bcebos.com/ppstructure/models/layout/picodet_lcnet_x1_0_fgd_layout_infer.tar",
  54. }
  55. DICT_EN = {
  56. "rec_char_dict_path": "en_dict.txt",
  57. "layout_dict_path": "layout_publaynet_dict.txt",
  58. }
  59. URLs_CN = {
  60. # 下载超中文轻量级PP-OCRv3模型的检测模型并解压
  61. "cn_PP-OCRv3_det_infer":
  62. "https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar",
  63. # 下载中文轻量级PP-OCRv3模型的识别模型并解压
  64. "cn_PP-OCRv3_rec_infer":
  65. "https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar",
  66. # 下载超轻量级英文表格英文模型并解压
  67. "cn_ppstructure_mobile_v2.0_SLANet_infer":
  68. "https://paddleocr.bj.bcebos.com/ppstructure/models/slanet/en_ppstructure_mobile_v2.0_SLANet_infer.tar",
  69. # 中文版面分析模型
  70. "picodet_lcnet_x1_0_fgd_layout_cdla_infer":
  71. "https://paddleocr.bj.bcebos.com/ppstructure/models/layout/picodet_lcnet_x1_0_fgd_layout_cdla_infer.tar",
  72. }
  73. DICT_CN = {
  74. "rec_char_dict_path": "ppocr_keys_v1.txt",
  75. "layout_dict_path": "layout_cdla_dict.txt",
  76. }
  77. def QImageToCvMat(incomingImage) -> np.array:
  78. '''
  79. Converts a QImage into an opencv MAT format
  80. '''
  81. incomingImage = incomingImage.convertToFormat(QImage.Format.Format_RGBA8888)
  82. width = incomingImage.width()
  83. height = incomingImage.height()
  84. ptr = incomingImage.bits()
  85. ptr.setsize(height * width * 4)
  86. arr = np.frombuffer(ptr, np.uint8).reshape((height, width, 4))
  87. return arr
  88. def readImage(image_file) -> list:
  89. if os.path.basename(image_file)[-3:] == 'pdf':
  90. imgs = []
  91. with fitz.open(image_file) as pdf:
  92. for pg in range(0, pdf.pageCount):
  93. page = pdf[pg]
  94. mat = fitz.Matrix(2, 2)
  95. pm = page.getPixmap(matrix=mat, alpha=False)
  96. # if width or height > 2000 pixels, don't enlarge the image
  97. if pm.width > 2000 or pm.height > 2000:
  98. pm = page.getPixmap(matrix=fitz.Matrix(1, 1), alpha=False)
  99. img = Image.frombytes("RGB", [pm.width, pm.height], pm.samples)
  100. img = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
  101. imgs.append(img)
  102. else:
  103. img = cv2.imread(image_file, cv2.IMREAD_COLOR)
  104. if img is not None:
  105. imgs = [img]
  106. return imgs
  107. class Worker(QThread):
  108. progressBarValue = Signal(int)
  109. progressBarRange = Signal(int)
  110. endsignal = Signal()
  111. exceptedsignal = Signal(str) #发送一个异常信号
  112. loopFlag = True
  113. def __init__(self, predictors, save_pdf, vis_font_path, use_pdf2docx_api):
  114. super(Worker, self).__init__()
  115. self.predictors = predictors
  116. self.save_pdf = save_pdf
  117. self.vis_font_path = vis_font_path
  118. self.lang = 'EN'
  119. self.imagePaths = []
  120. self.use_pdf2docx_api = use_pdf2docx_api
  121. self.outputDir = None
  122. self.totalPageCnt = 0
  123. self.pageCnt = 0
  124. self.setStackSize(1024 * 1024)
  125. def setImagePath(self, imagePaths):
  126. self.imagePaths = imagePaths
  127. def setLang(self, lang):
  128. self.lang = lang
  129. def setOutputDir(self, outputDir):
  130. self.outputDir = outputDir
  131. def setPDFParser(self, enabled):
  132. self.use_pdf2docx_api = enabled
  133. def resetPageCnt(self):
  134. self.pageCnt = 0
  135. def resetTotalPageCnt(self):
  136. self.totalPageCnt = 0
  137. def ppocrPrecitor(self, imgs, img_name):
  138. all_res = []
  139. # update progress bar ranges
  140. self.totalPageCnt += len(imgs)
  141. self.progressBarRange.emit(self.totalPageCnt)
  142. # processing pages
  143. for index, img in enumerate(imgs):
  144. res, time_dict = self.predictors[self.lang](img)
  145. # save output
  146. save_structure_res(res, self.outputDir, img_name)
  147. # draw_img = draw_structure_result(img, res, self.vis_font_path)
  148. # img_save_path = os.path.join(self.outputDir, img_name, 'show_{}.jpg'.format(index))
  149. # if res != []:
  150. # cv2.imwrite(img_save_path, draw_img)
  151. # recovery
  152. h, w, _ = img.shape
  153. res = sorted_layout_boxes(res, w)
  154. all_res += res
  155. self.pageCnt += 1
  156. self.progressBarValue.emit(self.pageCnt)
  157. if all_res != []:
  158. try:
  159. convert_info_docx(imgs, all_res, self.outputDir, img_name)
  160. except Exception as ex:
  161. print("error in layout recovery image:{}, err msg: {}".format(
  162. img_name, ex))
  163. print("Predict time : {:.3f}s".format(time_dict['all']))
  164. print('result save to {}'.format(self.outputDir))
  165. def run(self):
  166. self.resetPageCnt()
  167. self.resetTotalPageCnt()
  168. try:
  169. os.makedirs(self.outputDir, exist_ok=True)
  170. for i, image_file in enumerate(self.imagePaths):
  171. if not self.loopFlag:
  172. break
  173. # using use_pdf2docx_api for PDF parsing
  174. if self.use_pdf2docx_api \
  175. and os.path.basename(image_file)[-3:] == 'pdf':
  176. self.totalPageCnt += 1
  177. self.progressBarRange.emit(self.totalPageCnt)
  178. print(
  179. '===============using use_pdf2docx_api===============')
  180. img_name = os.path.basename(image_file).split('.')[0]
  181. docx_file = os.path.join(self.outputDir,
  182. '{}.docx'.format(img_name))
  183. cv = Converter(image_file)
  184. cv.convert(docx_file)
  185. cv.close()
  186. print('docx save to {}'.format(docx_file))
  187. self.pageCnt += 1
  188. self.progressBarValue.emit(self.pageCnt)
  189. else:
  190. # using PPOCR for PDF/Image parsing
  191. imgs = readImage(image_file)
  192. if len(imgs) == 0:
  193. continue
  194. img_name = os.path.basename(image_file).split('.')[0]
  195. os.makedirs(
  196. os.path.join(self.outputDir, img_name), exist_ok=True)
  197. self.ppocrPrecitor(imgs, img_name)
  198. # file processed
  199. self.endsignal.emit()
  200. # self.exec()
  201. except Exception as e:
  202. self.exceptedsignal.emit(str(e)) # 将异常发送给UI进程
  203. class APP_Image2Doc(QWidget):
  204. def __init__(self):
  205. super().__init__()
  206. # self.setFixedHeight(100)
  207. # self.setFixedWidth(520)
  208. # settings
  209. self.imagePaths = []
  210. # self.screenShotWg = ScreenShotWidget()
  211. self.screenShot = None
  212. self.save_pdf = False
  213. self.output_dir = None
  214. self.vis_font_path = os.path.join(root, "doc", "fonts", "simfang.ttf")
  215. self.use_pdf2docx_api = False
  216. # ProgressBar
  217. self.pb = QProgressBar()
  218. self.pb.setRange(0, 100)
  219. self.pb.setValue(0)
  220. # 初始化界面
  221. self.setupUi()
  222. # 下载模型
  223. self.downloadModels(URLs_EN)
  224. self.downloadModels(URLs_CN)
  225. # 初始化模型
  226. predictors = {
  227. 'EN': self.initPredictor('EN'),
  228. 'CN': self.initPredictor('CN'),
  229. }
  230. # 设置工作进程
  231. self._thread = Worker(predictors, self.save_pdf, self.vis_font_path,
  232. self.use_pdf2docx_api)
  233. self._thread.progressBarValue.connect(
  234. self.handleProgressBarUpdateSingal)
  235. self._thread.endsignal.connect(self.handleEndsignalSignal)
  236. # self._thread.finished.connect(QObject.deleteLater)
  237. self._thread.progressBarRange.connect(self.handleProgressBarRangeSingal)
  238. self._thread.exceptedsignal.connect(self.handleThreadException)
  239. self.time_start = 0 # save start time
  240. def setupUi(self):
  241. self.setObjectName("MainWindow")
  242. self.setWindowTitle(__APPNAME__ + " " + __VERSION__)
  243. layout = QGridLayout()
  244. self.openFileButton = QPushButton("打开文件")
  245. self.openFileButton.setIcon(QIcon(QPixmap("./icons/folder-plus.png")))
  246. layout.addWidget(self.openFileButton, 0, 0, 1, 1)
  247. self.openFileButton.clicked.connect(self.handleOpenFileSignal)
  248. # screenShotButton = QPushButton("截图识别")
  249. # layout.addWidget(screenShotButton, 0, 1, 1, 1)
  250. # screenShotButton.clicked.connect(self.screenShotSlot)
  251. # screenShotButton.setEnabled(False) # temporarily disenble
  252. self.startCNButton = QPushButton("中文转换")
  253. self.startCNButton.setIcon(QIcon(QPixmap("./icons/chinese.png")))
  254. layout.addWidget(self.startCNButton, 0, 1, 1, 1)
  255. self.startCNButton.clicked.connect(
  256. functools.partial(self.handleStartSignal, 'CN', False))
  257. self.startENButton = QPushButton("英文转换")
  258. self.startENButton.setIcon(QIcon(QPixmap("./icons/english.png")))
  259. layout.addWidget(self.startENButton, 0, 2, 1, 1)
  260. self.startENButton.clicked.connect(
  261. functools.partial(self.handleStartSignal, 'EN', False))
  262. self.PDFParserButton = QPushButton('PDF解析', self)
  263. layout.addWidget(self.PDFParserButton, 0, 3, 1, 1)
  264. self.PDFParserButton.clicked.connect(
  265. functools.partial(self.handleStartSignal, 'CN', True))
  266. self.showResultButton = QPushButton("显示结果")
  267. self.showResultButton.setIcon(QIcon(QPixmap("./icons/folder-open.png")))
  268. layout.addWidget(self.showResultButton, 0, 4, 1, 1)
  269. self.showResultButton.clicked.connect(self.handleShowResultSignal)
  270. # ProgressBar
  271. layout.addWidget(self.pb, 2, 0, 1, 5)
  272. # time estimate label
  273. self.timeEstLabel = QLabel(("Time Left: --"))
  274. layout.addWidget(self.timeEstLabel, 3, 0, 1, 5)
  275. self.setLayout(layout)
  276. def downloadModels(self, URLs):
  277. # using custom model
  278. tar_file_name_list = [
  279. 'inference.pdiparams', 'inference.pdiparams.info',
  280. 'inference.pdmodel', 'model.pdiparams', 'model.pdiparams.info',
  281. 'model.pdmodel'
  282. ]
  283. model_path = os.path.join(root, 'inference')
  284. os.makedirs(model_path, exist_ok=True)
  285. # download and unzip models
  286. for name in URLs.keys():
  287. url = URLs[name]
  288. print("Try downloading file: {}".format(url))
  289. tarname = url.split('/')[-1]
  290. tarpath = os.path.join(model_path, tarname)
  291. if os.path.exists(tarpath):
  292. print("File have already exist. skip")
  293. else:
  294. try:
  295. download_with_progressbar(url, tarpath)
  296. except Exception as e:
  297. print(
  298. "Error occurred when downloading file, error message:")
  299. print(e)
  300. # unzip model tar
  301. try:
  302. with tarfile.open(tarpath, 'r') as tarObj:
  303. storage_dir = os.path.join(model_path, name)
  304. os.makedirs(storage_dir, exist_ok=True)
  305. for member in tarObj.getmembers():
  306. filename = None
  307. for tar_file_name in tar_file_name_list:
  308. if tar_file_name in member.name:
  309. filename = tar_file_name
  310. if filename is None:
  311. continue
  312. file = tarObj.extractfile(member)
  313. with open(os.path.join(storage_dir, filename),
  314. 'wb') as f:
  315. f.write(file.read())
  316. except Exception as e:
  317. print("Error occurred when unziping file, error message:")
  318. print(e)
  319. def initPredictor(self, lang='EN'):
  320. # init predictor args
  321. args = parse_args()
  322. args.table_max_len = 488
  323. args.ocr = True
  324. args.recovery = True
  325. args.save_pdf = self.save_pdf
  326. args.table_char_dict_path = os.path.join(root, "ppocr", "utils", "dict",
  327. "table_structure_dict.txt")
  328. if lang == 'EN':
  329. args.det_model_dir = os.path.join(
  330. root, # 此处从这里找到模型存放位置
  331. "inference",
  332. "en_PP-OCRv3_det_infer")
  333. args.rec_model_dir = os.path.join(root, "inference",
  334. "en_PP-OCRv3_rec_infer")
  335. args.table_model_dir = os.path.join(
  336. root, "inference", "en_ppstructure_mobile_v2.0_SLANet_infer")
  337. args.output = os.path.join(root, "output") # 结果保存路径
  338. args.layout_model_dir = os.path.join(
  339. root, "inference", "picodet_lcnet_x1_0_fgd_layout_infer")
  340. lang_dict = DICT_EN
  341. elif lang == 'CN':
  342. args.det_model_dir = os.path.join(
  343. root, # 此处从这里找到模型存放位置
  344. "inference",
  345. "cn_PP-OCRv3_det_infer")
  346. args.rec_model_dir = os.path.join(root, "inference",
  347. "cn_PP-OCRv3_rec_infer")
  348. args.table_model_dir = os.path.join(
  349. root, "inference", "cn_ppstructure_mobile_v2.0_SLANet_infer")
  350. args.output = os.path.join(root, "output") # 结果保存路径
  351. args.layout_model_dir = os.path.join(
  352. root, "inference", "picodet_lcnet_x1_0_fgd_layout_cdla_infer")
  353. lang_dict = DICT_CN
  354. else:
  355. raise ValueError("Unsupported language")
  356. args.rec_char_dict_path = os.path.join(root, "ppocr", "utils",
  357. lang_dict['rec_char_dict_path'])
  358. args.layout_dict_path = os.path.join(root, "ppocr", "utils", "dict",
  359. "layout_dict",
  360. lang_dict['layout_dict_path'])
  361. # init predictor
  362. return StructureSystem(args)
  363. def handleOpenFileSignal(self):
  364. '''
  365. 可以多选图像文件
  366. '''
  367. selectedFiles = QFileDialog.getOpenFileNames(
  368. self, "多文件选择", "/", "图片文件 (*.png *.jpeg *.jpg *.bmp *.pdf)")[0]
  369. if len(selectedFiles) > 0:
  370. self.imagePaths = selectedFiles
  371. self.screenShot = None # discard screenshot temp image
  372. self.pb.setValue(0)
  373. # def screenShotSlot(self):
  374. # '''
  375. # 选定图像文件和截图的转换过程只能同时进行一个
  376. # 截图只能同时转换一个
  377. # '''
  378. # self.screenShotWg.start()
  379. # if self.screenShotWg.captureImage:
  380. # self.screenShot = self.screenShotWg.captureImage
  381. # self.imagePaths.clear() # discard openfile temp list
  382. # self.pb.setRange(0, 1)
  383. # self.pb.setValue(0)
  384. def handleStartSignal(self, lang='EN', pdfParser=False):
  385. if self.screenShot: # for screenShot
  386. img_name = 'screenshot_' + time.strftime("%Y%m%d%H%M%S",
  387. time.localtime())
  388. image = QImageToCvMat(self.screenShot)
  389. self.predictAndSave(image, img_name, lang)
  390. # update Progress Bar
  391. self.pb.setValue(1)
  392. QMessageBox.information(self, u'Information', "文档提取完成")
  393. elif len(self.imagePaths) > 0: # for image file selection
  394. # Must set image path list and language before start
  395. self.output_dir = os.path.join(
  396. os.path.dirname(self.imagePaths[0]),
  397. "output") # output_dir shold be same as imagepath
  398. self._thread.setOutputDir(self.output_dir)
  399. self._thread.setImagePath(self.imagePaths)
  400. self._thread.setLang(lang)
  401. self._thread.setPDFParser(pdfParser)
  402. # disenble buttons
  403. self.openFileButton.setEnabled(False)
  404. self.startCNButton.setEnabled(False)
  405. self.startENButton.setEnabled(False)
  406. self.PDFParserButton.setEnabled(False)
  407. # 启动工作进程
  408. self._thread.start()
  409. self.time_start = time.time() # log start time
  410. QMessageBox.information(self, u'Information', "开始转换")
  411. else:
  412. QMessageBox.warning(self, u'Information', "请选择要识别的文件或截图")
  413. def handleShowResultSignal(self):
  414. if self.output_dir is None:
  415. return
  416. if os.path.exists(self.output_dir):
  417. if platform.system() == 'Windows':
  418. os.startfile(self.output_dir)
  419. else:
  420. os.system('open ' + os.path.normpath(self.output_dir))
  421. else:
  422. QMessageBox.information(self, u'Information', "输出文件不存在")
  423. def handleProgressBarUpdateSingal(self, i):
  424. self.pb.setValue(i)
  425. # calculate time left of recognition
  426. lenbar = self.pb.maximum()
  427. avg_time = (time.time() - self.time_start
  428. ) / i # Use average time to prevent time fluctuations
  429. time_left = str(datetime.timedelta(seconds=avg_time * (
  430. lenbar - i))).split(".")[0] # Remove microseconds
  431. self.timeEstLabel.setText(f"Time Left: {time_left}") # show time left
  432. def handleProgressBarRangeSingal(self, max):
  433. self.pb.setRange(0, max)
  434. def handleEndsignalSignal(self):
  435. # enble buttons
  436. self.openFileButton.setEnabled(True)
  437. self.startCNButton.setEnabled(True)
  438. self.startENButton.setEnabled(True)
  439. self.PDFParserButton.setEnabled(True)
  440. QMessageBox.information(self, u'Information', "转换结束")
  441. def handleCBChangeSignal(self):
  442. self._thread.setPDFParser(self.checkBox.isChecked())
  443. def handleThreadException(self, message):
  444. self._thread.quit()
  445. QMessageBox.information(self, 'Error', message)
  446. def main():
  447. app = QApplication(sys.argv)
  448. window = APP_Image2Doc() # 创建对象
  449. window.show() # 全屏显示窗口
  450. QApplication.processEvents()
  451. sys.exit(app.exec())
  452. if __name__ == "__main__":
  453. main()