When the source document is not a file on disk (e.g., downloaded from a network, read from a database, or received as an upload), you can pass a binary stream directly to the MarkdownConverter constructor.
The stream is copied internally, so you may close it immediately after creating the converter.
fromgroupdocs.markdownimportMarkdownConverterdefload_from_stream():"""Load a document from a binary stream and convert to Markdown."""# Step 1: Open the document file as a binary streamwithopen("business-plan.docx","rb")asstream:# Step 2: Create a converter from the streamwithMarkdownConverter(stream)asconverter:# Step 3: Convert and save the Markdown outputconverter.convert("load-stream-basic.md")if__name__=="__main__":load_from_stream()
business-plan.docx is sample file used in this example. Click here to download it.
If the stream does not have a file extension for automatic format detection, specify the format explicitly via LoadOptions:
fromgroupdocs.markdownimportMarkdownConverter,LoadOptions,FileFormatdefload_from_stream_with_options():"""Load a document from a stream with explicit format specification."""# Step 1: Open the file as a binary streamwithopen("document","rb")asstream:# Step 2: Specify the file format explicitly (no auto-detection)load_options=LoadOptions(FileFormat.DOCX)# Step 3: Create a converter from the stream with load optionswithMarkdownConverter(stream,load_options=load_options)asconverter:# Step 4: Convert and save the Markdown outputconverter.convert("load-stream-with-options.md")if__name__=="__main__":importshutilshutil.copy("business-plan.docx","document")# create file without extensionload_from_stream_with_options()