简体   繁体   中英

How do Modules like Openpyxl create/read Excel files

tl;dr
How to interact with Excel using Python, but without using pre defined modules.

I am working with Openpyxl Module in Python to read and write data from and to Excel. There are some methods which are not available in Openpyxl but are available in Xlsxwriter like folding of rows (folding of columns is possible in Openpyxl). So I thought lets look at the source code and try to implement it myself.

But the first question I had was how do I interact with Excel using Python? How do these Modules read data from and write data to excel.

At a high level an xlsx file is a collection of XML files in a zip container. So xlsxwriter/openpyxl write XML files and then use the Python standard libary zip module to collect them into an xlsx file.

Most Excel xlsx features are defined as XML elements or as a collection of elements. Options are defined as elements or element attributes.

However, and this is a big however, there are interactions between elements within a file and with other files that form part of the collection. So a lot of the work in xlsxwriter/openpyxl goes into ensuring that the interactions are correct and that they don't break other features. At least that is my experience and it is also why xlsxwriter has a large test suite with tests against full Excel generated files.

So in order to extend one of these modules you will need to figure out the XML elements or attributes to be added and the interactions with other elements/files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM