How to convert Python application to Exe and operate Excel with one click
overview
There was a request to edit Excel with one click, so I will leave it as a memorandum. PyInstaller is a handy tool that allows you to easily convert your Python scripts into executables. Also, OpenPyXL is a great library for reading and writing Excel files in Python. Here, I will explain the procedure to develop an application that bundles PyInstaller and OpenPyXL.
version check
python: v3.11.2
openpyxl: v3.1.1
pyinstaller: v5.8.0
Installing modules
First, install openpyxl. Execute the following command.
pip install openpyxl
Then install pyinstaller. Execute the following command.
pip install pyinstaller
whole source code
from openpyxl import load_workbook
# read excel file
wb = load_workbook('input.xlsx')
# get sheet
ws = wb.worksheets[0]
# get cell
cell = ws.cell(row=1, column=1)
# set the value in the cell
cell.value = 'Hello, world!'
# save excel file
wb.save('output.xlsx')
convert script
pyinstaller --onefile your_script.py
Here, replace your_script.py with the filename of the Python script you want to convert.
This command packs your_script.py into a single executable and outputs it to the dist directory.
Run executable
Finally, run the generated executable in /dist. Executables do not require a Python execution environment and can be easily run by others.
That’s all there is to putting a Python script into a single file and turning it into an executable. By using PyInstaller, you can save yourself the trouble of installing a Python environment when sharing your Python scripts with others. Please try.
Manageable project structure (bonus)
The above method works well for exe conversion, but it is inconvenient when sharing with other project members. So write npm scripts to be more concise.
- Install node.js to use the supplemental npm scripts
Create requipments for version control
//requipments.txt
pyinstaller >= 5.8.0
openpyxl >= 3.1.1
// package.json
{
"name": "pyinstaller_openpyxl",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"install": "pip install -r requirements.txt",
"start": "python main.py",
"build": "pyinstaller --onefile main.py"
},
"author": "",
"license": "ISC"
}
After creating the following files, install pyinstaller and openpyxl using npm scripts.
npm run install
Once the installation is complete, use npm scripts to create an Exe.
npm run build
Please see below for the project https://github.com/wiblok/python/tree/main/pyInstaller