PyMuPDF Office

Enhance PyMuPDF capability with Office document support.

  • Enables Office document handling, including doc, docx, ppt, pptx, xls, xlsx, hwp, hwpx.

  • Supports text and table extraction, document conversion and more.

  • Includes everything from PyMuPDF

To enquire about obtaining a commercial license, then use this contact page.

Trial license keys are available for evaluation purposes. Please fill out the form on this page to obtain a trial key.

Platform support

Available for these platforms only:

  • Windows x86_64

  • Linux x86_64 (glibc)

  • MacOS x86_64

  • MacOS arm64

Office file support

In addition to the standard file types supported by PyMuPDF, PyMuPDF Office supports:

DOC/DOCX

XLS/XLSX

PPT/PPTX

HWP/HWPX

../_images/icon-docx.svg ../_images/icon-xlsx.svg ../_images/icon-pptx.svg ../_images/icon-hangul.svg

Usage

Installation

Install via pip with:

pip install pymupdf-office

Loading an Office document

Import PyMuPDF Office and you can then reference Office documents directly, e.g.:

import pymupdf.office
pymupdf.office.unlock()
# PyMuPDF has now been extended with PyMuPDF Office features, with some restrictions.
doc = pymupdf.open("my-office-doc.xls")

Note

All standard PyMuPDF functionality is exposed as expected - PyMuPDF Office handles the extended Office file types

From then on you can work with document pages just as you would do normally, but with respect to the restrictions.

Restrictions

PyMuPDF Office functionality is restricted without a license key as follows:

Only the first 3 pages of any document will be available.

To unlock full functionality you should obtain a trial key.

Trial keys

To obtain a license key please fill out the form on this page. You will then have the trial key emailled to the address you submitted.

Using a key

Initialize PyMuPDF Office with a key as follows:

import pymupdf.office
pymupdf.office.unlock(my_key)
# PyMuPDF has now been extended with PyMuPDF Office features.

This will allow you to evaluate the product for a limited time. If you want to use PyMuPDF Office after this time you should then enquire about obtaining a commercial license.

Converting

Office document to PDF

Use the to_pdf() method to convert an Office document to PDF, e.g.:

import pymupdf.office
pymupdf.office.unlock(my_key)

pymupdf.office.to_pdf("input.docx", "output.pdf")

If you require a byte representation of the PDF data, you can use the to_pdf() without specifying an output file, e.g.:

import pymupdf.office
pymupdf.office.unlock(my_key)

pdfdata = pymupdf.office.to_pdf("input.docx")

Office document to Images

In order to convert an Office document to images, you should iterate the document pages, convert to Pixmap and save, e.g.:

doc = pymupdf.open("input.docx")

for i, page in enumerate(doc):
    pix = page.get_pixmap(dpi=200)
    pix.save(f"page-{i+1}.png")

doc.close()

Office document to Markdown

Use the to_markdown() method to convert an Office document to Markdown, e.g.:

import pymupdf.office
pymupdf.office.unlock(my_key)

pymupdf.office.to_markdown("input.docx", "output.md")

If you require a string representation of the Markdown data, you can use the to_markdown() method without specifying an output file.

Office document to JSON

Use the to_json() method to convert an Office document to JSON, e.g.:

import pymupdf.office
pymupdf.office.unlock(my_key)

pymupdf.office.to_json("input.docx", "output.json")

If you require a string representation of the JSON data, you can use the to_json() method without specifying an output file.

Fonts

By default pymupdf.office.unlock() searches for all installed font directories.

This can be controlled with keyword-only args:

  • fontpath: specific font directories, either as a list/tuple or os.pathsep-separated string. If None (the default), we use os.environ['PYMUPDFPRO_FONT_PATH'] if set.

  • fontpath_auto: Whether to append system font directories. If None (the default) we use true if os.environ['PYMUPDFPRO_FONT_PATH_AUTO'] is ‘1’. If true we append all system font directories.

Function pymupdf.office.get_fontpath() returns a tuple of all font directories used by unlock().

API

unlock(my_key: str = None, *, fontpath: str | list | tuple = None, fontpath_auto: bool = None)

Unlocks the PyMuPDF Office functionality with the provided key.

Parameters:
  • my_key (str) – the license key for PyMuPDF Office.

  • fontpath (str | list | tuple) – specific font directories, either as a list/tuple or os.pathsep-separated string.

  • fontpath_auto (bool) – whether to append system font directories. If None (the default) we use True if os.environ['PYMUPDFPRO_FONT_PATH_AUTO'] is unset or ‘1’, otherwise False.

Grants access to the full functionality of PyMuPDF Office. For a free trial key please visit: https://pymupdf.io/office/try/.

to_pdf(input_path: str, output_path: str = None) → bytes | None

Reads the input file and converts its contents into PDF format.

Parameters:
  • input_path (str) – the input file path.

  • output_path (str) – the path to the output file. If None (the default), the method returns the PDF content as bytes.

Returns:

bytes of the PDF content or None if output_path is specified.

to_markdown(input_path: str, output_path: str = None) → str | None

Reads the input file and outputs the text of its pages in Markdown format.

Parameters:
  • input_path (str) – the input file path.

  • output_path (str) – the path to the output file. If None, the method returns the content as a string.

Returns:

string of the Markdown representation or None if output_path is specified.

to_json(input_path: str, output_path: str = None) → str | None

Reads the input file and outputs the text of its pages in JSON format.

Parameters:
  • input_path (str) – the input file path.

  • output_path (str) – the path to the output file. If None, the method returns the content as a string.

Returns:

string of the JSON representation or None if output_path is specified.

This software is provided AS-IS with no warranty, either express or implied. This software is distributed under license and may not be copied, modified or distributed except as expressly authorized under the terms of that license. Refer to licensing information at artifex.com or contact Artifex Software Inc., 39 Mesa Street, Suite 108A, San Francisco CA 94129, United States for further information.