This post will focus exclusively on the native Python installation included with Abaqus 2023 and demonstrate and how to work with Excel documents.
Viable Solutions
When we look at the solution space for interacting with Excel, it's apparent that we have a variety of tools at our disposal. These tools can be categorized into two distinct variations:
1) Natively accessible functionality through COM objects and CSV formats, and,
2) Popular external libraries accessible through subprocesses or integrated into the Abaqus Python environment.
Solutions for each of these are outlined below:
- Native Abaqus Python functionality
- Use COM objects to drive Excel
- CSV functionality
2. Use popular libs like openpyxl or xlwriter
-
- Append these libraries into Abaqus Python
- Use an external master-script to drive Abaqus Python
- Call an external script to interact with Excel
When selecting a solution, you'll need to consider several factors, for example:
- Ease of use
- Does it require any extra installation or external libraries?
- Can it be run by anyone out of the box?
- Requirements of functionality
- Read and write data or create advanced dashboards with formulas and graphs?
- Is .csv acceptable, or do you need .xlsx?
- Willingness to use out-dated libraries
- Security risk
- Buggy libraries
- Lack of documentation
Let’s take a look at the natively available methods and provide simple examples for reading and writing.
1.A – win32com
We can use the win32com library to interact directly with Excel. Whilst it presents some fall-pits and does not offer the comfort of a ready-made library, this method is quite powerful. Even reading and writing is quite straight forward.
Functionality to create advanced excel dashboards with graphs and formulas are available. Useful resources from Microsoft:
Microsoft Learn | COM Fundamentals
Microsoft Learn | Excel Object Model Overview
The example code is available on github.
1.B – CSV Reader
Abaqus is shipped with the CSV reader – which is great for reading and writing tables with minimal code. This allows us to completely work around the proprietary .xlsx format – but this will also greatly restrict the functionality available.
This code writes and reads a simple table of material properties.
The example code is available on github.
External libraries such as Openpyxl
Openpyxl is a powerful library that allows you to create advanced Excel sheets. It handles a lot of the fall-pits associated with the win32com approach, hence dramatically increasing the “ease-of-use” with Python-based examples for much of the available functionality.
Accessing old versions of external libraries is not recommended. It imposes security risks that might be exploited; the library will have bugs and the documentation might have been documented.
If you disregard these issues and install such libraries, you'll create Abaqus scripts that are almost impossible to share and that will cause you some difficulties when you want to run it on your new laptop in 2–3 years’ time. Been there, done that!
Having a native-based Python script that uses a subprocess to drive “abaqus python -noGUI” type scripts is a pretty viable option. If you go for vanilla Python 3 and straight Abaqus, sharing and working with it should be quite straightforward. It’ll also allow you to use more advanced customized environments - if you’re into that sort of thing!
Overview of the Solutions
Final Thoughts
With Abaqus 2024 supporting python 3.10, we to hope to see some natively shipped libraries that will be easier to use, such as openpyxl. We’re eager to see what lies ahead!