Jun 01, 2021 Article blog
This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column
Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn
Build little sisters in Excel using simple Python and VBA programs.
It feels very interesting, let's start happily
Related documents
Follow WeChat's public number "Charles's Pikachu" to reply to 'Little Sister' get.
Develop tools
Python version: 3.6.4
Related modules:
PIL module;
openpyxl module;
and some Python's own modules.
Excel version:
Excel2016。
Environment construction
Install Python and add it to the environment variable, and pip installs the relevant modules that are required.
The implementation process
Step1:
Use Python to output the RGB information (randomly) of the pixels on the little sister's photo to Sheet2 of the Excel file.
Note that the photo should not be too large, otherwise it is not conducive to follow-up operations.
How to do it:
Run the Gen_Excel.py file in the cmd window (or double-click the Gen_Excel.exe file) and enter the path of your little sister's photo based on the prompts:
Step2:
Read the pixel value information in Sheet2 and populate it in Sheet1.
How to do it:
Open the Excel file generated in Step1 (the default file name is
"test.xlsx"),
press
"Alt-F11"
to open the VB editor and create a new module:
Copy the first piece of code from the
VBA .txt
file provided in the relevant file into the module:
At the same time, in order for the Excel file to open and automatically generate a picture of the little sister, set up a Workbook_open event to copy the second piece of code from the VBA .txt file into the "ThisWorkbook":
Finally, save the excel file in the
".xlsx"
format (default to "test .xlsx") as an excel file in the
".xlsm"
format to ensure that the macro works properly:
Step3:
Open the excel file in the ".xlsm" format and you will find that the end result is this:
And the original picture looks like this:
So we need to adjust the column width, row height, and page zoom levels of the cell where the picture is located to match the aspect ratio of the original image (online, so the quality is a little worse):
Finally, hide Sheet2 to avoid revealing:
OK, it's done!!!
Close the excel file in the ".xlsm" format and reopen it to get the effect in the video at the beginning of the article
more
Related files have packaged exe files, so the environment can be omitted a step
Pictures can be any small sister, little brother and some budding animals and so on
○