Send Close Add comments: (status displays here)
Got it!  This site "robinsnyder.com" uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.  Note: This appears on each machine/browser from which this site is accessed.
Python: OpenXML xlsx files
by RS  admin@robinsnyder.com : 1024 x 640


1. Python: OpenXML xlsx files
Python can be used to both create and read xlsx files in the OpenXML data format. The Python openpyxl package will be used.

One way to install the openpyxl package is as follows.
D:\Python38\Scripts\pip3.exe install openpyxl

Use the path to your installed version of Python. See below to install a specific version.

2. Package issue
In the case of openpyxl, as of 2020-04-24, there is an issue in saving xlsx files using version 3.0.2. The solution, until it is fixed (and this works for other packages with issues) is to downgrade the installation. Here is one (safe) way.
D:\Python38\Scripts\pip3.exe uninstall openpyxl D:\Python38\Scripts\pip3.exe install openpyxl==3.0.1


3. Another way
Another way is as follows. An upgrade or downgrade uses the same form of the command.
D:\Python38\Scripts\pip3.exe install --upgrade openpyxl==3.0.1


4. Charts
In many cases, one does not need to read nor create charts in Excel (for data science acquisition or data distribution). If needed, one can check the documentation for the large number of ways in which Excel charts can be created. In most cases, one will need to extract data from existing workbooks or generate workbooks.

I have found that generating workbook sheets in many different forms is useful for whoever it is that is getting the data. They can then decide which best suites them.

In the example below, the same data is provided in table order and in transposed table order.

5. Getting started
Besides this page, more information on getting started is at the openpyxl web site at https://openpyxl.readthedocs.io/en/stable/.

6. Workbook
Excel is based on slides of a workbook containing a collection of sheets, charts, etc.

It is often best to start with a blank sheet and add cells.

Each cell has a content, a type, a format, etc.

7. Quirks
For historical reasons, Excel has many specific ways to do things that may not be obvious.

There are some things that are difficult or impossible to do with Excel. There are also certain specific things that cannot be done using openpyxl

For such things I use VBA (Visual Basic for Applications) code in Excel to do actions, fix-ups, etc.

8. Example table
Here is the table used for example purposes.


9. Parameters
An example xlsx file will be created, read, and accessed.


10. Excel module and class
As with many packages, it is often useful to create a custom class with which to access that package.

11. Imports



12. Initialization
The initialization can be done as follows and which creates a new in-memory workbook.

Sheets are numbered starting at 1.

13. Make a new sheet
The make method makes a new sheet in the current workbook, adding it to the end of the existing sheets.

A workbook must have at least one sheet so a new sheet is not created if "making" the first sheet, just the title changed.

14. Load a workbook
The load method loads a new in-memory workbook.

The instance variable excel1 is the workbook.

15. Save the workbook
The save method saves the in-memory workbook.


16. Add a cell
The add method adds a cell at the specified row and column and with the specified value.

Cell rows start at 1. Cell columns start at A. Row and column positions start at 1.

17. Cell references
The ref method returns the user-friendly cell reference for the provided row and column.


18. Module rsExcel
Here is a code module in Lua [module rsExcel in rsExcel.py].


19. Complete program
The complete program that uses the rsExcel module and excel class is now covered.

The program creates an xlsx file with two sheets using the data table and the data table transposed.

The program then reads in the created xlsx file and goes through and outputs the (used) cells in each sheet. Note that Excel keeps track of which cells and ranges are used, so this is built into openpyxl.

20. Create the sheet
The sheets can be created as follows.

Since the creation of the sheets are similar, the procedure sheetAdd1 is used to create the sheet using the global variable excel1. Note that a global variable can be created at any place in the program and does not need a static scope instance (i.e., somewhere above the code using excel1).

21. Colors
To show cells shaded by colors, the following color lists are defined.

This list has a light background and a dark foreground/text color. The foreground/text color is not used here. Note: Excel, like Word and PowerPoint, and for historical reasons, sometimes support only limited colors or have "tricks" that are needed to get a desired color.

The above "trick" is that a gradient fill is needed to get the desired color as the original Excel (like Word) only permitted a small number of indexed colors.

22. Add the sheet



23. Read the created sheets


Here is the Python code [#15]


24. Output
Here is the output of the Python code.


25. Generated sheets
Here is one sheet called "Table". Generated tableHere is the other sheet called "TableT" (for transposed). Generated table transposed

26. Sheet types
The above assumed all sheets were worksheets. The type of a sheet can be used to determine the type of each sheet in a workbook.
type(sheet1)


27. Alignment
For cell text alignment, the following can be useful. Alignments can be one of "left", "center", "right".
from openpyxl.styles import Alignment # ... cell1.alignment = Alignment(horizontal="center",vertical="center")


28. Formulas
Sometimes one wants to create a worksheet that can be easily changed by the intended user.

In such cases, formulas can be assigned to cells such that Excel calculates the values of those cells from other cells.

29. Locked files
Note that Excel (and other programs) will lock the file currently loaded into the application.

If you try to generate a xlsx file while Excel has it locked, a run-time error results.

30. CSV as text
Note that csv files are stored and read as text. When it is important that certain data be numeric fields, explicit conversion is necessary.

Whenever converting csv to xlsx, or when creating and/or working with xlsx files, it may be important to do some explicit conversions of cells rather than using the default "General" format.

31. End of page

by RS  admin@robinsnyder.com : 1024 x 640