import os
import xlrd
from xlutils.copy import copy
def base_dir(filename=None):
return os.path.join(os.path.dirname(__file__),filename)
"""對excel進行操作"""
work = xlrd.open_workbook(base_dir("excel.xls"))
# 索引到第X個工作表
sheet = work.sheet_by_index(0)
#查看有多少行
print(sheet.nrows)
#查看有多少列
print(sheet.ncols)
#獲取單元格內容
print(sheet.cell_value(5,2))
"""對excel進行修改/添加內容"""
# 找到需要更該的xls
work = xlrd.open_workbook(base_dir("excel.xls"))
print(work)
# 對數據表格進行復制
old_content = copy(work)
# 定位到Sheet1表
ws = old_content.get_sheet(0)
#在sheet1表中寫入內容
ws.write(7,2,"Tao")
#對修改后的內容進行保存
old_content.save(base_dir("data.xls"))