Before officially starting today's article, I first want to say one thing, I remember that a senior programmer in Zhihu said such a thing, "As far as python is concerned, when you write **, you will definitely encounter a third-party library that you are not familiar with, but a qualified programmer can check the official introduction article of this unfamiliar third-party library by himself, and then find the function he wants, and finally combine his specific needs to reproduce the ** of the introduction article", I agree with this statement very much.
Back to the point, today I encountered such a requirement in a python learning group that I joined, please see the screenshot below.
Seeing the above two screenshots, there may still be people who don't understand the specific needs of the questioner for the time being, let me explain first, people who often operate excel** know that when you fill a cell in excel** with a large amount of text, if there is no line break, then the content of your cell at this time is likely to be what the screenshot below looks like.
At this time, if we click on the word wrap function in excel, the whole cell will look much better when displayed, as shown in the following figure:
Therefore, when we fill in the content in excel**, if there is a lot of content, we still have to consider the reader's feelings, and the function of automatic line wrapping still needs to be used.
Let's go back to the topic, there are also many people in the group who have ideas, and some even say that they can use VBA and Python together to meet their needs.
At that time, I had been watching, and seeing the excitement of the group chat, it also aroused my interest, and I felt that I would also join the research queue, so I carefully asked the questioner about the needs and decided to take the challenge.
I will first summarize the specific requirements, all the content is put into a merged cell, the content should be displayed in separate lines according to Party A's meaning, and the content in the text should be displayed in red.
As always,The first thing I did was to ask Du Niang again,As expected,CSDN this old rascal appeared again,It's the same as his previous style,I still want you to open a member to tell you the specific approach.,But I've dealt with this guy a lot.,I understand that maybe python's openpyxl library should be able to perfectly implement this demand.,Just do it.,So I went to the official introduction of the openpyxl library**Read it in detail., Sure enough, I found a solution.
The openpyxl library proposes the concept of "rich text" (literal translation), which in the vernacular allows you to modify the formatting of some specified text in a cell (such as font, color, font size, etc.), which is a perfect solution to some of our needs (the content specified in the text mentioned above should be displayed in red).
Back to our other needs, "the content should be displayed in branches according to Party A's meaning", in fact, Party A (that is, the questioner in the group at the beginning) has already written the branch character in the position of the branch in the filled text, but his final effect is that you also need to manually click on the cell filled with the content in the cell of excel**, and then the excel** on the computer screen will display the content in the branch, so it is still not perfect.
In fact, the reason why this problem occurs (you need to manually click on the cell page to show the effect of the branches) is that you haven't set the text alignment of this cell.
The author of openpyxl, a third-party library, also thought of this thing, so he also provided the alignment method to help us set the text alignment of cells (interested friends can go to openpyxl's official ** to take a look).
At this point, we have cleared up the solution ideas, and the rest is to hit the keyboard, and the following is the complete **screenshot.
The above **,Friends with a foundation probably have already understood.,It's estimated that it's the rich text part of the rich string line, I'll explain it again.。
The official ** of the openpyxl library is an example of this rich text operation.
In fact, it is to put the text you need to format separately into the textblock method (you can also guess from the literal meaning, that is, it provides an area for the text that needs to be set separately, and isolates it from other texts that do not need to be formatted separately), and the inlinefont behind it is to let you fill in the various text formats you need to set, and the openpyxl library has also made a detailed introduction, what we have to do is to compare the introduction, Just write the text formatting you need to set.
I just want to remind you that the argument b in the inlinefont method (the default is true) is to tell the computer whether to format the text you specify separately according to your requirements, and there will be no change if it is set to false, and I only color part of the text this time, so only the color is set, and the color should be entered into the hexadecimal number of rgb instead of the English word of the color directly.
The operation of merging cells should be done at the end, at this time, excel follows the upper left rule, and automatically treats the content of the cells in the upper left corner as the content after merging cells.
* Running screenshots (privacy involved, so it is coded).
At this point, this need has been solved, thank you for seeing now, if you are interested in python, or have other opinions on my **, welcome to chat privately or express your opinions, my writing is not good, if the article has deficiencies, please forgive me, and don't chat privately about what I send "111 to obtain information" content, I am not a training institution, nor csdn
ps: Friends who are interested in python, you can search for *** early python", and then let the group owner pull you**, I am also in this group, welcome everyone to discuss python technology.