程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

I have to tell the story between Python and excel. I have modules. Do you have data?

編輯:Python

stay python in , Yes excel Read the form , Write , Additional data , Use the following three modules :

1、wlrd Read excel Table data

2、xlwt Create a new excel file , Then write and save the file .

3、xlutils Read into one excel file , Then modify or add , Can't operate xlsx, You can only operate xls.

One 、 read excel surface

read excel Want to use xlrd modular

1、 The import module

import xlrd

2、 open excel file

table = data.sheets()[0] # By index order
table = data.sheet_by_index(0) # By index order
table = data.sheet_by_name(u'Sheet1')# Get by name

The code is as follows :

import xlrd
data = xlrd.open_workbook(r"C:\Users\907968\Desktop\test.xlsx")
table1 = data.sheets()[0]
table2 = data.sheet_by_index(0)
table3=data.sheet_by_name(u'Sheet1')
print(table1)
print(table2)
print(table3)

return :

<xlrd.sheet.Sheet object at 0x0000000002F7F208>
<xlrd.sheet.Sheet object at 0x0000000002F7F208>
<xlrd.sheet.Sheet object at 0x0000000002F7F208>

3、 Gets the number of rows and columns

import xlrd
data = xlrd.open_workbook(r"C:\Users\907968\Desktop\test.xlsx")
table = data.sheets()[0]
nrows = table.nrows
ncols = table.ncols
print(" Row number :%d\n Number of columns :%d"%(nrows,ncols))

return :

Row number :13

Number of columns :3

4、 Get values for entire rows and columns , Return... As a list

rows = table.row_values(0)
cols = table.col_values(0)
print("rows:%s\ncols:%s"%(rows,cols))

return :

rows:['A1', 'B1', 'C1']
cols:['A1', 'A2', 'A3', 'A4', 'A5', 'A6', 'A7', 'A8', 'A9', 'A10', 'A11', 'A12', 'A13']

5、 Get cell data

cell_A1 = table.cell_value(0,0)
cell_C4 = table.cell_value(3,2)
print("A1:%s\nC4:%s"%(cell_A1,cell_C4))

return :

A1:A1
C4:C4

You can also use row and column indexes to get cell data

cell_A1 = table.row(0)[0].value
cell_C4 = table.col(2)[3].value
print("A1:%s\nC4:%s"%(cell_A1,cell_C4))

return :

A1:A1
C4:C4

3、 ... and 、 Write excel operation

1、 Import :

import xlwt

2、 establish workbook

workbook = xlwt.Workbook(encoding='utf-8', style_compression=0)

encoding: Set character encoding , Generally, it should be set like this :w = Workbook(encoding=’utf-8’), You can go to excel Chinese is output in . The default is ascii

style_compression: Indicates whether to compress , Not commonly used .

3、 Create a sheet object , One sheet Object correspondence Excel A table in the file

sheet = workbook.add_sheet(‘test’, cell_overwrite_ok=True)

Among them test It's the name of this table ,cell_overwrite_ok, Indicates whether cells can be overwritten , It's actually Worksheet An instantiated parameter , The default value is False

4、 Add data to the table

sheet.write(0, 0, ‘EnglishName’) # Among them ’0- That's ok , 0- Column ’ Specify the cells in the table ,'EnglishName’ Is what is written to the unit

sheet.write(1, 0, 'Marcovaldo')
txt1 = ' Chinese name '
sheet.write(0, 1, txt1)
txt2 = ' Marco Vado '
sheet.write(1, 1, txt2)

5、 preservation

workbook.save(r'e:\test1.xls')

Four 、 Additional data

import xlrd
import xlutils.copy
data = xlrd.open_workbook(r'C:\Users\907968\Desktop\test222.xls')
ws = xlutils.copy.copy(data)
table=ws.get_sheet(0)
table.write(0,3,'D1')
ws.save(r'C:\Users\907968\Desktop\test222.xls')

Before addition :

A1 B1 C1

After addition :

A1 B1 C1 D1

About Python Technology reserve

Learn from good examples Python Whether it's employment or sideline, it's good to make money , But learn to Python Still have a learning plan . Finally, let's share a complete set of Python Learning materials , For those who want to learn Python Let's have a little help !

One 、Python Learning routes in all directions

Python All directions are Python Sort out the common technical points , Form a summary of knowledge points in various fields , The use of it is , You can find the corresponding learning resources according to the above knowledge points , Make sure you learn more comprehensively .

Two 、 Learning software

If a worker wants to do a good job, he must sharpen his tools first . Study Python Common development software is here , It saves you a lot of time .

3、 ... and 、 Getting started video

When we were watching videos to learn , You can't just move your eyes and brain without hands , A more scientific way to learn is to use them after understanding , At this time, the hand training program is very suitable .

Four 、 Practical cases

Optical theory is useless , Learn to knock together , Do it , Can you apply what you have learned to practice , At this time, we can make some practical cases to learn .

5、 ... and 、 Interview information

We learn Python Must be to find a well paid job , The following interview questions are from Ali 、 tencent 、 The latest interview materials of big Internet companies such as byte , And the leader Ali gave an authoritative answer , After brushing this set of interview materials, I believe everyone can find a satisfactory job .


This full version of Python A full set of learning materials has been uploaded CSDN, Friends can scan the bottom of wechat if necessary CSDN The official two-dimensional code is free 【 Guarantee 100% free


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved