Python reading and writing Excel files-2 (openpyxl advanced)
Reading and writing Excel files with Python (advanced)
1. Introduction to openpyxl: the first choice for modern office automation
In the previous chapter we studied the old version.xlsThe method of reading and writing files, but now mainstream reports and statistical tables have already been fully shifted to Office 2007 and later versions.xlsxXML format. Among the Python tools that handle this format,openpyxlIt is a well-deserved first choice - it integrates reading and writing, has rich functions, and its syntax design is very suitable for Excel's native operating habits.
Compared to the old tool setxlrd / xlwtWhen reading and writing, you need to switch between different libraries and only support the old formats they are responsible for.openpyxlAll operations are unified, and the advantages are very obvious:
- Read, write and modify in one go: The same workbook object can not only read existing content, but also modify and append new data, without the need to convert back and forth between multiple libraries.
- Cell positioning is more intuitive: use it directly
'A1'、'C4'Such an Excel coordinate string saves you the trouble of memorizing row and column numbers. - Very comprehensive feature coverage: natively supports style customization such as fonts, borders, alignment, filling, etc.; can directly write truly calculable Excel formulas; and can also embed interactive native charts in files (the chart itself is an Excel object, not a static picture).
⚠️ format restrictions
openpyxlOnly supports.xlsxand.xlsmformat, cannot handle older versions.xlsdocument. If you still need to process.xls, please refer to the special tools introduced in the previous chapter.
Installation instructions
2. Reading Excel files: flexible coordinate positioning
openpyxlProvides a very flexible way to read files. In addition to the ordinary row and column indexes, the most popular thing is the native Excel coordinate string - when writing code, you no longer need to mentally calculate the numbers corresponding to the rows and rows, which is highly readable and saves subsequent maintenance.
Practical combat: reading stock historical data
Tips:
sheet.dimensionsIt can quickly tell you the rectangular range occupied by the data, for exampleA1:G255, which is very useful when debugging.
3. Writing and style customization: Make the report professional and beautiful
openpyxlVarious styles (fonts, alignment, borders, padding, etc.) are designed into independent "building blocks" that we can define separately and then assemble into cells as needed to achieve highly reusable style templates.
Reminder: The function name and punctuation marks in the formula must be in English format. Chinese punctuation will cause the formula to fail to be written.
4. Generate professional native Excel charts
This isopenpyxlOne of the most popular core functions in office and reporting scenarios - what it generates is not a static picture, but a truly interactive Excel chart object. After opening the file, you can click on the chart, edit the data source, change the color scheme and even switch chart types (for example, from a bar chart to a line chart, with just a few clicks).
If you need to make a line chart, just addBarChartReplace withLineChart, the rest of the usage is almost the same, very convenient.
5. Summary and pitfall avoidance suggestions
openpyxlis dealing with modern.xlsxThe library of choice for files, it's perfect for scenarios where you need fine control over styling, writing formulas, or generating charts. If you are just doing large-scale data cleaning and analysis, Pandas is usually more efficient, but Pandas saves.xlsxThe bottom layer often relies onopenpyxlengine.
Practical reminder to avoid pitfalls
- Column and column indexes start from 1: This is different from many programming habits. If the records are mixed, it may lead to obtaining wrong data.
- Remember to turn on the read-only/write-only mode for large amounts of data: Please use this when reading millions of rows of data.
load_workbook(…, read_only=True), used when writing large amounts of dataWorkbook(write_only=True), to significantly reduce memory usage. - Formulas must be punctuated in English: Even if the entire file content is in Chinese, the formula part must be in half-width English symbols, otherwise
openpyxlAn error will be reported. - Be careful with the row and column range when referencing chart data:
Referenceofmin_row、max_rowIt must correspond accurately to the data source, otherwise the chart may display abnormally.
Mastering these skills, you can use Python to efficiently generate Excel reports with clear structure, beautiful style and professional charts, truly realizing office automation.

