如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#encoding=utf-8 import xlrd from xlwt import * #------------------讀數據--------------------------------- fileName = "C:\\Users\\st\\Desktop\\test\\20170221131701.xlsx" bk = xlrd.open_workbook(fileName) shxrange = range (bk.nsheets) try : sh = bk.sheet_by_name( "Sheet1" ) except : print "代碼出錯" nrows = sh.nrows #獲取行數 book = Workbook(encoding = 'utf-8' ) sheet = book.add_sheet( 'Sheet1' ) #創建一個sheet for i in range ( 1 ,nrows): row_data = sh.row_values(i) #獲取第i行第3列數據 #sh.cell_value(i,3) #---------寫出文件到excel-------- print "-----正在寫入 " + str (i) + " 行" sheet.write(i, 1 , label = sh.cell_value(i, 3 )) #向第1行第1列寫入獲取到的值 sheet.write(i, 2 , label = sh.cell_value(i, 5 )) #向第1行第2列寫入獲取到的值 book.save( "C:\\Users\\st\\Desktop\\test\\demo1.xls" ) |
以上這篇python讀取excel指定列數據并寫入到新的excel方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_32502511/article/details/56496195