Working with Cell Styles Using Python

Python 3: Automating Your Job Tasks Superhero Level: Automate Excel Tasks with Python 3
9 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€93.07
List Price:  €130.30
You save:  €37.23
£79.62
List Price:  £111.47
You save:  £31.85
CA$136.60
List Price:  CA$191.24
You save:  CA$54.64
A$151.87
List Price:  A$212.63
You save:  A$60.75
S$135.20
List Price:  S$189.29
You save:  S$54.08
HK$781.33
List Price:  HK$1,093.90
You save:  HK$312.56
CHF 90.72
List Price:  CHF 127.01
You save:  CHF 36.29
NOK kr1,092.46
List Price:  NOK kr1,529.50
You save:  NOK kr437.03
DKK kr694.20
List Price:  DKK kr971.90
You save:  DKK kr277.70
NZ$167.15
List Price:  NZ$234.02
You save:  NZ$66.86
د.إ367.25
List Price:  د.إ514.16
You save:  د.إ146.91
৳10,946.16
List Price:  ৳15,325.07
You save:  ৳4,378.90
₹8,342.71
List Price:  ₹11,680.13
You save:  ₹3,337.42
RM473.95
List Price:  RM663.55
You save:  RM189.60
₦123,487.65
List Price:  ₦172,887.65
You save:  ₦49,400
₨27,847.21
List Price:  ₨38,987.21
You save:  ₨11,140
฿3,679.53
List Price:  ฿5,151.49
You save:  ฿1,471.96
₺3,232.18
List Price:  ₺4,525.18
You save:  ₺1,293
B$511.10
List Price:  B$715.57
You save:  B$204.46
R1,851.52
List Price:  R2,592.21
You save:  R740.68
Лв181.98
List Price:  Лв254.78
You save:  Лв72.80
₩135,881.87
List Price:  ₩190,240.05
You save:  ₩54,358.18
₪371.60
List Price:  ₪520.25
You save:  ₪148.65
₱5,713.97
List Price:  ₱7,999.79
You save:  ₱2,285.82
¥15,308.41
List Price:  ¥21,432.39
You save:  ¥6,123.98
MX$1,693.57
List Price:  MX$2,371.07
You save:  MX$677.49
QR364.01
List Price:  QR509.63
You save:  QR145.62
P1,365.33
List Price:  P1,911.52
You save:  P546.18
KSh13,468.65
List Price:  KSh18,856.65
You save:  KSh5,388
E£4,794.02
List Price:  E£6,711.82
You save:  E£1,917.80
ብር5,699.43
List Price:  ብር7,979.43
You save:  ብር2,280
Kz83,741.62
List Price:  Kz117,241.62
You save:  Kz33,500
CLP$94,755.52
List Price:  CLP$132,661.52
You save:  CLP$37,906
CN¥723.62
List Price:  CN¥1,013.10
You save:  CN¥289.48
RD$5,785.38
List Price:  RD$8,099.76
You save:  RD$2,314.38
DA13,457.99
List Price:  DA18,841.73
You save:  DA5,383.73
FJ$225.30
List Price:  FJ$315.43
You save:  FJ$90.13
Q775.21
List Price:  Q1,085.33
You save:  Q310.11
GY$20,876.73
List Price:  GY$29,228.26
You save:  GY$8,351.52
ISK kr13,989.60
List Price:  ISK kr19,586
You save:  ISK kr5,596.40
DH1,008.69
List Price:  DH1,412.20
You save:  DH403.51
L1,771.31
List Price:  L2,479.90
You save:  L708.59
ден5,730.13
List Price:  ден8,022.42
You save:  ден2,292.28
MOP$803.11
List Price:  MOP$1,124.38
You save:  MOP$321.27
N$1,860.85
List Price:  N$2,605.27
You save:  N$744.41
C$3,673.63
List Price:  C$5,143.23
You save:  C$1,469.60
रु13,311.40
List Price:  रु18,636.50
You save:  रु5,325.09
S/375.46
List Price:  S/525.66
You save:  S/150.20
K383.63
List Price:  K537.10
You save:  K153.47
SAR375.03
List Price:  SAR525.05
You save:  SAR150.02
ZK2,685.42
List Price:  ZK3,759.69
You save:  ZK1,074.27
L463.14
List Price:  L648.42
You save:  L185.27
Kč2,328.17
List Price:  Kč3,259.54
You save:  Kč931.36
Ft36,248.95
List Price:  Ft50,749.98
You save:  Ft14,501.03
SEK kr1,083.98
List Price:  SEK kr1,517.62
You save:  SEK kr433.63
ARS$87,816.53
List Price:  ARS$122,946.66
You save:  ARS$35,130.12
Bs689.12
List Price:  Bs964.79
You save:  Bs275.67
COP$389,940.87
List Price:  COP$545,932.82
You save:  COP$155,991.94
₡50,893.45
List Price:  ₡71,252.86
You save:  ₡20,359.41
L2,475.75
List Price:  L3,466.15
You save:  L990.40
₲747,391.81
List Price:  ₲1,046,378.43
You save:  ₲298,986.62
$U3,833.15
List Price:  $U5,366.56
You save:  $U1,533.41
zł402.67
List Price:  zł563.75
You save:  zł161.08
Already have an account? Log In

Transcript

