Create an add-in-enabled Excel instance with xlwings

It's too niche, but I'll show you the problems and solutions when creating an Excel instance with xlwings.

0. Environment

Check below.

1. Problems

To create an Excel instance with xlwings, use xw.apps.add () or xw.App (). However, this method has the following problems.

--For some reason, the add-in does not load. --After closing the instant, opening the workbook from Explorer does not load the add-in.

If you open only Excel without opening the workbook (start it from the start menu), the add-in will load normally.

2. Solution

Create an Excel instance from the shell and pass it to xlwings without using xw.apps.add () or xw.App ().

import re
import subprocess
import time
from pathlib import Path

import xlwings as xw

def get_xl_path() -> Path:
    #Function that returns the Excel installation path
    subprocess_rtn = (
        subprocess
        .run(['assoc','.xlsx'], shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
        .stdout.decode("utf8")
    )
    assoc_to = re.search('Excel.Sheet.[0-9]+', subprocess_rtn).group()

    subprocess_rtn = (
        subprocess
        .run(['ftype', assoc_to], shell=True, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
        .stdout.decode('utf-8')
    )
    xl_path = re.search('C:.*EXCEL.EXE', subprocess_rtn).group()

    return Path(xl_path)


def xw_apps_add_fixed() -> xw.App:
    #Function to create an Excel instance
    xl_path = get_xl_path()
    num = xw.apps.count
    pid = subprocess.Popen([str(xl_path),'/e']).pid

    #Wait until it can be recognized by xlwings
    while xw.apps.count == num:
        time.sleep(1)

    #Wait until available from xlwings
    while True:
        try:
            xw.apps[pid].activate()
            break
        except:
            time.sleep(1)

    return xw.apps[pid]


app = xw_apps_add_fixed()

reference

--Change file extension association at Windows command prompt

Recommended Posts

Create an add-in-enabled Excel instance with xlwings
Create an Excel file with Python3
Quickly create an excel file with Python #python
Create an environment with virtualenv
Create an API with Django
Create an age group with pandas
Create an application by classifying with Pygame
Create an image processing viewer with PySimpleGUI
Create an update screen with Django Updateview
Create an English word app with python
Create an upgradeable msi file with cx_Freeze
Create an app that guesses students with python
Create an academic society program with combinatorial optimization
Create an image composition app with Flask + Pillow
Create an image with characters in python (Japanese)
Create an AWS GPU instance to train StyleNet
Send an email with Excel attached in Python
Create an API server quickly with Python + Falcon
Excel with Python
Create an animated GIF local server with Python + Flask
Run Python from Excel VBA with xlwings & tutorial supplement
code-server online environment (3) Launch an EC2 instance with Boto3
Create an idol-like tweet with Keras LSTM (sentence generation)
An easy way to create an import module with jupyter
Create an app that guesses students with python-GUI version
Create a random number with an arbitrary probability density
Create an OpenAI Gym environment with bash on Windows 10
Type after reading an excel file with pandas read_excel
Create an environment for test automation with AirtestIDE (Tips)
Stop an instance with a specific tag in Boto3
Handle Excel with python
Create games with Pygame
Read excel with openpyxl
Operate Excel with Python (1)
Operate Excel with Python (2)
Create an environment for "Deep Learning from scratch" with Docker
Minimum Makefile and buildout.cfg to create an environment with buildout
Create an example game-like stage with just the Blender 2.80 script
Let's create an app that authenticates with OIDC with Azure AD
Introduction to Python for VBA users-Calling Python from Excel with xlwings-
I'm trying to create an authentication / authorization process with Django
Create a Todo app with Django ① Build an environment with Docker
[Python Kivy] How to create an exe file with pyinstaller
How to read an Excel file (.xlsx) with Pandas [Python]
I tried to create an article in Wiki.js with SQLAlchemy