docling-core 2.5.0__py3-none-any.whl → 2.6.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of docling-core might be problematic. Click here for more details.

@@ -10,6 +10,7 @@ import re
10
10
  import sys
11
11
  import textwrap
12
12
  import typing
13
+ import warnings
13
14
  from io import BytesIO
14
15
  from pathlib import Path
15
16
  from typing import Any, Dict, Final, List, Literal, Optional, Tuple, Union
@@ -809,14 +810,8 @@ class PictureItem(FloatingItem):
809
810
  ):
810
811
  return default_response
811
812
 
812
- if (
813
- isinstance(self.image.uri, AnyUrl) and self.image.uri.scheme == "file"
814
- ) or isinstance(self.image.uri, Path):
815
- text = f"\n![Image]({str(self.image.uri)})\n"
816
- return text
817
-
818
- else:
819
- return default_response
813
+ text = f"\n![Image]({str(self.image.uri)})\n"
814
+ return text
820
815
 
821
816
  else:
822
817
  return default_response
@@ -869,14 +864,8 @@ class PictureItem(FloatingItem):
869
864
  ):
870
865
  return default_response
871
866
 
872
- if (
873
- isinstance(self.image.uri, AnyUrl) and self.image.uri.scheme == "file"
874
- ) or isinstance(self.image.uri, Path):
875
- img_text = f'<img src="{str(self.image.uri)}">'
876
- return f"<figure>{caption_text}{img_text}</figure>"
877
-
878
- else:
879
- return default_response
867
+ img_text = f'<img src="{str(self.image.uri)}">'
868
+ return f"<figure>{caption_text}{img_text}</figure>"
880
869
 
881
870
  else:
882
871
  return default_response
@@ -1008,14 +997,23 @@ class TableItem(FloatingItem):
1008
997
  )
1009
998
  return md_table
1010
999
 
1011
- def export_to_html(self, doc: "DoclingDocument", add_caption: bool = True) -> str:
1000
+ def export_to_html(
1001
+ self, doc: Optional["DoclingDocument"] = None, add_caption: bool = True
1002
+ ) -> str:
1012
1003
  """Export the table as html."""
1004
+ if doc is None:
1005
+ warnings.warn(
1006
+ "The `doc` argument will be mandatory in a future version. "
1007
+ "It must be provided to include a caption.",
1008
+ DeprecationWarning,
1009
+ )
1010
+
1013
1011
  body = ""
1014
1012
  nrows = self.data.num_rows
1015
1013
  ncols = self.data.num_cols
1016
1014
 
1017
1015
  text = ""
1018
- if add_caption and len(self.captions):
1016
+ if doc is not None and add_caption and len(self.captions):
1019
1017
  text = self.caption_text(doc)
1020
1018
 
1021
1019
  if len(self.data.table_cells) == 0:
@@ -1201,19 +1199,58 @@ class DoclingDocument(BaseModel):
1201
1199
  """DoclingDocument."""
1202
1200
 
1203
1201
  _HTML_DEFAULT_HEAD: str = r"""<head>
1202
+ <link rel="icon" type="image/png"
1203
+ href="https://ds4sd.github.io/docling/assets/logo.png"/>
1204
1204
  <meta charset="UTF-8">
1205
+ <title>
1206
+ Powered by Docling
1207
+ </title>
1205
1208
  <style>
1209
+ html {
1210
+ background-color: LightGray;
1211
+ }
1212
+ body {
1213
+ margin: 0 auto;
1214
+ width:800px;
1215
+ padding: 30px;
1216
+ background-color: White;
1217
+ font-family: Arial, sans-serif;
1218
+ box-shadow: 10px 10px 10px grey;
1219
+ }
1220
+ figure{
1221
+ display: block;
1222
+ width: 100%;
1223
+ margin: 0px;
1224
+ margin-top: 10px;
1225
+ margin-bottom: 10px;
1226
+ }
1227
+ img {
1228
+ display: block;
1229
+ margin: auto;
1230
+ margin-top: 10px;
1231
+ margin-bottom: 10px;
1232
+ max-width: 640px;
1233
+ max-height: 640px;
1234
+ }
1206
1235
  table {
1207
- border-collapse: separate;
1208
- /* Maintain separate borders */
1209
- border-spacing: 5px; /*
1210
- Space between cells */
1211
- width: 50%;
1236
+ min-width:500px;
1237
+ background-color: White;
1238
+ border-collapse: collapse;
1239
+ cell-padding: 5px;
1240
+ margin: auto;
1241
+ margin-top: 10px;
1242
+ margin-bottom: 10px;
1212
1243
  }
1213
1244
  th, td {
1214
1245
  border: 1px solid black;
1215
- /* Add lines etween cells */
1216
- padding: 8px; }
1246
+ padding: 8px;
1247
+ }
1248
+ th {
1249
+ font-weight: bold;
1250
+ }
1251
+ table tr:nth-child(even) td{
1252
+ background-color: LightGray;
1253
+ }
1217
1254
  </style>
1218
1255
  </head>"""
