All static and instance methods have async counterparts. File I/O is truly asynchronous — source files are read and output files are written using async streams, while the CPU-bound conversion itself runs on a worker thread so your event loop stays responsive.
Note
Async methods are designed for use with Python’s built-in asyncio framework. Call them with await from inside a coroutine and run the coroutine with asyncio.run().
Static async methods
importasynciofromgroupdocs.markdownimportMarkdownConverter,ConvertOptionsasyncdefasync_static():"""Use the static async API to convert a document and inspect its metadata."""# Step 1: Convert to a Markdown stringmd=awaitMarkdownConverter.to_markdown_async("business-plan.docx")# Step 2: Convert and save directly to a fileawaitMarkdownConverter.to_file_async("business-plan.docx","async-static.md",None)# Step 3: Convert with optionsoptions=ConvertOptions()options.include_front_matter=TrueawaitMarkdownConverter.to_file_async("business-plan.docx","async-static.md",options)# Step 4: Retrieve document info without a full conversioninfo=awaitMarkdownConverter.get_info_async("business-plan.docx",None)# info.file_format, info.page_countif__name__=="__main__":asyncio.run(async_static())
business-plan.docx is sample file used in this example. Click here to download it.
importasynciofromgroupdocs.markdownimportMarkdownConverter,ConvertOptionsasyncdefasync_instance():"""Use the instance async API with a context manager."""# Step 1: Open the document with a context managerwithMarkdownConverter("business-plan.docx")asconverter:# Step 2: Configure conversion optionsoptions=ConvertOptions()options.heading_level_offset=1# Step 3: Convert and save the Markdown output asynchronouslyawaitconverter.convert_async("async-instance.md",options)# Step 4: Retrieve document metadata asynchronouslyinfo=awaitconverter.get_document_info_async()# info.file_format, info.page_countif__name__=="__main__":asyncio.run(async_instance())
business-plan.docx is sample file used in this example. Click here to download it.
Use asyncio.gather() to convert multiple documents in parallel while the event loop stays free for other work:
importasynciofromgroupdocs.markdownimportMarkdownConverterasyncdefasync_concurrent():"""Convert several documents concurrently with asyncio.gather()."""# Step 1: Define the source files and their output targetsjobs=[("business-plan.docx","async-concurrent-docx.md"),("business-plan.pdf","async-concurrent-pdf.md"),("cost-analysis.xlsx","async-concurrent-xlsx.md"),]# Step 2: Launch every conversion concurrentlyawaitasyncio.gather(*(MarkdownConverter.to_file_async(src,dst,None)forsrc,dstinjobs))if__name__=="__main__":asyncio.run(async_concurrent())
business-plan.docx is sample file used in this example. Click here to download it.
business-plan.pdf is sample file used in this example. Click here to download it.
cost-analysis.xlsx is sample file used in this example. Click here to download it.
The async API is a natural fit for ASGI frameworks such as FastAPI — file I/O does not block the event loop, so a single worker can handle many concurrent conversion requests:
importosimporttempfilefromfastapiimportFastAPI,UploadFilefromfastapi.responsesimportPlainTextResponsefromgroupdocs.markdownimportMarkdownConverter,ConvertOptions,MarkdownFlavorapp=FastAPI()@app.post("/convert",response_class=PlainTextResponse)asyncdefasync_fastapi(file:UploadFile):"""Accept an uploaded document and return Markdown without blocking the event loop."""# Stream the upload to a temp file so GroupDocs.Markdown can open itsuffix=os.path.splitext(file.filename)[1]withtempfile.NamedTemporaryFile(delete=False,suffix=suffix)astmp:tmp.write(awaitfile.read())tmp_path=tmp.nameoptions=ConvertOptions()options.flavor=MarkdownFlavor.GIT_HUBtry:returnawaitMarkdownConverter.to_markdown_async(tmp_path,convert_options=options)finally:os.unlink(tmp_path)
Was this page helpful?
Any additional feedback you'd like to share with us?
Please tell us how we can improve this page.
Thank you for your feedback!
We value your opinion. Your feedback will help us improve our documentation.
On this page
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.