Plug in wings and let Excel fly--xlwings (Thu)

Source: Internet
Author: User

Objective

That year to see "Don't be afraid, Excel VBA is actually very simple" brief Encounter, read the first version of the electronic version, bought a paper version, and then send it to people. Then, found the second edition, Buy the collection. Later, the discovery of the Python programming language, is simply the inverse of the day, amateur non-programmer's gospel. Life is short, I use Python. A short few lines of code, to achieve a lot of their own dare not to think of features.
Self-learning VBA discovery for VBA and Python, I have a few ideas: (Just for automated office)

    • Non-professional cousin, just ordinary office, I hope occasionally encounter point duplication of work can be automated. VBA for me, to remember things are too many, grammatical not concise. Each time you write a function, the code is too long.
    • Although VBA has interfaces in many programs, the scope of the application is still slightly narrower, but there are too few scenarios for the average user to learn in depth. Have a task-driven, have the power to improve the level.
    • Python is definitely not running fast, but it's definitely faster than running VBA in Excel.
    • The Python language is concise (Python Dafa is good), the module is rich, and there are many great God-made wheels waiting for us to use.
    • Python has a wide range of applications that can help me solve occasional tabular problems and connect with a variety of other software or platforms.
Use Python custom macros (Windows only)
    1. Preparatory work
    2. Installing the Xlwings module
    • Download template file, Baidu Web link: xlwings custom macros and function template password: 2dut

    • Where the custom template can modify the name, MyMacro the python file as a custom macro, and myfun the Python file as a custom function.
    • Set up the Excel Trust Center to Trust access to the VBA project object model

Custom macros

    1. Open mymacro0 with Notepad and enter the following statement:
import xlwings as xw@xw.subdef my_macro(): wb = xw.Book.caller() wb.sheets[0].range(‘A1‘).value = wb.name

A macro with a macro name of My_macro is written, and the name of the workbook is entered into cell A1. A custom macro requires only the first two sentences to be the same, and the content is written by itself, that is, the custom macro needs to start with the following statement

import xlwings as xw@xw.sub

Run as: First Alt+f8 run Importpythonudfs built-in macros, import our custom macros and functions, import, and then run the MY_MACRO macro I defined.

    1. Custom functions
      The preparation is consistent, open myfun0, and write the first custom function:
import xlwings as xw@xw.funcdef myfun(): wb = xw.Book.caller() return wb.name

Note that the start statement is @xw.func instead of @xw.sub, and then the function is relative macro, with a return value, so the return statement is used. After customizing the function, you can just use the same function as Excel itself. If you enter "=myfun ()" In cell A1, you will get the name of the workbook.

End

This paper presents a template for customizing macros and functions in the Xlwings module, and then gives a brief description of how to define macros and functions. Attention:

    • A python file, can only define a macro or function, so the template has been given by default three macros and function files, easy to use directly.
    • The name and relative file location of the Python file cannot be changed, the file location of the Excel file cannot be changed, and the name can be changed. For modifications, please refer to the official Xlwings documentation.

Plug in wings and let Excel fly--xlwings (Thu)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.