cyhasuka commited on
Commit
3c857ed
·
1 Parent(s): 2067c85

Fix: page_chars attribute does not exist in some formats of PDF (#3796)

Browse files

### What problem does this PR solve?

In #3335 someone suggested to upgrade pdfplumber==0.11.1, but that
didn't solve it.
It's actually the special formatting in some of the pdfs that triggers
the problem.

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

Files changed (1) hide show
  1. deepdoc/parser/pdf_parser.py +6 -2
deepdoc/parser/pdf_parser.py CHANGED
@@ -956,8 +956,12 @@ class RAGFlowPdfParser:
956
  enumerate(self.pdf.pages[page_from:page_to])]
957
  self.page_images_x2 = [p.to_image(resolution=72 * zoomin * 2).annotated for i, p in
958
  enumerate(self.pdf.pages[page_from:page_to])]
959
- self.page_chars = [[{**c, 'top': c['top'], 'bottom': c['bottom']} for c in page.dedupe_chars().chars if self._has_color(c)] for page in
960
- self.pdf.pages[page_from:page_to]]
 
 
 
 
961
  self.total_page = len(self.pdf.pages)
962
  except Exception:
963
  logging.exception("RAGFlowPdfParser __images__")
 
956
  enumerate(self.pdf.pages[page_from:page_to])]
957
  self.page_images_x2 = [p.to_image(resolution=72 * zoomin * 2).annotated for i, p in
958
  enumerate(self.pdf.pages[page_from:page_to])]
959
+ try:
960
+ self.page_chars = [[{**c, 'top': c['top'], 'bottom': c['bottom']} for c in page.dedupe_chars().chars if self._has_color(c)] for page in self.pdf.pages[page_from:page_to]]
961
+ except Exception as e:
962
+ logging.warning(f"Failed to extract characters for pages {page_from}-{page_to}: {str(e)}")
963
+ self.page_chars = [[] for _ in range(page_to - page_from)] # If failed to extract, using empty list instead.
964
+
965
  self.total_page = len(self.pdf.pages)
966
  except Exception:
967
  logging.exception("RAGFlowPdfParser __images__")