1219
1256
 
@@ -1723,6 +1760,20 @@ class DoclingDocument(BaseModel):
1723
1760
  with open(filename, "w") as fw:
1724
1761
  json.dump(out, fw, indent=indent)
1725
1762
 
1763
+ @classmethod
1764
+ def load_from_json(cls, filename: Path) -> "DoclingDocument":
1765
+ """load_from_json.
1766
+
1767
+ :param filename: The filename to load a saved DoclingDocument from a .json.
1768
+ :type filename: Path
1769
+
1770
+ :returns: The loaded DoclingDocument.
1771
+ :rtype: DoclingDocument
1772
+
1773
+ """
1774
+ with open(filename, "r") as f:
1775
+ return cls.model_validate(json.loads(f.read()))
1776
+
1726
1777
  def save_as_yaml(
1727
1778
  self,
1728
1779
  filename: Path,
@@ -1815,26 +1866,28 @@ class DoclingDocument(BaseModel):
1815
1866
  from_element and to_element; defaulting to the whole document.
1816
1867
 
1817
1868
  :param delim: Delimiter to use when concatenating the various
1818
- Markdown parts. Defaults to "\n\n".
1819
- :type delim: str
1869
+ Markdown parts. (Default value = "\n").
1870
+ :type delim: str = "\n"
1820
1871
  :param from_element: Body slicing start index (inclusive).
1821
- Defaults to 0.
1822
- :type from_element: int
1872
+ (Default value = 0).
1873
+ :type from_element: int = 0
1823
1874
  :param to_element: Body slicing stop index
1824
- (exclusive). Defaults to 0maxint.
1825
- :type to_element: int
1826
- :param delim: str: (Default value = "\n\n")
1827
- :param labels: set[DocItemLabel]
1828
- :param "subtitle-level-1":
1829
- :param "paragraph":
1830
- :param "caption":
1831
- :param "table":
1832
- :param "Text":
1833
- :param "text":
1834
- :param strict_text: bool: (Default value = False)
1835
- :param image_placeholder str: (Default value = "<!-- image -->")
1836
- the placeholder to include to position images in the markdown.
1837
- :param indent: int (default=4): indent of the nested lists
1875
+ (exclusive). (Default value = maxint).
1876
+ :type to_element: int = sys.maxsize
1877
+ :param labels: The set of document labels to include in the export.
1878
+ :type labels: set[DocItemLabel] = DEFAULT_EXPORT_LABELS
1879
+ :param strict_text: bool: Whether to only include the text content
1880
+ of the document. (Default value = False).
1881
+ :type strict_text: bool = False
1882
+ :param image_placeholder: The placeholder to include to position
1883
+ images in the markdown. (Default value = "\<!-- image --\>").
1884
+ :type image_placeholder: str = "<!-- image -->"
1885
+ :param image_mode: The mode to use for including images in the
1886
+ markdown. (Default value = ImageRefMode.PLACEHOLDER).
1887
+ :type image_mode: ImageRefMode = ImageRefMode.PLACEHOLDER
1888
+ :param indent: The indent in spaces of the nested lists.
1889
+ (Default value = 4).
1890
+ :type indent: int = 4
1838
1891
  :returns: The exported Markdown representation.
1839
1892
  :rtype: str
1840
1893
  """
@@ -2037,7 +2090,7 @@ class DoclingDocument(BaseModel):
2037
2090
  if artifacts_dir is None:
2038
2091
  # Remove the extension and add '_pictures'
2039
2092
  artifacts_dir = filename.with_suffix("")
2040
- artifacts_dir = artifacts_dir.with_name(artifacts_dir.stem + "_artifacts")
2093
+ artifacts_dir = artifacts_dir.with_name(artifacts_dir.name + "_artifacts")
2041
2094
  if artifacts_dir.is_absolute():
2042
2095
  reference_path = None
2043
2096
  else:
@@ -0,0 +1,19 @@
1
+ #
2
+ # Copyright IBM Corp. 2024 - 2024
3
+ # SPDX-License-Identifier: MIT
4
+ #
5
+
6
+ """Models for io."""
7
+
8
+ from io import BytesIO
9
+
10
+ from pydantic import BaseModel, ConfigDict
11
+
12
+
13
+ class DocumentStream(BaseModel):
14
+ """Wrapper class for a bytes stream with a filename."""
15
+
16
+ model_config = ConfigDict(arbitrary_types_allowed=True)
17
+
18
+ name: str
19
+ stream: BytesIO
@@ -7,28 +7,62 @@
7
7
 
8
8
  import importlib
9
9
  import tempfile
10
+ from io import BytesIO
10
11
  from pathlib import Path
11
12
  from typing import Dict, Optional, Union
12
13
 
13
14
  import requests
14
15
  from pydantic import AnyHttpUrl, TypeAdapter, ValidationError
16
+ from typing_extensions import deprecated
15
17
 
18
+ from docling_core.types.io import DocumentStream
16
19
 
17
- def resolve_file_source(
18
- source: Union[Path, AnyHttpUrl, str], headers: Optional[Dict[str, str]] = None
19
- ) -> Path:
20
- """Resolves the source (URL, path) of a file to a local file path.
21
20
 
22
- If a URL is provided, the content is first downloaded to a temporary local file.
21
+ def resolve_remote_filename(
22
+ http_url: AnyHttpUrl,
23
+ response_headers: Dict[str, str],
24
+ fallback_filename="file",
25
+ ) -> str:
26
+ """Resolves the filename from a remote url and its response headers.
27
+
28
+ Args:
29
+ source AnyHttpUrl: The source http url.
30
+ response_headers Dict: Headers received while fetching the remote file.
31
+ fallback_filename str: Filename to use in case none can be determined.
32
+
33
+ Returns:
34
+ str: The actual filename of the remote url.
35
+ """
36
+ fname = None
37
+ # try to get filename from response header
38
+ if cont_disp := response_headers.get("Content-Disposition"):
39
+ for par in cont_disp.strip().split(";"):
40
+ # currently only handling directive "filename" (not "*filename")
41
+ if (split := par.split("=")) and split[0].strip() == "filename":
42
+ fname = "=".join(split[1:]).strip().strip("'\"") or None
43
+ break
44
+ # otherwise, use name from URL:
45
+ if fname is None:
46
+ fname = Path(http_url.path or "").name or fallback_filename
47
+
48
+ return fname
49
+
50
+
51
+ def resolve_source_to_stream(
52
+ source: Union[Path, AnyHttpUrl, str], headers: Optional[Dict[str, str]] = None
53
+ ) -> DocumentStream:
54
+ """Resolves the source (URL, path) of a file to a binary stream.
23
55
 
24
56
  Args:
25
57
  source (Path | AnyHttpUrl | str): The file input source. Can be a path or URL.
58
+ headers (Dict | None): Optional set of headers to use for fetching
59
+ the remote URL.
26
60
 
27
61
  Raises:
28
62
  ValueError: If source is of unexpected type.
29
63
 
30
64
  Returns:
31
- Path: The local file path.
65
+ DocumentStream: The resolved file loaded as a stream.
32
66
  """
33
67
  try:
34
68
  http_url: AnyHttpUrl = TypeAdapter(AnyHttpUrl).validate_python(source)
@@ -44,29 +78,98 @@ def resolve_file_source(
44
78
  # fetch the page
45
79
  res = requests.get(http_url, stream=True, headers=req_headers)
46
80
  res.raise_for_status()
47
- fname = None
48
- # try to get filename from response header
49
- if cont_disp := res.headers.get("Content-Disposition"):
50
- for par in cont_disp.strip().split(";"):
51
- # currently only handling directive "filename" (not "*filename")
52
- if (split := par.split("=")) and split[0].strip() == "filename":
53
- fname = "=".join(split[1:]).strip().strip("'\"") or None
54
- break
55
- # otherwise, use name from URL:
56
- if fname is None:
57
- fname = Path(http_url.path or "").name or "file"
58
- local_path = Path(tempfile.mkdtemp()) / fname
59
- with open(local_path, "wb") as f:
60
- for chunk in res.iter_content(chunk_size=1024): # using 1-KB chunks
61
- f.write(chunk)
81
+ fname = resolve_remote_filename(http_url=http_url, response_headers=res.headers)
82
+
83
+ stream = BytesIO(res.content)
84
+ doc_stream = DocumentStream(name=fname, stream=stream)
62
85
  except ValidationError:
63
86
  try:
64
87
  local_path = TypeAdapter(Path).validate_python(source)
88
+ stream = BytesIO(local_path.read_bytes())
89
+ doc_stream = DocumentStream(name=local_path.name, stream=stream)
65
90
  except ValidationError:
66
91
  raise ValueError(f"Unexpected source type encountered: {type(source)}")
92
+ return doc_stream
93
+
94
+
95
+ def _resolve_source_to_path(
96
+ source: Union[Path, AnyHttpUrl, str],
97
+ headers: Optional[Dict[str, str]] = None,
98
+ workdir: Optional[Path] = None,
99
+ ) -> Path:
100
+ doc_stream = resolve_source_to_stream(source=source, headers=headers)
101
+
102
+ # use a temporary directory if not specified
103
+ if workdir is None:
104
+ workdir = Path(tempfile.mkdtemp())
105
+
106
+ # create the parent workdir if it doesn't exist
107
+ workdir.mkdir(exist_ok=True, parents=True)
108
+
109
+ # save result to a local file
110
+ local_path = workdir / doc_stream.name
111
+ with local_path.open("wb") as f:
112
+ f.write(doc_stream.stream.read())
113
+
67
114
  return local_path
68
115
 
69
116
 
117
+ def resolve_source_to_path(
118
+ source: Union[Path, AnyHttpUrl, str],
119
+ headers: Optional[Dict[str, str]] = None,
120
+ workdir: Optional[Path] = None,
121
+ ) -> Path:
122
+ """Resolves the source (URL, path) of a file to a local file path.
123
+
124
+ If a URL is provided, the content is first downloaded to a local file, located in
125
+ the provided workdir or in a temporary directory if no workdir provided.
126
+
127
+ Args:
128
+ source (Path | AnyHttpUrl | str): The file input source. Can be a path or URL.
129
+ headers (Dict | None): Optional set of headers to use for fetching
130
+ the remote URL.
131
+ workdir (Path | None): If set, the work directory where the file will
132
+ be downloaded, otherwise a temp dir will be used.
133
+
134
+ Raises:
135
+ ValueError: If source is of unexpected type.
136
+
137
+ Returns:
138
+ Path: The local file path.
139
+ """
140
+ return _resolve_source_to_path(
141
+ source=source,
142
+ headers=headers,
143
+ workdir=workdir,
144
+ )
145
+
146
+
147
+ @deprecated("Use `resolve_source_to_path()` or `resolve_source_to_stream()` instead")
148
+ def resolve_file_source(
149
+ source: Union[Path, AnyHttpUrl, str],
150
+ headers: Optional[Dict[str, str]] = None,
151
+ ) -> Path:
152
+ """Resolves the source (URL, path) of a file to a local file path.
153
+
154
+ If a URL is provided, the content is first downloaded to a temporary local file.
155
+
156
+ Args:
157
+ source (Path | AnyHttpUrl | str): The file input source. Can be a path or URL.
158
+ headers (Dict | None): Optional set of headers to use for fetching
159
+ the remote URL.
160
+
161
+ Raises:
162
+ ValueError: If source is of unexpected type.
163
+
164
+ Returns:
165
+ Path: The local file path.
166
+ """
167
+ return _resolve_source_to_path(
168
+ source=source,
169
+ headers=headers,
170
+ )
171
+
172
+
70
173
  def relative_path(src: Path, target: Path) -> Path:
71
174
  """Compute the relative path from `src` to `target`.
72
175
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: docling-core
3
- Version: 2.5.0
3
+ Version: 2.6.0
4
4
  Summary: A python library to define and validate data types in Docling.
5
5
  Home-page: https://ds4sd.github.io/
6
6
  License: MIT
@@ -32,6 +32,7 @@ Requires-Dist: pillow (>=10.3.0,<11.0.0)
32
32
  Requires-Dist: pydantic (>=2.6.0,<2.10)
33
33
  Requires-Dist: pyyaml (>=5.1,<7.0.0)
34
34
  Requires-Dist: tabulate (>=0.9.0,<0.10.0)
35
+ Requires-Dist: typing-extensions (>=4.12.2,<5.0.0)
35
36
  Project-URL: Repository, https://github.com/DS4SD/docling-core
36
37
  Description-Content-Type: text/markdown
37
38
 
@@ -21,10 +21,11 @@ docling_core/types/__init__.py,sha256=MVRSgsk5focwGyAplh_TRR3dEecIXpd98g_u3zZ5HX
21
21
  docling_core/types/base.py,sha256=PusJskRVL19y-hq0BgXr5e8--QEqSqLnFNJ8UbOqW88,8318
22
22
  docling_core/types/doc/__init__.py,sha256=bEL4zKVOG7Wxm6xQrgF58mu-Teds9aSavuEAKVNhrTU,639
23
23
  docling_core/types/doc/base.py,sha256=_ttU8QI8wXDTQRUnN5n7L6D9wYFVLSAibxlFoMbgAsk,4557
24
- docling_core/types/doc/document.py,sha256=05q8KZ64TVpxJoegPy7MOlvI0fmqUtKRKZMGsdvUz9c,85711
24
+ docling_core/types/doc/document.py,sha256=K6ixUeB0vyrnd3_ljM0Ed_8JBdltLPCsrGz7IoLgjUI,87094
25
25
  docling_core/types/doc/labels.py,sha256=A8vWP82VAeXO1rlCO0oDKo_Hb8uDeQe0myOTY3P03hk,1596
26
26
  docling_core/types/gen/__init__.py,sha256=C6TuCfvpSnSL5XDOFMcYHUY2-i08vvfOGRcdu6Af0pI,124
27
27
  docling_core/types/gen/generic.py,sha256=l4CZ4_Lb8ONG36WNJWbKX5hGKvTh_yU-hXp5hsm7uVU,844
28
+ docling_core/types/io/__init__.py,sha256=7QYvFRaDE0AzBg8e7tvsVNlLBbCbAbQ9rP2TU8aXR1k,350
28
29
  docling_core/types/legacy_doc/__init__.py,sha256=Pzj_8rft6SJTVTCHgXRwHtuZjL6LK_6dcBWjikL9biY,125
29
30
  docling_core/types/legacy_doc/base.py,sha256=l8NKCuORUQ1ebjdGWpj6b30oQEvtErLsIHKQHbbJiPg,14683
30
31
  docling_core/types/legacy_doc/doc_ann.py,sha256=CIQHW8yzu70bsMR9gtu7dqe4oz603Tq2eDDt9sh-tYo,1203
@@ -44,13 +45,13 @@ docling_core/types/rec/statement.py,sha256=YwcV4CbVaAbzNwh14yJ_6Py3Ww0XnUJrEEUiK
44
45
  docling_core/types/rec/subject.py,sha256=PRCERGTMs4YhR3_Ne6jogkm41zYg8uUWb1yFpM7atm4,2572
45
46
  docling_core/utils/__init__.py,sha256=VauNNpWRHG0_ISKrsy5-gTxicrdQZSau6qMfuMl3iqk,120
46
47
  docling_core/utils/alias.py,sha256=B6Lqvss8CbaNARHLR4qSmNh9OkB6LvqTpxfsFmkLAFo,874
47
- docling_core/utils/file.py,sha256=ug4-z0KuthkEb_d5YDRPbY79PWfNSj9GYsi16xF2sDA,3699
48
+ docling_core/utils/file.py,sha256=B1Iu8buqk_Yz4bhrGf7NyFIiYlsa_MC37vZLwQHqKLU,6876
48
49
  docling_core/utils/generate_docs.py,sha256=BdKAoduWXOc7YMvcmlhjoJOFlUxij1ybxglj6LZDtC8,2290
49
50
  docling_core/utils/generate_jsonschema.py,sha256=uNX1O5XnjyB5nA66XqZXTt3YbGuR2tyi_OhHepHYtZg,1654
50
51
  docling_core/utils/validate.py,sha256=3FmnxnKTDZC5J9OGxCL3U3DGRl0t0bBV1NcySXswdas,2031
51
52
  docling_core/utils/validators.py,sha256=azcrndLzhNkTWnbFSu9shJ5D3j_znnLrIFA5R8hzmGU,2798
52
- docling_core-2.5.0.dist-info/LICENSE,sha256=2M9-6EoQ1sxFztTOkXGAtwUDJvnWaAHdB9BYWVwGkIw,1087
53
- docling_core-2.5.0.dist-info/METADATA,sha256=u4KdNbLkumFHT5HFI7XZo9AUeYryHHkH8iYpDDInA7Q,5468
54
- docling_core-2.5.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
55
- docling_core-2.5.0.dist-info/entry_points.txt,sha256=jIxlWv3tnO04irlZc0zfhqJIgz1bg9Hha4AkaLWSdUA,177
56
- docling_core-2.5.0.dist-info/RECORD,,
53
+ docling_core-2.6.0.dist-info/LICENSE,sha256=2M9-6EoQ1sxFztTOkXGAtwUDJvnWaAHdB9BYWVwGkIw,1087
54
+ docling_core-2.6.0.dist-info/METADATA,sha256=LhnsqU5AgndZllazTDXe_acmPWQ6NuMuH_b6-d4K1gM,5519
55
+ docling_core-2.6.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
56
+ docling_core-2.6.0.dist-info/entry_points.txt,sha256=jIxlWv3tnO04irlZc0zfhqJIgz1bg9Hha4AkaLWSdUA,177
57
+ docling_core-2.6.0.dist-info/RECORD,,