In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use Python to operate the excel table, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let Xiaobian take you to understand.
Environment
One linux server, or one windows10 computer
Python3.7.1 version, python3.x version is fine.
Installation module
The module needed to operate excel is "xlwt". We need to use the pip command in python to install this module.
The default python of PS:centos is version 2.x. It is recommended to upgrade to the python3.x version, otherwise the subsequent process cannot be carried out.
[yunweijia@localhost jier] $sudo pip3 install xlwt-I https://pypi.tuna.tsinghua.edu.cn/simple # download xlwt module using domestic source [yunweijia@localhost jier] $sudo pip3 list # View download result Package Version--pip 21.3.1setuptools 39.0.1xlwt 1.3.0 [yunweijia@localhost jier] $New excel
Code:
# create a new excelworkbook = xlwt.Workbook (encoding='utf-8') # create a new sheet, named "operation and maintenance family", and can create multiple sheetworksheet = workbook.add_sheet ("operation and maintenance home") # save excel, named "yunweijia" workbook.save ("operation")
Run:
[yunweijia@localhost jier] $python3 ceshi.py [yunweijia@localhost jier] $lsceshi.py yunweijia.xls [yunweijia@localhost jier] $
Results:
Cell write data
Code:
# codingxlwt.Workbook UTF8 import module import xlwt# create a new excelworkbook = xlwt.Workbook (encoding='utf-8') # create a new sheetworksheet = workbook.add_sheet ('operator') # write data in the cell worksheet.write (0,0, "Hello") worksheet.write (0,1, "Hello") worksheet.write (0,2, "Hello") worksheet.write (1,1 "Hey") # Save excelworkbook.save (str ("yunweijia.xls"))
Run:
[yunweijia@localhost jier] $python3 ceshi.py [yunweijia@localhost jier] $lsceshi.py yunweijia.xls [yunweijia@localhost jier] $
Results:
Merge cells
Code:
# excelworkbook xlwt.Workbook (encoding='utf-8') # create a new sheetworksheet = workbook.add_sheet ('operator') # write data in the cell worksheet.write (0,0, "Hello") worksheet.write (0,1, "Hello") worksheet.write (0,2) "Hello everyone") # merge cells (first cell column Tail cell column, first cell row, tail cell row) worksheet.write_merge (1, 1, 0, 2, "hey") # Save excelworkbook.save (str ("yunweijia.xls"))
Run:
[yunweijia@localhost jier] $python3 ceshi.py [yunweijia@localhost jier] $lsceshi.py yunweijia.xls [yunweijia@localhost jier] $
Results:
Center display
PS: you can create multiple styles according to the actual situation, as long as the names are different.
Code:
# codingxlwt.Workbook UTF8 import module import xlwt# create a new excelworkbook = xlwt.Workbook (encoding='utf-8') # create a new sheetworksheet = workbook.add_sheet ('operator') # create a style head_style = xlwt.XFStyle () # create a style # set cell alignment ahead_lignment = xlwt.Alignment () # align initialization ahead_lignment .horz = xlwt.Alignment.HORZ_CENTER # horizontal Center ahead_lignment.vert = xlwt.Alignment.VERT_CENTER # Vertical Center head_style.alignment = ahead_lignment # style overload # write data worksheet.write (0) in the cell 0, "Hello") worksheet.write (0,1, "Hello") worksheet.write (0,2, "Hello") # merge cells (the first cell column Tail cell column, first cell row, tail cell row) worksheet.write_merge (1,2,0,2, "hey hey", head_style) # Save excelworkbook.save (str ("yunweijia.xls"))
Run:
[yunweijia@localhost jier] $python3 ceshi.py [yunweijia@localhost jier] $lsceshi.py yunweijia.xls [yunweijia@localhost jier] $
Results:
Modify fonts and colors
Code:
# codingxlwt.Workbook UTFMUF 8 create a style # create a style head_style = xlwt.XFStyle () # create a style # create a font head_font = xlwt.Font () # Font initialization head_font.name = 'Kai Body'# font head_font.height = 300 # size head_font.colour_index = 2 # Red head_style.font = head_font # style overload # set cell alignment ahead_lignment = xlwt.Alignment () # alignment initialization ahead_lignment.horz = xlwt.Alignment.HORZ_CENTER # horizontal Center ahead_lignment.vert = xlwt.Alignment.VERT_CENTER # Vertical Center head_style .write = ahead_lignment # style overload # write data worksheet.write (0) in the cell 0, "Hello") worksheet.write (0,1, "Hello") worksheet.write (0,2, "Hello") # merge cells (the first cell column Tail cell column, first cell row, tail cell row) worksheet.write_merge (1,2,0,2, "hey hey", head_style) # Save excelworkbook.save (str ("yunweijia.xls"))
Run:
[yunweijia@localhost jier] $python3 ceshi.py [yunweijia@localhost jier] $lsceshi.py yunweijia.xls [yunweijia@localhost jier] $
Results:
Thank you for reading this article carefully. I hope the article "how to use Python to operate excel tables" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.