Typ2docx
Convert math-rich Typst project to Microsoft Word format
Install / Use
/learn @sghng/Typ2docxREADME
typ2docx: Convert Math-Rich Typst Project to Microsoft Word Format
typ2docx is a command line tool that converts a Typst project to Microsoft
Word .docx format, with tables, cross-references, most of the styles, and most
importantly the math markups preserved. It combines the mature, comprehensive
document conversion of standard PDF-to-Word tools with Pandoc's high-quality
mathematical formula export.
You're encouraged to read this document thoroughly before using it, as I employed many non-trivial hacks for this non-trivial problem! (It involves 6 different programming languages!)
If this tool enhanced your workflow, especially if it helped with your academic publication, please consider crediting this project or sponsoring me. :heart:
[!NOTE]
If you don't care about the quality of math export, this tool is no different from other PDF-to-Word converter.
If your project doesn't use any non-basic features in Typst, try Pandoc first.
Installation
[!NOTE]
You may now try this tool without installing through a web app. Note that it runs on my scarce free-tier API quota, so please install this tool if you intend to use it more than once!
Prerequisite
This tool is distributed via PyPI. Installation via
uv is recommended.
You may also use pipx or other similar tools to install and run this program.
For more details, read
uv's guide on using tools.
Tool Installation
Execute the following command to install:
uv tool install typ2docx
[!NOTE]
The package installation process is expected to take some time, since it requires compiling a Rust extension. Read along to know why.
If you want to tinker with this program:
git clone git@github.com:sghng/typ2docx.git
cd typ2docx
# do your modifications...
uv tool install .
Runtime Dependencies
The following runtime dependencies are also required:
- Pandoc, a universal document converter,
should be available in
PATH. - One of the supported
.pdfto.docxengines.
Nix
typ2docx is also available in nixpkgs
To install typ2docx on NixOS, put the following lines into your configuration.nix
environment.systemPackages = [
pkgs.typ2docx
];
To enter a shell environment with typ2docx:
# New nix3 commands
nix shell nixpkgs#typ2docx
# Legacy nix-shell
nix-shell -p typ2docx
Usage
Once the tool is installed, invoke it with the path to the entry point of your
Typst project and specify an engine to convert it into Microsoft Word .docx
format. For example:
typ2docx main.typ -e pdf2docx
Or, if you want better export quality and are ok with some additional steps:
# obtain your free Adobe PDF Services API key first
export PDF_SERVICES_CLIENT_ID=xxx
export PDF_SERVICES_CLIENT_SECRET=xxx
typ2docx main.typ -e pdfservices
Or, if you have Adobe Acrobat installed:
typ2docx --install-acrobat # only needed for first time using Acrobat engine
typ2docx main.typ -e acrobat
Run typ2docx --help to see the help info on how to use this tool.
PDF to DOCX Engines
You need to specify the engine used to convert a PDF to .docx file. Currently
these are the available options:
pdf2docx: A Python package based on PyMuPDF. This engine is bundled withtyp2docxand can be used out of the box. The export quality of this engine is not as good as the others.- Adobe PDFServices API: It requires internet connection and valid PDFServices API credentials. This service comes with 500 free conversions per month, which should be enough for most people.
- Adobe Acrobat: It uses Acrobat's JavaScript
API to export a PDF to
.docx. Either the free Acrobat Reader or the paid Acrobat Pro would work.
What It Does and Does Not
There are some known issues -- which may or may not be a real issue depending on your use cases. Read the Motivation section to understand why I built this tool.
- Text in SVG/PDF images are distorted.
- Some spaces between inline equations and regular text are missing.
- Not all stylings are preserved. (This is expected, just like for any file format conversion.)
Similar Tools
- Adobe Acrobat does a great job in converting PDF
to
.docx, but the math equations are completely messed up. - Microsoft Word can also
convert a PDF to
.docxformat. In my experience, it doesn't work as well as Adobe Acrobat. pdf2docxPython library doesn't work for most of my PDF files.- Pandoc provides superb support for math markup when
converting
.typfile to.docx, but its support for Typst is very limited. For example, it doesn't recognize basic functions like#stroke. It also doesn't support latest features in Typst, such as embedding PDF as image. typliteis a tool developed by the author oftinymist. Its support for conversion to.docxis limited, as it relies on HTML as an intermediary. Styles and cross-references are lost, and math are rendered as images.
Motivation
This tool is developed so that a .docx export that meets the basic requirement
of academic paper submission can be produced. These requirements are very loose,
since the press has their own process for making a manuscript publication ready.
- Cross-referencing is NOT required.
- Figures are NOT required. They can be included as standalone attachments, as long as the names are matched.
- NO typesetting required.
With these said, the only true requirement is the quality of math equations, which must be retained effectively. And in Microsoft Word, it should be in Office Math Markup Language (OMML).
This tool is developed primarily to address the equation output problem.
Solution
The idea is to export with both Adobe Acrobat and Pandoc, and merge the best part in the two exports together.
- Branch 1
- A preamble is injected to the Typst project entry point, so that all math are rendered as markers.
- Typst compiles the project into PDF file.
- This PDF file is converted to Word format by some converter.
- Branch 2
- A Rust lib extracts all math source code in a Typst project.
- The source code are put into a new Typst source file, in order of their appearance in original document.
- This source file is converted to
.docxwith Pandoc, they are cleanly formatted with MathML.
- The two Microsoft Word files are unpacked. A XLST script merges the
document.xmlfiles by examining the markers. The result is finally repacked into a.docxfile as output.
The math source code can not be extracted with purely static analysis or regex
matching, since the location where a content is defined can be different from
where it shows up in document, and multiple source files can be involved via
#include and #import. This necessitates the use of typst and typst-eval
Rust crates for parsing as well as evaluating the Typst project.
Contribution
You are more than welcome to contribute by raising issues or opening pull requests!
Related Skills
himalaya
340.2kCLI to manage emails via IMAP/SMTP. Use `himalaya` to list, read, write, reply, forward, search, and organize emails from the terminal. Supports multiple accounts and message composition with MML (MIME Meta Language).
node-connect
340.2kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
claude-opus-4-5-migration
84.1kMigrate prompts and code from Claude Sonnet 4.0, Sonnet 4.5, or Opus 4.1 to Opus 4.5
frontend-design
84.1kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
