# -*- coding: utf-8 -*-
import xlrd
import xlwt
from datetime import date,datetime
def read_excel():
# 打開文件
workbook = xlrd.open_workbook(r'F:\demo.xlsx')
# 獲取所有sheet
print workbook.sheet_names() # [u'sheet1', u'sheet2']
sheet2_name = workbook.sheet_names()[1]
# 根據(jù)sheet索引或者名稱獲取sheet內(nèi)容
sheet2 = workbook.sheet_by_index(1) # sheet索引從0開始
sheet2 = workbook.sheet_by_name('sheet2')
# sheet的名稱,行數(shù),列數(shù)
print sheet2.name,sheet2.nrows,sheet2.ncols
# 獲取整行和整列的值(數(shù)組)
rows = sheet2.row_values(3) # 獲取第四行內(nèi)容
cols = sheet2.col_values(2) # 獲取第三列內(nèi)容
print rows
print cols
# 獲取單元格內(nèi)容
print sheet2.cell(1,0).value.encode('utf-8')
print sheet2.cell_value(1,0).encode('utf-8')
print sheet2.row(1)[0].value.encode('utf-8')
# 獲取單元格內(nèi)容的數(shù)據(jù)類型
print sheet2.cell(1,0).ctype
if __name__ == '__main__':
read_excel()
1、python讀取excel中單元格內(nèi)容為日期的方式
python讀取excel中單元格的內(nèi)容返回的有5種類型,即上面例子中的ctype:
ctype : 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
即date的ctype=3,這時需要使用xlrd的xldate_as_tuple來處理為date格式,先判斷表格的ctype=3時xldate才能開始操作?,F(xiàn)在命令行看下:
>>> sheet2.cell(2,2).ctype #1990/2/22
>>> sheet2.cell(2,1).ctype #24
>>> sheet2.cell(2,0).ctype #小胖
>>> sheet2.cell(2,4).ctype #空值(這里是合并單元格的原因)
>>> sheet2.cell(2,2).value #1990/2/22
33656.0
>>> xlrd.xldate_as_tuple(sheet2.cell_value(2,2),workbook.datemode)
(1992, 2, 22, 0, 0, 0)
>>> date_value = xlrd.xldate_as_tuple(sheet2.cell_value(2,2),workbook.datemode)
>>> date_value
(1992, 2, 22, 0, 0, 0)
>>> date(*date_value[:3])
datetime.date(1992, 2, 22)
>>> date(*date_value[:3]).strftime('%Y/%m/%d')
'1992/02/22'
即可以做下簡單處理,判斷ctype是否等于3,如果等于3,則用時間格式處理:
if (sheet.cell(row,col).ctype == 3):
date_value = xlrd.xldate_as_tuple(sheet.cell_value(rows,3),book.datemode)
date_tmp = date(*date_value[:3]).strftime('%Y/%m/%d')
讀取整行整列的內(nèi)容
>>> sheet2.col_values(4)
[u'\u5173\u7cfb', u'\u597d\u670b\u53cb', '', u'\u540c\u5b66', '', '', u'\u4e00\u4e2a\u4eba', '']
>>> for i in range(sheet2.nrows):
print sheet2.col_values(4)[i]
關(guān)系
好朋友
同學(xué)
一個人
>>> sheet2.row_values(7)
[u'\u65e0\u540d', 20.0, u'\u6682\u65e0', '', '']
>>> for i in range(sheet2.ncols):
print sheet2.row_values(7)[i]
無名
20.0
暫無
>>>
3、獲取合并的單元格
讀取文件的時候需要將formatting_info參數(shù)設(shè)置為True,默認是False,所以上面獲取合并的單元格數(shù)組為空,
>>> workbook = xlrd.open_workbook(r'F:\demo.xlsx',formatting_info=True)
>>> sheet2 = workbook.sheet_by_name('sheet2')
>>> sheet2.merged_cells
[(7, 8, 2, 5), (1, 3, 4, 5), (3, 6, 4, 5)]
merged_cells返回的這四個參數(shù)的含義是:(row,row_range,col,col_range),其中[row,row_range)包括row,不包括row_range,col也是一樣,即(1, 3, 4, 5)的含義是:第1到2行(不包括3)合并,(7, 8, 2, 5)的含義是:第2到4列合并。
利用這個,可以分別獲取合并的三個單元格的內(nèi)容:
>>> print sheet2.cell_value(1,4) #(1, 3, 4, 5)
好朋友
>>> print sheet2.cell_value(3,4) #(3, 6, 4, 5)
同學(xué)
>>> print sheet2.cell_value(7,2) #(7, 8, 2, 5)
暫無
發(fā)現(xiàn)規(guī)律了沒?是的,獲取merge_cells返回的row和col低位的索引即可! 于是可以這樣一勞永逸:
>>> merge = []
>>> for (rlow,rhigh,clow,chigh) in sheet2.merged_cells:
merge.append([rlow,clow])
>>> merge
[[7, 2], [1, 4], [3, 4]]
>>> for index in merge:
print sheet2.cell_value(index[0],index[1])
暫無
好朋友
同學(xué)
>>>
寫xlsxwrite
from xlsxwriter.workbook import Workbook
list1 = []
list2 = []
with open('a.txt', 'r') as f:
while True:
i = f.readline()
if i=='':
break
data = i.split(' ')
list1.append(data)
for i in list1:
if len(i)==1:
path = i
path = i[0].replace('\n','')
if i[0].startswith('-r'):
list2.append([path, i[0]])
workbook = Workbook('a.xlsx')
worksheet = workbook.add_worksheet('sheet1')
workformat = workbook.add_format({'bold':12})
worksheet.write_row('A1', ['num', 'dir'], workformat)
for num, dir in enumerate(list2):
worksheet.write_row(num+1, 0, dir, workformat)
workbook.close()
OPENPYXL
import datetime
from random import choice
from time import time
from openpyxl import load_workbook
from openpyxl.utils import get_column_letter
# 設(shè)置文件 mingc
addr = "openpyxl.xlsx"
# 打開文件
wb = load_workbook(addr)
# 創(chuàng)建一張新表
ws = wb.create_sheet()
# 第一行輸入
ws.append(['TIME', 'TITLE', 'A-Z'])
# 輸入內(nèi)容(500行數(shù)據(jù))
for i in range(500):
TIME = datetime.datetime.now().strftime("%H:%M:%S")
TITLE = str(time())
A_Z = get_column_letter(choice(range(1, 50)))
ws.append([TIME, TITLE, A_Z])
# 獲取最大行
row_max = ws.max_row
# 獲取最大列
con_max = ws.max_column
# 把上面寫入內(nèi)容打印在控制臺
for j in ws.rows: # we.rows 獲取每一行數(shù)據(jù)
for n in j:
print(n.value, end="\t") # n.value 獲取單元格的值
print()
# 保存,save(必須要寫文件名(絕對地址)默認 py 同級目錄下,只支持 xlsx 格式)
wb.save(addr)
可以參考:https://blog.csdn.net/weixin_43094965/article/details/82226263