MCPcopy Index your code
hub / github.com/pymupdf/PyMuPDF / extract_text

Function extract_text

src/table.py:824–854  ·  view source on GitHub ↗
(chars: list, **kwargs)

Source from the content-addressed store, hash-verified

822
823
824def extract_text(chars: list, **kwargs) -> str:
825 chars = to_list(chars)
826 if len(chars) == 0:
827 return ""
828
829 if kwargs.get("layout"):
830 return chars_to_textmap(chars, **kwargs).as_string
831 else:
832 y_tolerance = kwargs.get("y_tolerance", DEFAULT_Y_TOLERANCE)
833 extractor = WordExtractor(
834 **{k: kwargs[k] for k in WORD_EXTRACTOR_KWARGS if k in kwargs}
835 )
836 words = extractor.extract_words(chars)
837 if words:
838 rotation = words[0]["rotation"] # rotation cannot change within a cell
839 else:
840 rotation = 0
841
842 if rotation == 90:
843 words.sort(key=lambda w: (w["x1"], -w["top"]))
844 lines = " ".join([w["text"] for w in words])
845 elif rotation == 270:
846 words.sort(key=lambda w: (-w["x1"], w["top"]))
847 lines = " ".join([w["text"] for w in words])
848 else:
849 lines = cluster_objects(words, itemgetter("doctop"), y_tolerance)
850 lines = "\n".join(" ".join(word["text"] for word in line) for line in lines)
851 if rotation == 180: # needs extra treatment
852 lines = "".join([(c if c != "\n" else " ") for c in reversed(lines)])
853
854 return lines
855
856
857def collate_line(

Callers 1

extractMethod · 0.85

Calls 6

extract_wordsMethod · 0.95
to_listFunction · 0.85
chars_to_textmapFunction · 0.85
WordExtractorClass · 0.85
cluster_objectsFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…