| 78 | |
| 79 | |
| 80 | def export_documents( |
| 81 | conv_results: Iterable[ConversionResult], |
| 82 | output_dir: Path, |
| 83 | export_json: bool, |
| 84 | export_html: bool, |
| 85 | export_md: bool, |
| 86 | export_txt: bool, |
| 87 | export_doctags: bool, |
| 88 | image_export_mode: ImageRefMode, |
| 89 | ): |
| 90 | |
| 91 | success_count = 0 |
| 92 | failure_count = 0 |
| 93 | |
| 94 | for conv_res in conv_results: |
| 95 | if conv_res.status == ConversionStatus.SUCCESS: |
| 96 | success_count += 1 |
| 97 | doc_filename = conv_res.input.file.stem |
| 98 | |
| 99 | # Export JSON format: |
| 100 | if export_json: |
| 101 | fname = output_dir / f"{doc_filename}.json" |
| 102 | _log.info(f"writing JSON output to {fname}") |
| 103 | conv_res.document.save_as_json( |
| 104 | filename=fname, image_mode=image_export_mode |
| 105 | ) |
| 106 | |
| 107 | # Export HTML format: |
| 108 | if export_html: |
| 109 | fname = output_dir / f"{doc_filename}.html" |
| 110 | _log.info(f"writing HTML output to {fname}") |
| 111 | conv_res.document.save_as_html( |
| 112 | filename=fname, image_mode=image_export_mode |
| 113 | ) |
| 114 | |
| 115 | # Export Text format: |
| 116 | if export_txt: |
| 117 | fname = output_dir / f"{doc_filename}.txt" |
| 118 | _log.info(f"writing TXT output to {fname}") |
| 119 | conv_res.document.save_as_markdown( |
| 120 | filename=fname, |
| 121 | strict_text=True, |
| 122 | image_mode=ImageRefMode.PLACEHOLDER, |
| 123 | ) |
| 124 | |
| 125 | # Export Markdown format: |
| 126 | if export_md: |
| 127 | fname = output_dir / f"{doc_filename}.md" |
| 128 | _log.info(f"writing Markdown output to {fname}") |
| 129 | conv_res.document.save_as_markdown( |
| 130 | filename=fname, image_mode=image_export_mode |
| 131 | ) |
| 132 | |
| 133 | # Export Document Tags format: |
| 134 | if export_doctags: |
| 135 | fname = output_dir / f"{doc_filename}.doctags" |
| 136 | _log.info(f"writing Doc Tags output to {fname}") |
| 137 | conv_res.document.save_as_document_tokens(filename=fname) |