Skip to main content

mcad2py

Convert PTC Mathcad Prime worksheets (.mcdx) into runnable Python — either a Jupyter notebook (one region per cell, results echoed inline like Mathcad's =) or a plain .py script. Units are preserved with Pint.

A .mcdx file is a zip archive of XML; the math is stored as an expression tree, so the conversion walks that tree into an intermediate representation and emits Python from it.

Install

pip install mcad2py

Or from a clone, for development:

git clone https://github.com/ChristianCruger/mcad2py
cd mcad2py
pip install -e ".[dev]"

Requires Python 3.10+. Dependencies: pint, nbformat, sympy, numpy, scipy, matplotlib, Pillow.

Usage

# Jupyter notebook (default)
mcad2py convert worksheet.mcdx                 # -> worksheet.ipynb
mcad2py convert worksheet.mcdx -o out.ipynb

# Plain Python script
mcad2py convert worksheet.mcdx -f py           # -> worksheet.py
mcad2py convert worksheet.mcdx -o - -f py      # to stdout

Or from Python:

from mcad2py import convert_file
print(convert_file("worksheet.mcdx", fmt="py"))

Example

A Mathcad region f_cd := 30 MPa / 1.5 = (displayed in MPa) becomes, in a notebook cell:

f_cd = 30 * ureg.MPa / 1.5
f_cd.to(ureg.MPa)        # the bare last line echoes "20 megapascal", like Mathcad's "="

Mathcad trig handles angle units automatically; generated code matches this via small angle-aware helpers (from mcad2py.runtime import sin, cos, tan, cot), so tan(phi) works whether phi is in deg or rad.

Converted code needs mcad2py installed to run. The output isn't standalone: it imports mcad2py.runtime for the unit- and shape-aware helpers that give Mathcad's semantics rather than Python's (angle-aware trig, matmul, index_build, solve_block, …). If you send a converted .py or .ipynb to someone else, they need pip install mcad2py too — along with the dependencies above, since the generated code uses Pint, NumPy, SciPy and matplotlib directly.

What's supported

  • := definitions and inline = evaluations, with display-unit overrides
  • Arithmetic, unit scaling (30 MPa), roots, %, comparisons and boolean connectives
  • Constants (π, e, ), Greek/subscripted identifiers (f_cd, β, ϕf_cd, beta, phi)
  • Vectors and matrices — literals, 0-based indexing (including the two-subscript form), ranges, range-built vectors and matrices, and the full built-in family: rows/cols/identity/ augment/submatrix, det/lsolve/geninv/rank/rref, the norm and condition sets, the eigen and singular-value set, sort/reverse. Mathcad's one · is resolved into a scalar, matrix or dot product by inferring shapes across the whole sheet.
  • Functions — the complete trigonometric and hyperbolic families (angle-aware), logs, powers and roots, rounding, min/max, linterp
  • Programs — inline and block if, plus multi-line imperative programs (loops, local assignments, return, try) emitted as real Python defs
  • Solve blocks — numeric Given/Find (via scipy.optimize), including a block that defines a function; symbolic solve/simplify/factor/expand via SymPy
  • Integrals and sums — definite and double integrals (scipy.integrate), indexed Σ
  • Plots — x-y (function and parametric, including a plot whose variable is never defined — Mathcad's invented -10..10 domain), contour and 3D, rendered with matplotlib
  • Controls and tables — ComboBox row selectors, data tables, scriptable controls (via their cached value), text/comment regions → markdown cells
  • Images embedded from picture regions

Anything not yet handled is emitted as a clearly marked # TODO unsupported stub rather than dropped, so the output always loads. See docs/mathcad-function-coverage.md for the full category-by-category map and what's next.

Tests

The suite lives in the git repository (it is not part of the published package, since each test converts and executes a real .mcdx fixture from references/):

git clone https://github.com/ChristianCruger/mcad2py
cd mcad2py
pip install -e ".[dev]"
pytest

Every test converts a reference worksheet, runs the generated code, and checks the computed values against Mathcad's own cached results in that file's result.xml.

Scope

Targets Mathcad Prime (.mcdx, worksheet50/math50 schema). Legacy Mathcad 15 (.xmcd) is a future target — the parser is structured behind an intermediate representation so a second front-end can be added without changing the code generators.

License

MIT — see LICENSE.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mcad2py-0.3.0.tar.gz (86.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mcad2py-0.3.0-py3-none-any.whl (90.2 kB view details)

Uploaded Python 3

File details

Details for the file mcad2py-0.3.0.tar.gz.

File metadata

  • Download URL: mcad2py-0.3.0.tar.gz
  • Upload date:
  • Size: 86.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for mcad2py-0.3.0.tar.gz
Algorithm Hash digest
SHA256 eeb3cec06dfa2720382b6cab38dc9b9aea9398414c8ad5e60b1a0101fb7a0607
MD5 128374625ae5b6abe55c06d66ab93c61
BLAKE2b-256 d7b54e0c8bb29f0c4e1263e11d201199d698131a86b2a9516fe723c82e992c21

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcad2py-0.3.0.tar.gz:

Publisher: publish.yml on ChristianCruger/mcad2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mcad2py-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: mcad2py-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 90.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for mcad2py-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3395024ccd58cf759b92b1aac76c50bd893fbbcefc4376d87b7426fe3a4b919b
MD5 1da945e1b466f0078cb854f35e22e8a8
BLAKE2b-256 1a1cd8275b06b607690403eb2ed9c66cf3d7c276ba5595af27a77f76be39cdd9

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcad2py-0.3.0-py3-none-any.whl:

Publisher: publish.yml on ChristianCruger/mcad2py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page