Hi, and welcome to this lecture. Inside this lecture we are going to talk about applying different styles to the cells inside our sheet. In order to do that, first of all, I'm going to select a specific cell inside this sheet that we are going to work on from within the Python interpreter. So let's say that we choose cell B eight, which is Richard right here. What we want to do is to use the open pi Excel module and change the font color of the cell. Also, we want to make the text bold and italic as well.

We are also going to insert a background color for this cell. On top of that, we are going to modify the border for the cell and we're going to add a different border from the default that you're seeing right now on the screen. And we're also going to edit the alignment of the text inside the cell from center which is the setting we're seeing right now. All the texts in the cells inside this ship centered to, for example, let's say Left Alignment. So the text inside this cell B eight will be aligned to the left. And we are going to do all that using Python and the open pi Excel module.

Okay, so remember that we are going to use the cell B eight in order to apply all the changes all the style changes I have already mentioned. And now I'm going to close the file. And I'm going into the Python interpreter. First of all, I'm going to import open bi Excel. And we're going to have a look at open pi Excel that styles. So you can see that here we have multiple classes and attributes that we can use in order to change and edit the style within our sheets within our cells.

We are going to use some of these classes and attributes inside this lecture. For example, we are going to use alignment, border color, also font and pattern fill. For example, To perform the desired style changes on our cell, okay, now since we are going to access each of these classes in the code that follows, and because we want to save some space into our code, and not be required to always specify open pi Excel dot styles dot, and now the name of the class, let's say border. In order to avoid this into our code, I'm just going to use from open Vy XL dot styles, import all the names within this namespace. Okay? And now we can just use the name of the class for example, border whenever we want to access and use it.

Okay, one more thing before moving on to the code. I have attached a link to this lecture to the official documentation of open pi Excel regarding styles. And you should check that out in order to learn how to use each of these classes. Each of these attributes regarding style editing in Excel, okay? Now, before anything else, we should load our workbook. And in order to do that, I'm going to use the same line of code as we did earlier in this section, including the full path to the file.

The next thing we should do is to reference our sheet, and we're going to consider the fourth sheet inside the workbook, which is employee data. So let me reference this sheet. Okay, and finally, we should reference we should select the cell within the sheet that we want to work on. In our case, as I said, that would be B eight, so cell equals sheet of B eight. And at this point, we are ready to start applying the classes from within open pi Excel dot styles. And first of all, we are going to change the color of the font inside this cell.

And also we are going to make the text bold and italic at the same time. In order to do that. I'm going to create this variable let's call it font equals the name of the class inside open pi Excel dot style. That would be font. So font, and in between parentheses we are going to insert several arguments. For example, color equals colors, dot red.

And this is the way in which to set the color of the font. Comma, we said we want the text to be bold, so bold equals true, and also italic equals true. Okay, let's hit Enter. And now we should apply these changes to our cell. So we have cell dot font, where font is a predefined attribute of a cell inside the open pi Excel module. So cell dot font equals our variable right here, font.

Okay, so we just set the font color and bold and italic for the text within the cell. The next thing we should do is to also add a pattern fill a background color for our cell. And in order to do that, I'm going to create a new variable called field. equals the name of the class from within the open pi Excel module. And that would be pattern fill right here. So pattern fill, and in between its parentheses, we have fill underscore type equals solid.

So we want a solid color in the background, comma, and now BG color equals, and now we will input a color code for a random color I already picked. So this is it. Okay, Enter. And now in order to apply this pattern to our cell, we should simply use cell dot fill, which is another attribute of a cell equals fill. Okay, next on our list is the border. For the border of a cell, we can change both the border style, which refers to how thick the border is, if it's a double border, or a simple border and so on, and also the border color by also specifying color code as we did before for the pattern fill, okay, in order to do that, we are going to use the border class from within open pi Excel dot styles.

So let me show you how to do that. Since this is a longer command, I will copy and paste it right into the interpreter. So we have border, this variable right here equals border the class. And now left equals So the left border border style double. So we are going to change the border style to a double border and color this color right here, which is referenced by this code. Next we have the right border, also a double border and with the same color, and finally we are going to apply the same settings on the top border right here, and also for the bottom border of this cell.

Okay, let's hit Enter. And now let's apply these border settings to our cell. So cell dot border, we are using the border attribute equals border, the variable that we just defined. Okay? Finally, we also said that we want to set the alignment of the text inside the cell. So for that, I'm going to create another variable, align equals.

And now we are going to use alignment from up here. So alignment, and I'm going to specify horizontal equals left instead of the Center Alignment that we currently have inside the sheet. Okay, of course, we should apply this setting as well to the cell. So cell dot alignment, equals align. Okay, great. Now, the last thing we should do before checking the result is, of course, saving the workbook by using workbook dot save and the full path to the file, Enter.

And now before opening up the file and checking the results, let's do a short recap and see what changes have we applied to ourselves. So first of all, we should have a different font color which is red, the font inside the cells should be bold. italic as well, the background pattern for this cell should be solid. And also the color should be changed as well. We should also have a double border for this cell with a different color. And also the alignment of the text inside the cell should be left.

Okay, now let's open up the file. So font color red checked, text is bold checked. Text is italic checked. Pattern fill is solid checked, background color has changed checked, the border style is double. Let's zoom in and check that as well. I don't know if you can see this, but the thickness of this border is greater than for the rest of the cells and also the color of the border has changed.

And finally, the alignment of the text inside this cell is to the left which is also correct. Okay. Again, you can check out the link attached to this lecture in order to learn more about Cell Styles using open pi Excel. And I will see you in the next lecture.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.