Python tool for converting files and office documents to Markdown.
fix: support -o param to avoid encoding issues (#116)
Soulter1123392306bf
diff --git a/README.md b/README.mdindex be265604..0fae1e6d 100644--- a/README.md+++ b/README.md@@ -29,6 +29,12 @@ To install MarkItDown, use pip: `pip install markitdown`. Alternatively, you canmarkitdown path-to-file.pdf > document.md```+Or use `-o` to specify the output file:++```bash+markitdown path-to-file.pdf -o document.md+```+You can also pipe content:```bashdiff --git a/src/markitdown/__main__.py b/src/markitdown/__main__.pyindex be2a0f2f..3193ae7e 100644--- a/src/markitdown/__main__.py+++ b/src/markitdown/__main__.py@@ -4,7 +4,7 @@import sysimport argparsefrom textwrap import dedent-from ._markitdown import MarkItDown+from ._markitdown import MarkItDown, DocumentConverterResultdef main():@@ -29,20 +29,42 @@ def main():ORmarkitdown < 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("filename", nargs="?")+ parser.add_argument(+ "-o",+ "--output",+ help="Output file name. If not provided, output is written to stdout.",+ )args = parser.parse_args()if args.filename is None:markitdown = MarkItDown()result = markitdown.convert_stream(sys.stdin.buffer)- print(result.text_content)+ _handle_output(args, result)else:markitdown = MarkItDown()result = markitdown.convert(args.filename)+ _handle_output(args, result)+++def _handle_output(args, result: DocumentConverterResult):+ """Handle output to stdout or file"""+ if args.output:+ with open(args.output, "w", encoding="utf-8") as f:+ f.write(result.text_content)+ else:print(result.text_content)