Python:读取xlsx文件(Excel操作1)

#!/usr/bin/env python
# coding:UTF-8


"""
@version: python3.x
@author:曹新健
@contact: [email protected]
@software: PyCharm
@file: 1读取xlsx文件.py
@time: 2018/9/13 16:59
"""

from openpyxl.reader.excel import load_workbook

def readXlsx(path):
    #打开文件
    file = load_workbook(filename=path)
    #file.sheetnames获取所有工作表的名称
    sheets = file.sheetnames
    #print(sheets)
    #拿出一个表格file[sheets[0]]  file[sheetname]
    #sheet = file[sheets[0]]
    #sheet.max_row 行数
    #sheet.max_column 列数
    #sheet.title 表名称
    sheetsDic={}
    for sheetName in sheets:
        sheet = file[sheetName]
        sheetList = []
        for lineNum in range(1,sheet.max_row + 1):
            lineList = []
            for columnNum in range(1,sheet.max_column + 1):
                value = sheet.cell(row=lineNum,column=columnNum).value
                lineList.append(value)
            sheetList.append(lineList)

        sheetsDic[sheetName] = sheetList
    return sheetsDic


if __name__ == "__main__":
    print(readXlsx(r"C:\Users\曹新健\AppData\Local\Programs\Python\Python36\Scripts\test\备份-温习\文件读写\Excel读写\1.xlsx"))

 

你可能感兴趣的:(Python脚本编程,Python脚本编程)