This Docker container provides a solution to run Excel macros using Python win32 on a Wine and Office environment. It supports installation via GitHub and Docker Hub. Below are the steps for installation and usage.
-
Clone the repository:
git clone https://github.com/xeden3/docker-excel-macro-run.git cd docker-excel-macro-run
-
Build the Docker image:
docker build -t docker-excel-macro-run:v1 .
Pull the Docker image directly from Docker Hub:
docker pull xeden3/docker-excel-macro-run:v1
Run the Docker container with the following command:
docker run -v ./example.xlsm:/opt/wineprefix/drive_c/test.xlsm --rm docker-excel-macro-run:v1 test.xlsm ThisWorkbook.WriteDataToSheet1
The parameters explained:
docker-excel-macro-run:v1
: Docker image name and tag.test.xlsm
: Name of the Excel file to run the macro on. It should match the filename in the container's directory (/opt/wineprefix/drive_c/test.xlsm
).ThisWorkbook.WriteDataToSheet1
: Macro command to execute.
The output will be in JSON format:
{"errcode": 0, "errmsg": ""}
You can retrieve and print the output using:
output=$(docker run -v ./example.xlsm:/opt/wineprefix/drive_c/test.xlsm --rm docker-excel-macro-run:v1 test.xlsm ThisWorkbook.WriteDataToSheet1)
echo $output
This Docker container addresses two main challenges:
-
Handling Errors with Chinese Programs: Chinese characters are not recognized in the default Wine environment. Therefore, when macros contain Chinese or other languages, errors may occur. Installing the appropriate font library and changing the
locales
value can resolve this issue.To address this in Docker, the following steps are taken:
RUN apt-get update && apt-get install -y locales RUN sed -i -e 's/# zh_CN.UTF-8 UTF-8/zh_CN.UTF-8 UTF-8/' /etc/locale.gen && \ dpkg-reconfigure --frontend=noninteractive locales && \ update-locale LANG=zh_CN.UTF-8 ENV LC_ALL=zh_CN.UTF-8
This Docker container also addresses another significant challenge:
-
Suppressing xvfb-run Output: During program execution,
xvfb-run
may output the following warning:X connection to :100 broken (explicit kill or server shutdown).
To mitigate this, an
entrypoint.sh
file is added. This shell script filters out the warning message using thegrep
command. Failure to suppress this warning may result in numerous exception messages being returned.Below is the code snippet used to suppress the warning:
# Disable the 'X connection to :100 broken (explicit kill or server shutdown).' warning xvfb-run -a wine python /opt/wineprefix/drive_c/app/excel_xlsm_macro_run.py "$@" | grep -v '100 broken'
This command ensures that the warning message is filtered out from the output, providing a cleaner execution result.