Search code examples
pythonexcelnamed-ranges

Reading Excel data and named ranges into Python script


I am new to Python but have used Excel and named ranges for some time. I have a football workbook, several worksheets, with several named ranges for team name, colors, plays, rules, formations, etc. The workbook is used to create a wristband for football players. I have userforms and sheet change code that handles various activities. I would like to create this in python and be able to send out to other coaches in an exe file. Some named ranges are 1 cell others are multiple columns and rows.
For example a list of formations is 15 plus.

Each play has up to 20 columns of rules for each player and data for the wristband. The 'playbook' as a whole could be several hundred plays potentially. That is a table.
In python, I am able to open windows and create buttons, textblocks, etc. I am able to convert a py file into exe (Without a data file), just trying to get to the next step in my mind.

I am trying to better understand how to read in varying data into python, assign it to a variable, and then later write it back out to a data file tagged to the exe. What are the ways to achieve such effect?


Solution

  • Python is fine to use with Excel and maybe even easier to use than VBA in some instances. It probable that the most issue would be converting a python script to an exe if that indeed is what you want at the end.

    There are two methods of working with Excel in Python, apart from Python in Excel which is running Python in the Excel App.
    The aforementioned Openpyxl and Xlsxwriter; neither of these modules use Excel they modify the Excel file itself.
    The other two main modules are Xlwings and pywin32 aka win32com both of which interact with the Excel Application to modify an Excel file.

    Each has its advantages and disadvantages of course;
    Some of the obvious being;
    Openpyxl and Xlsxwriter do not need Excel to be installed and as such will run on Linux as well as Windows and MACOS.

    Openpyxl is probably the more commonly used module due to it not needing to run the Excel App each time you use it. But this module does have it disadvantages too and as it uses the OOXML interface which is not well documented and some functionality is not available or may be difficult to determine.
    But if it suits your needs there are lots of questions/answers on it's use you can use for reference.

    Xlsxwriter is a very well documented module with much functionality for building workbooks however...
    it should be noted that Xlsxwriter can only create new workbooks, so if you are editing/updating existing workbooks this module cannot help you.

    Xlwings will work on Windows and MACOS where Excel is installed though MACOS attributes may vary slightly from the more common Windows. But as Xlwings is sometimes termed VBA for Python it can pretty much do anything VBA can do, but you might find some areas lacking.

    Win32com is similar to Xlwings but for Windows only.

    This document python-tools-for-excel may also help with some details on modules and what they do.

    Your question may be closed since it is not a specific query about a programming problem so technically is off-topic for this site.

    You need to research what you need to do exactly and try out some code and if you have issues getting it working come back with questions.