gist
In our daily work, we will often and frequently use the excel table, so can we manipulate the excel table through python, so that its automation to replace our work? For example, it involves centering, merging cells, modifying font colors and so on.
matrix
A linux server or a windows 10 computer.
Python version 3.7.1, of all can be
mounting module
The module you need to operate excel is "xlwt", we need to use the pip command in python to install this module;
PS: centos default python is version, it is recommended to upgrade to the version, otherwise the subsequent process can not be carried out; you can refer to this article:https:///article/You can view the exact installation process;
[yunweijia@localhost jier]$ sudo pip3 install xlwt -i /simple # Download the xlwt module using domestic sources [yunweijia@localhost jier]$ sudo pip3 list # View download results Package Version ---------- ------- pip 21.3.1 setuptools 39.0.1 xlwt 1.3.0 [yunweijia@localhost jier]$
New excel
Code:
#coding:utf-8 #!/usr/local/python3/bin/python3 # Imported modules import xlwt # Create a new excel workbook = (encoding='utf-8') # Create a new sheet with the name "Operator", you can create more than one sheet. worksheet = workbook.add_sheet('Operator') # Save the excel with the name "yunweijia" # (str(""))
Running:
[yunweijia@localhost jier]$ python3 [yunweijia@localhost jier]$ ls [yunweijia@localhost jier]$
Results:
Cell Write Data
Code:
#coding:utf-8 #!/usr/local/python3/bin/python3 # Imported modules import xlwt # Create a new excel workbook = (encoding='utf-8') # Create a new sheet worksheet = workbook.add_sheet('Operations and Maintenance Home') # Write data in cells (0, 0, "Hello.") (0, 1, "I'm good.") (0, 2, "Hello, everyone.") (1, 1, "Hey, hey, hey.") # Save excel (str(""))
Running:
[yunweijia@localhost jier]$ python3 [yunweijia@localhost jier]$ ls [yunweijia@localhost jier]$
Results:
Merge Cells
Code:
#coding:utf-8 #!/usr/local/python3/bin/python3 # Imported modules import xlwt # Create a new excel workbook = (encoding='utf-8') # Create a new sheet worksheet = workbook.add_sheet('Operator') # Write data in cells (0, 0, "Hello.") (0, 1, "I'm good.") (0, 2, "Hello, everyone.") # Merge cells (first cell column, last cell column, first cell row, last cell row) worksheet.write_merge(1, 1, 0, 2, "Hey, hey, hey.") # Save excel (str(""))
Running:
[yunweijia@localhost jier]$ python3 [yunweijia@localhost jier]$ ls [yunweijia@localhost jier]$
Results:
center
PS: You can create multiple styles as you see fit, as long as the names are different;
Code:
#coding:utf-8 #!/usr/local/python3/bin/python3 # Imported modules import xlwt # Create a new excel workbook = (encoding='utf-8') # Create a new sheet worksheet = workbook.add_sheet('Operator') # Create a style head_style = () # Create styles # Set cell alignment ahead_lignment = () # Alignment initialization ahead_lignment.horz = .HORZ_CENTER # Center horizontally ahead_lignment.vert = .VERT_CENTER # Vertical centering head_style.alignment = ahead_lignment # Style overloading # Write data in cells (0, 0, "Hello.") (0, 1, "I'm good.") (0, 2, "Hello, everyone.") # Merge cells (first cell column, last cell column, first cell row, last cell row) worksheet.write_merge(1, 2, 0, 2, "Hey, hey, hey.", head_style) # Save excel (str(""))
Running:
[yunweijia@localhost jier]$ python3 [yunweijia@localhost jier]$ ls [yunweijia@localhost jier]$
Results:
Modify fonts and colors
Code:
#coding:utf-8 #!/usr/local/python3/bin/python3 # Imported modules import xlwt # Create a new excel workbook = (encoding='utf-8') # Create a new sheet worksheet = workbook.add_sheet('Operations and Maintenance Home') # Create a style head_style = () # Create styles # Create fonts head_font = () # Font initialization head_font.name = 'italicized' # Fonts head_font.height = 300 # Size head_font.colour_index = 2 # Red head_style.font = head_font # Style overloading # Set cell alignment ahead_lignment = () # Alignment initialization ahead_lignment.horz = .HORZ_CENTER # Center horizontally ahead_lignment.vert = .VERT_CENTER # Vertical centering head_style.alignment = ahead_lignment # Style overloading # Write data in cells (0, 0, "Hello.") (0, 1, "I'm good.") (0, 2, "Hello, everyone.") # Merge cells (first cell column, last cell column, first cell row, last cell row) worksheet.write_merge(1, 2, 0, 2, "Hey, hey, hey.", head_style) # Save excel (str(""))
Running:
[yunweijia@localhost jier]$ python3 [yunweijia@localhost jier]$ ls [yunweijia@localhost jier]$
Results:
summarize
To this article on the use of Python excel table is introduced to this article, more related Python excel table content please search my previous posts or continue to browse the following related articles I hope you will support me in the future!