461bf6fd40
CI / lint (3.11) (push) Has been cancelled
CI / lint (3.12) (push) Has been cancelled
CI / lint (3.13) (push) Has been cancelled
CI / shellcheck (push) Has been cancelled
CI / shfmt (push) Has been cancelled
CI / setup (3.11) (push) Has been cancelled
CI / setup (3.12) (push) Has been cancelled
CI / setup (3.13) (push) Has been cancelled
CI / check-licenses (3.12) (push) Has been cancelled
CI / test_unit (3.11) (push) Has been cancelled
CI / test_unit (3.12) (push) Has been cancelled
CI / test_unit (3.13) (push) Has been cancelled
CI / test_unit_no_extras (3.11) (push) Has been cancelled
CI / test_unit_no_extras (3.12) (push) Has been cancelled
CI / test_json_to_html (3.12) (push) Has been cancelled
CI / test_unit_no_extras (3.13) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.12, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.11, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.12, --extra xlsx) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.11, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (csv, 3.13, --extra csv) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.11, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.12, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (docx, 3.13, --extra docx) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.11, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.12, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (markdown, 3.13, --extra md) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.11, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.12, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (odt, 3.13, --extra odt) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.11, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.12, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pdf-image, 3.13, --extra pdf --extra image --extra paddleocr) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.11, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.12, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pptx, 3.13, --extra pptx) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.11, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.12, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
CI / test_unit_dependency_extras (pypandoc, 3.13, --extra epub --extra org --extra rtf --extra rst) (push) Has been cancelled
Build And Push Docker Image / set-short-sha (push) Has been cancelled
Partition Benchmark / setup (push) Has been cancelled
Partition Benchmark / Measure and compare partition() runtime (push) Has been cancelled
CI / test_unit_dependency_extras (xlsx, 3.13, --extra xlsx) (push) Has been cancelled
CI / test_ingest_src (3.12) (push) Has been cancelled
CI / test_json_to_markdown (3.12) (push) Has been cancelled
CI / changelog (push) Has been cancelled
CI / test_dockerfile (push) Has been cancelled
CodeQL / Analyze (python) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/amd64, opensource-linux-8core) (push) Has been cancelled
Build And Push Docker Image / build-images (linux/arm64, ubuntu-24.04-arm) (push) Has been cancelled
Build And Push Docker Image / publish-images (push) Has been cancelled
82 lines
3.0 KiB
Python
82 lines
3.0 KiB
Python
from __future__ import annotations
|
|
|
|
import os
|
|
import re
|
|
import tempfile
|
|
from typing import IO
|
|
|
|
from unstructured.errors import UnprocessableEntityError
|
|
from unstructured.partition.common.common import exactly_one
|
|
from unstructured.utils import requires_dependencies
|
|
|
|
|
|
@requires_dependencies(["pypandoc"])
|
|
def convert_file_to_text(filename: str, source_format: str, target_format: str) -> str:
|
|
"""Uses pandoc to convert the source document to a raw text string."""
|
|
import pypandoc
|
|
|
|
try:
|
|
text: str = pypandoc.convert_file(
|
|
filename, target_format, format=source_format, sandbox=True
|
|
)
|
|
except FileNotFoundError as err:
|
|
msg = (
|
|
f"Error converting the file to text. Ensure you have the pandoc package installed on"
|
|
f" your system. Installation instructions are available at"
|
|
f" https://pandoc.org/installing.html. The original exception text was:\n{err}"
|
|
)
|
|
raise FileNotFoundError(msg)
|
|
except RuntimeError as err:
|
|
err_str = str(err)
|
|
if source_format == "epub" and (
|
|
"Couldn't extract ePub file" in err_str
|
|
or "No entry on path" in err_str
|
|
or re.search(r"exitcode ['\"]?64['\"]?", err_str)
|
|
):
|
|
raise UnprocessableEntityError(f"Invalid EPUB file: {err_str}")
|
|
|
|
supported_source_formats, _ = pypandoc.get_pandoc_formats()
|
|
|
|
if source_format == "rtf" and source_format not in supported_source_formats:
|
|
additional_info = (
|
|
"Support for RTF files is not available in the current pandoc installation. "
|
|
"It was introduced in pandoc 2.14.2.\n"
|
|
"Reference: https://pandoc.org/releases.html#pandoc-2.14.2-2021-08-21"
|
|
)
|
|
else:
|
|
additional_info = ""
|
|
|
|
msg = (
|
|
f"{err}\n\n{additional_info}\n\n"
|
|
f"Current version of pandoc: {pypandoc.get_pandoc_version()}\n"
|
|
"Make sure you have the right version installed in your system. Please follow the"
|
|
" pandoc installation instructions in README.md to install the right version."
|
|
)
|
|
raise RuntimeError(msg)
|
|
|
|
return text
|
|
|
|
|
|
def convert_file_to_html_text_using_pandoc(
|
|
source_format: str, filename: str | None = None, file: IO[bytes] | None = None
|
|
) -> str:
|
|
"""Converts a document to HTML raw text.
|
|
|
|
Enables the doucment to be processed using `partition_html()`.
|
|
"""
|
|
exactly_one(filename=filename, file=file)
|
|
|
|
if file is not None:
|
|
with tempfile.TemporaryDirectory() as temp_dir_path:
|
|
tmp_file_path = os.path.join(temp_dir_path, f"tmp_file.{source_format}")
|
|
with open(tmp_file_path, "wb") as tmp_file:
|
|
tmp_file.write(file.read())
|
|
return convert_file_to_text(
|
|
filename=tmp_file_path, source_format=source_format, target_format="html"
|
|
)
|
|
|
|
assert filename is not None
|
|
return convert_file_to_text(
|
|
filename=filename, source_format=source_format, target_format="html"
|
|
)
|