The PymuPDF Parser
The PymuPDF parser uses pymupdf under the hood to parse pdfs and extract text from them. It is a wrapper around the pymupdf library that provides a more user-friendly interface.
Documentation
PyMuPDFParser
Parse a PDF file using PyMuPDF parser.
Source code in parsestudio/parsers/pymupdf_parser.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
|
__export_result(pages, modalities)
Export the result of the parsing process.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pages
|
List[Page]
|
List of pages |
required |
modalities
|
List[str]
|
List of modalities to extract |
required |
Returns:
Name | Type | Description |
---|---|---|
output |
ParserOutput
|
The ParserOutput object containing the extracted modalities. |
Source code in parsestudio/parsers/pymupdf_parser.py
_extract_images(page)
staticmethod
Extract the images from the page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
page
|
Page
|
The page object |
required |
Returns:
Name | Type | Description |
---|---|---|
images |
List[ImageElement]
|
List of ImageElement objects |
Example:
Example
Source code in parsestudio/parsers/pymupdf_parser.py
_extract_tables(page)
staticmethod
Extract the tables from the page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
page
|
Page
|
The page object |
required |
Returns:
Name | Type | Description |
---|---|---|
tables |
List[TableElement]
|
List of TableElement objects |
Example:
Example
Source code in parsestudio/parsers/pymupdf_parser.py
_extract_text(page)
staticmethod
Extract the text from the page.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
page
|
Page
|
The page object |
required |
Returns:
Name | Type | Description |
---|---|---|
text |
TextElement
|
The extracted text element |
Example:
Example
Source code in parsestudio/parsers/pymupdf_parser.py
_validate_modalities(modalities)
Validate the modalities provided by the user. The valid modalities are: ["text", "tables", "images"]
Parameters:
Name | Type | Description | Default |
---|---|---|---|
modalities
|
List[str]
|
List of modalities to validate |
required |
Raises:
Type | Description |
---|---|
ValueError
|
If the modality is not valid |
Source code in parsestudio/parsers/pymupdf_parser.py
load_documents(paths)
staticmethod
Load the documents from the given paths.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths
|
List[str]
|
List of paths to the PDF files. |
required |
Returns:
Name | Type | Description |
---|---|---|
result |
Generator[List[Page], None, None]
|
A generator that yields a list of pages for each document |
Source code in parsestudio/parsers/pymupdf_parser.py
parse(paths, modalities=['text', 'tables', 'images'])
Parse the PDF file and return the extracted the specified modalities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
paths
|
Union[str, List[str]]
|
A path or a list of paths to the PDF files. |
required |
modalities
|
List[str]
|
List of modalities to extract. Defaults to ["text", "tables", "images"]. |
['text', 'tables', 'images']
|
Returns:
Name | Type | Description |
---|---|---|
data |
List[ParserOutput]
|
A list of ParserOutput objects containing the extracted modalities. |
Raises:
Type | Description |
---|---|
ValueError
|
If the modality is not valid |
Example:
Example
parser = PyMuPDFParser()
data = parser.parse("path/to/file.pdf", modalities=["text", "tables", "images"])
print(len(data))
# Output: 1
text = data[0].text # TextElement
tables = data[0].tables # List of TableElement
images = data[0].images # List of ImageElement
# Access the text
text = text.text
# Access the first table
table = tables[0]
# Access the markdown representation of the table
table_md = table.markdown
# Access the dataframe representation of the table
table_df = table.dataframe
# Access the metadata of the table
page_number = table.metadata.page_number
bbox = table.metadata.bbox
# Access the first image
image = images[0]
# Access the image object
image_obj = image.image # PIL Image object
# Access the metadata of the image
page_number = image.metadata.page_number
bbox = image.metadata.bbox