日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

pythone函数基础(11)读,写,修改EXCEL

發(fā)布時(shí)間:2023/12/2 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pythone函数基础(11)读,写,修改EXCEL 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#讀EXCEL需要導(dǎo)入xlrd模塊---在python控制臺pip install xlrd模塊
import xlrd
book = xlrd.open_workbook('stu3.xls')
sheet = book.sheet_by_index(0)
# sheet = book.sheet_by_name('sheet1')
# print(sheet.cell(0,0).value)#獲取指定單元格的內(nèi)容
# print(sheet.cell(1,0).value)
# print(sheet.row_values(0)) #獲取整行的數(shù)據(jù)
# print(sheet.row_values(1))
# print(sheet.col_values(0))#獲取整列的數(shù)據(jù)
# print(sheet.col_values(1))

print(sheet.nrows) #行數(shù)
print(sheet.ncols) #列數(shù)
for row in range(1,sheet.nrows):
print(sheet.row_values(row))

寫EXCEL需要導(dǎo)入 import xlwt -----在python控制臺pip install xlwt模塊 import xlwt
book = xlwt.Workbook() #新建一個(gè)excel
sheet = book.add_sheet('sheet1') #添加一個(gè)sheet頁

# sheet.write(0,0,'編號')
# sheet.write(0,1,'名字')
# sheet.write(0,2,'性別')
#
# sheet.write(1,0,'1')
# sheet.write(1,1,'馬春波')
# sheet.write(1,2,'男')

stu_info = [
['編號','姓名','密碼','性別','地址'],
[1,'machunbo','sdfsd23sdfsdf2','男','北京'],
[2,'machunbo2','sdfsd23sdfsdf2','男','北京'],
[3,'machunb3','sdfsd23sdfsdf2','男','北京'],
[4,'machunbo4','sdfsd23sdfsdf2','男','北京'],
[5,'machunbo5','sdfsd23sdfsdf2','男','北京'],
[6,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[7,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[8,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[9,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[10,'machunbo6','sdfsd23sdfsdf2','男','北京'],
[11,'machunbo6','sdfsd23sdfsdf2','男','北京'],
]
#6行5列
# row = 0 #行
# for stu in stu_info:
# sheet.write(row,0,stu[0])
# sheet.write(row,1,stu[1])
# sheet.write(row,2,stu[2])
# sheet.write(row,3,stu[3])
# sheet.write(row,4,stu[4])
# row+=1

# row = 0 #行
# for stu in stu_info:
# #stu
# col = 0 # 列
# # [1, 'machunbo', 'sdfsd23sdfsdf2', '男', '北京'],
# for s in stu: #控制列
# sheet.write(row,col,s) #0 3 男
# col+=1
# row+=1

for index,value in enumerate(stu_info):
# index 0
# value ['編號','姓名','密碼','性別','地址']

#index 1
#value [1,'machunbo','sdfsd23sdfsdf2','男','北京']
for index2,v2 in enumerate(value):
print(index,index2,v2)
#0 1
#1 machunbo
#2 sdfsd23sdfsdf2
#4 北京
sheet.write(index,index2,v2)

book.save('stu3.xls') #wps xls xlsx ,微軟的office xls
修改EXCEL,需要import xlutils-----在python控制臺pip install xlwt模塊 import xlrd
from xlutils import copy

#1、先打開原來的excel
#2、復(fù)制一份
#3、在復(fù)制的excel上修改
#4、保存

book = xlrd.open_workbook('stu3.xls')
new_book = copy.copy(book) #
sheet = new_book.get_sheet(0) #修改excel的時(shí)候,得用get_sheet()
sheet.write(0,0,'id')
sheet.write(0,3,'password')
new_book.save('stu3.xls')

轉(zhuǎn)載于:https://www.cnblogs.com/yulinlincoding/p/10278044.html

總結(jié)

以上是生活随笔為你收集整理的pythone函数基础(11)读,写,修改EXCEL的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。