3. Interacting with Mathcad using Python - MattWoodhead/MathcadPy GitHub Wiki
The instructions on this page assume you have successfully completed the steps in Designating Inputs and Outputs in Mathcad.
Create a Python script
Using your prefered editor, create a new Python script. Add the following lines at the top of the script:
# Standard Library Imports
from pathlib import Path
# External library Imports
from MathcadPy import Mathcad
mathcad_app = Mathcad() # creates an instance of the Mathcad class - this object represents the Mathcad application
mathcad_app is an instance of the Mathcad() class from the Mathcadpy module. This class contains methods and attributes that will allow us to interact with Mathcad Prime via its built in COM API.
We now need a Mathcad file with which we are going to interact. You can use documentation/Examples/simple_example_complete.mcdx if you do not have your own Mathcad file with which to test the script (Note: This file was created in Mathcad Prime 5 so will not open in earlier versions). The example code below assumes that this Mathcad file is located within the same directory as the Python script.
First steps - fetch the variable Aliases from Mathcad
We can provide the filepath to the Mathcad file that we want to open. If this file is already open in a running instance of Mathcad, the API will interact with the open instance rather than opening a second copy. Add the following lines below:
mathcad_worksheet = mathcad_app.open(Path.cwd() / "simple_example_complete.mcdx")
print(mathcad_worksheet.inputs())
print(mathcad_worksheet.outputs())
Save your script, and now run it using your Python interpreter with Mathcadpy installed (You can run the code directly from many editors, such as Spyder). You should see a result that looks like the below:
The values returned by the inputs() and outputs() methods match the Aliases defined in the Mathcad document.
Set input variable values using Python
With the link to Mathcad established and the names of the inputs and outputs verified, we can now begin to interact with the values in the worksheet. At the end of the file we began in the above steps, add the following lines:
print(f"Old input value: {mathcad_worksheet.get_input('input_1')}") # only here for debugging
mathcad_worksheet.set_real_input("input_1", 2) # change the value in Mathcad programmatically
print(f"New input value: {mathcad_worksheet.get_input('input_1')}") # only here for debugging
mathcad_worksheet.set_real_input("input_2", 4) # change the value in Mathcad programmatically
Save the script, and then run it as before. You should receive something similar to the following output:
Note that returned value from Mathcad is in the form of a Tuple, with the first element being the value, the second value being the units (in this instance there are none), and the third value being an error code.
Fetch Output variable values using Python
Fetching an output is quite similar to setting an input. Append the following lines to the script:
value, units, error_code = mathcad_worksheet.get_real_output("output")
print(value)
Save the script, and then run it as before. You should get a return value that is correct for the function you are evaluating in Mathcad
Saving the Mathcad File
Todo