Document format conversion processor that supports conversion between multiple office document formats
Input column name | Note |
|---|---|
files | Input file path; supports TOS and https paths |
Output file path; only TOS paths are supported
If a parameter does not have a default value, it is required
Parameter name | Type | Default value | Description |
|---|---|---|---|
target_format | str | Target format (required); supports pdf, odt, html, txt, and docx | |
output_dir | str | Output path (required); can be a local path or TOS/S3 path | |
convert_time_out | int | 60 | Conversion timeout in seconds; default value: 60 |
The following code demonstrates how to use Daft (for distributed processing) to perform docx to pdf conversion.
# Copyright (c) Beijing Volcano Engine Technology Ltd. from __future__ import annotations import os import daft from daft import col from daft.las.functions.doc import DocConvert from daft.las.functions.udf import las_udf if __name__ == "__main__": # After conversion, the modified document will be saved to the specified TOS path. Therefore, you need to set environment variables to ensure write permissions to TOS, including: ACCESS_KEY, SECRET_KEY, TOS_ENDPOINT, TOS_REGION, TOS_TEST_DIR TOS_DIR = os.getenv("TOS_TEST_DIR", "tos_bucket") output_dir = f"tos://{TOS_DIR}/doc/doc_convert" if os.getenv("DAFT_RUNNER", "native") == "ray": import logging import ray def configure_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S.%s".format(), ) logging.getLogger("tracing.span").setLevel(logging.WARNING) logging.getLogger("daft_io.stats").setLevel(logging.WARNING) logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING) ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging}) daft.set_runner_ray() daft.set_execution_config(actor_udf_ready_timeout=600) daft.set_execution_config(min_cpu_per_task=0) tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com") samples = { "docx": [ f"https://{tos_dir_url}/public/shared_doc_dataset/sample.docx" ], } df = daft.from_pydict(samples) constructor_kwargs = {"target_format": "pdf", "output_dir": output_dir} # Distributed processing using Daft df = df.with_column( "pdf", las_udf(DocConvert, construct_args=constructor_kwargs, concurrency=1)(col("docx")), ) df.show() # ╭──────────────────────────────────────────────┬──────────────────────────────────────────────╮ # │ docx ┆ pdf │ # │ --- ┆ --- │ # │ Utf8 ┆ Utf8 │ # ╞══════════════════════════════════════════════╪══════════════════════════════════════════════╡ # │ https://las-cn-beijing-publi-… ┆ tos://tos_bucket/doc/doc_convert/sample.pdf │ # ╰──────────────────────────────────────────────┴──────────────────────────────────────────────╯