Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Python-VBA generates a little sister in Excel


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

Video preview


Lead

Build little sisters in Excel using simple Python and VBA programs.

It feels very interesting, let's start happily

Related documents


Related documents

Follow WeChat's public number "Charles's Pikachu" to reply to 'Little Sister' get.

Develop tools


Develop tools

Python version: 3.6.4

Related modules:

PIL module;

openpyxl module;

and some Python's own modules.

Excel version:

Excel2016。

Environment construction


Environment construction

Install Python and add it to the environment variable, and pip installs the relevant modules that are required.

The implementation process


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:

 Python-VBA generates a little sister in Excel1  Python-VBA generates a little sister in Excel2

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:

 Python-VBA generates a little sister in Excel3

Copy the first piece of code from the VBA .txt file provided in the relevant file into the module:

 Python-VBA generates a little sister in Excel4  Python-VBA generates a little sister in Excel5

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":

 Python-VBA generates a little sister in Excel6  Python-VBA generates a little sister in Excel7

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:

 Python-VBA generates a little sister in Excel8

Step3:

Open the excel file in the ".xlsm" format and you will find that the end result is this:

 Python-VBA generates a little sister in Excel9

And the original picture looks like this:

 Python-VBA generates a little sister in Excel10

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):

 Python-VBA generates a little sister in Excel11

Finally, hide Sheet2 to avoid revealing:

 Python-VBA generates a little sister in Excel12

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


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