In daily work, the operation of Excel is essential. If a large number of Excel operations are involved, the efficiency of manual processing is still relatively low, so Excel automatic words are absolutely necessary. Today I will share with you how to use Python to Batch processing Excel.
Install
Python operates Excel mainly using two libraries xlrd and xlwt, the former is responsible for reading and the latter is responsible for writing.
These two libraries can be installed directly through the pip command.
pip3 install xlrd
pip3 install xlwt
good
Let's take a look at the reading module first, which has the following Excel file.
1. Get sheet
file_path = './person.xls'
data = xlrd.open_workbook(file_path)
names = data.sheet_names()
print(names)
# 输出结果
['list']
Of course, we can also get the specified sheet by subscript or name.
sheet = data.sheets()[0]
print(sheet)
sheet = data.sheet_by_name('list')
print(sheet)
# 输出结果
Sheet 0:<list>
Sheet 0:<list>
2. Operation of rows and columns
Each sheet in Excel is composed of rows and columns. Let's take a look at the operations of rows and columns first.
rows = table.nrows
print(rows)
# 输出结果
4
cols = table.ncols
print(cols)
# 输出结果
3
print(table.row_slice(1))
# 输出结果
[text:'张三', number:18.0, text:'男']
Read Excel table data by row and column.
for row in range(rows):
for col in range(cols):
print(table.cell(row, col), table.cell_type(row, col))
# 输出结果
text:'name' 1
text:'age' 1
text:'sex' 1
text:'张三' 1
number:18.0 2
...
To write data to Excel through rows and columns, you need to use the library xlwt to write data.
import xlwt
workbook = xlwt.Workbook(encoding=ascii)
worksheet = workbook.add_sheet('my sheet') # 创建工作表
for i in range(10):
for j in range(10):
worksheet.write(i, j, i + j) # 写入内容
workbook.save('data.xls')
Of course, we can also do some settings on the cells.
# 更改列宽:
worksheet.col(10).width = 256 * 20
# 更改行高:
style = xlwt.easyxf('font:height 360;') # 18pt,类型小初的字号
row = worksheet.row(10)
row.set_style(style)
# 填充颜色
pattern = xlwt.Pattern()
pattern.pattern = xlwt.Pattern.SOLID_PATTERN
pattern.pattern_fore_colour = 2
style = xlwt.XFStyle()
style.pattern = pattern
worksheet.write(4, 4, '填充颜色', style)
# 边框样式
borders = xlwt.Borders()
borders.left = xlwt.Borders.DASHED
borders.right = xlwt.Borders.DASHED
borders.top = xlwt.Borders.THIN
borders.bottom = xlwt.Borders.THICK
style = xlwt.XFStyle()
style.borders = borders
worksheet.write(5, 5, '边框样式', style)
# 合并单元格
worksheet.write_merge(6, 7, 7, 9, '合并单元格') # 合并 6-7 行,7-9 列
Today I shared with you how to process Excel in batches through Python, hoping to improve the work efficiency of my friends.
Do you have some magical operation methods about Excel, you can share them with you in the comment area~
at last
As an IT person, I have compiled some python learning materials by myself, which were shared with me by others. I hope it will be helpful to you.
1. Learning routes in all directions of Python
The technical points in all directions of Python are sorted out to form a summary of knowledge points in various fields. Its usefulness lies in that you can find corresponding learning resources according to the above knowledge points to ensure that you can learn more comprehensively.
2. Essential development tools for Python
4. Python video collection
Watching the zero-based learning video is the fastest and most effective way to learn. Following the teacher's ideas in the video, it is still very easy to get started from the basics to the in-depth.
5. Practical cases
Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.
6. Python exercises
Check the learning results.
7. Interview materials
We must learn Python to find high-paying jobs. The following interview questions are the latest interview materials from first-line Internet companies such as Ali, Tencent, and Byte, and Ali bosses have given authoritative answers. After finishing this set The interview materials believe that everyone can find a satisfactory job.
This full version of the full set of learning materials for Python has been uploaded to CSDN. If you need it, you can scan the QR code of CSDN official certification below on WeChat to get it for free [100% free guarantee]