parent
82d84e3edd
commit
c0c0533a5e
1 changed files with 13 additions and 42 deletions
|
|
@ -14,36 +14,14 @@ def main():
|
||||||
description="Convert various file formats to markdown.",
|
description="Convert various file formats to markdown.",
|
||||||
prog="markitdown",
|
prog="markitdown",
|
||||||
formatter_class=argparse.RawDescriptionHelpFormatter,
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
||||||
usage=dedent(
|
epilog=dedent(
|
||||||
"""
|
"""\
|
||||||
SYNTAX:
|
examples:
|
||||||
|
markitdown example.pdf
|
||||||
markitdown <OPTIONAL: FILENAME>
|
markitdown -o example.md example.pdf
|
||||||
If FILENAME is empty, markitdown reads from stdin.
|
cat example.pdf | markitdown > example.md"""
|
||||||
|
),
|
||||||
EXAMPLE:
|
|
||||||
|
|
||||||
markitdown example.pdf
|
|
||||||
|
|
||||||
OR
|
|
||||||
|
|
||||||
cat example.pdf | markitdown
|
|
||||||
|
|
||||||
OR
|
|
||||||
|
|
||||||
markitdown < example.pdf
|
|
||||||
|
|
||||||
OR to save to a file use
|
|
||||||
|
|
||||||
markitdown example.pdf -o example.md
|
|
||||||
|
|
||||||
OR
|
|
||||||
|
|
||||||
markitdown example.pdf > example.md
|
|
||||||
"""
|
|
||||||
).strip(),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-v",
|
"-v",
|
||||||
"--version",
|
"--version",
|
||||||
|
|
@ -51,41 +29,39 @@ def main():
|
||||||
version=f"%(prog)s {__version__}",
|
version=f"%(prog)s {__version__}",
|
||||||
help="show the version number and exit",
|
help="show the version number and exit",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-o",
|
"-o",
|
||||||
"--output",
|
"--output",
|
||||||
help="Output file name. If not provided, output is written to stdout.",
|
dest="filename",
|
||||||
|
help="if unspecified, defaults to stdout",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-d",
|
"-d",
|
||||||
"--use-docintel",
|
"--use-docintel",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Use Document Intelligence to extract text instead of offline conversion. Requires a valid Document Intelligence Endpoint.",
|
help="Use Document Intelligence to extract text instead of offline conversion. Requires a valid Document Intelligence Endpoint.",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-e",
|
"-e",
|
||||||
"--endpoint",
|
"--endpoint",
|
||||||
type=str,
|
type=str,
|
||||||
help="Document Intelligence Endpoint. Required if using Document Intelligence.",
|
help="Document Intelligence Endpoint. Required if using Document Intelligence.",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"-p",
|
"-p",
|
||||||
"--use-plugins",
|
"--use-plugins",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="Use 3rd-party plugins to convert files. Use --list-plugins to see installed plugins.",
|
help="Use 3rd-party plugins to convert files. Use --list-plugins to see installed plugins.",
|
||||||
)
|
)
|
||||||
|
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
"--list-plugins",
|
"--list-plugins",
|
||||||
action="store_true",
|
action="store_true",
|
||||||
help="List installed 3rd-party plugins. Plugins are loaded when using the -p or --use-plugin option.",
|
help="List installed 3rd-party plugins. Plugins are loaded when using the -p or --use-plugin option.",
|
||||||
)
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"filename", nargs="?", help="if unspecified, defaults to stdin"
|
||||||
|
)
|
||||||
|
|
||||||
parser.add_argument("filename", nargs="?")
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.list_plugins:
|
if args.list_plugins:
|
||||||
|
|
@ -123,14 +99,9 @@ def main():
|
||||||
else:
|
else:
|
||||||
result = markitdown.convert(args.filename)
|
result = markitdown.convert(args.filename)
|
||||||
|
|
||||||
_handle_output(args, result)
|
|
||||||
|
|
||||||
|
|
||||||
def _handle_output(args, result: DocumentConverterResult):
|
|
||||||
"""Handle output to stdout or file"""
|
|
||||||
if args.output:
|
if args.output:
|
||||||
with open(args.output, "w", encoding="utf-8") as f:
|
with open(args.output, "w", encoding="utf-8") as f:
|
||||||
f.write(result.text_content)
|
print(result.text_content, file=f)
|
||||||
else:
|
else:
|
||||||
print(result.text_content)
|
print(result.text_content)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue