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.2.0.tar.gz (69.0 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.2.0-py3-none-any.whl (71.7 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for mcad2py-0.2.0.tar.gz
Algorithm Hash digest
SHA256 bda36524347915a275b2795fa9e1c19ec3c9d35a8161e5646c7f112fbe8d8620
MD5 ff33a429a405e136eb1121e791848e76
BLAKE2b-256 089c3ec150147d16bf56e45f77af5a7728678767a3c735290dea507ea7d09adf

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcad2py-0.2.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.2.0-py3-none-any.whl.

File metadata

  • Download URL: mcad2py-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 71.7 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 46a079fcfa93d94b4ed2aa9d6d68dd807d3f103123ec352278f6ed83edeca940
MD5 bcb0a729445d45439a066e2e1aeff939
BLAKE2b-256 b5e48c8c47ca7f51a373e8c5a5053d5528b4a2a302ae6a1f38a5fc54b7a528c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for mcad2py-0.2.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