zoukankan      html  css  js  c++  java
  • python读取excel文件

    python操作excel常用的库包:

    xlrd(主要做读取操作)

    xlwt(主要做写入的操作)

    xlutils(操作excel的工具)

    openpyxl(可完成读取、写入、操作)。

    本文写的是通过openpyxl完成对excel的操作,openpyxl是一个Python库,用于读取/写入Excel 2010 xlsx / xlsm / xltx / xltm文件。

    官网文档:https://openpyxl.readthedocs.io/en/stable/

    1.通过python创建excel文件并将数据写入

     1 # workbook = Workbook()         # 创建工作簿对象
     2 
     3 # sheet = workbook.active       # 得到sheet对象
     4 
     5 # # 将数据写入 append接收一个可迭代对象
     6 # sheet.append(['A1', 'B1', 'C1'])
     7 
     8 # # 第二种写入方式
     9 # sheet["B2"] = "这是B2"
    10 
    11 # # 遍历数据写入
    12 # for i in range(1,5):
    13 #     sheet[f"D{i}"] = "test"
    14 
    15 # # 保存创建的文件
    16 # workbook.save("./source_file/test.xlsx")

     2.通过python打开已有的excel文件读取并写入数据

     1 #得到工作簿对象
     2 workbook = openpyxl.load_workbook("./source_file/test.xlsx") 
     3 # 获取当前活动的sheet页对象
     4 sheet = workbook.active
     5 # 获取指定的sheet页对象
     6 sheet1 = workbook["Sheet1"] 
     7 
     8 # 第一种读取方式
     9 print(sheet["A1"].value)
    10 # 第二种读取方式: _get_cell(row,cloumn)
    11 print(sheet._get_cell(1,1).value)
    12 
    13 # 写入数据
    14 sheet["F2"] = "F2"
    15 
    16 # 如果只读取了数据可以直接关闭
    17 workbook.close()
    18 
    19 # 有将数据更改或写入需要保存文件
    20 workbook.save("./source_file/test.xlsx")

    3.更多的操作请参考官网文档,下面提供一个封装好的版本。

     1 class ExcelFile:
     2 
     3     def __init__(self, filepath):
     4         self.filepath = filepath
     5         self.workbook = openpyxl.load_workbook(filepath)
     6         self.sheet = self.workbook.active    # 默认创建当前活动的sheet页对象
     7 
     8     def getSheet(self) -> list:
     9         """获取所有Sheet页"""
    10         return self.workbook.sheetnames
    11 
    12     def setSheet(self, sheetName: str):
    13         """设置Sheet页"""
    14         self.sheet = self.workbook[sheetName]
    15 
    16     def getCell(self, column: str, row):
    17         """获取指定的单元格对象"""
    18         return self.sheet[column+ str(row)]
    19 
    20     def readCell(self, column: str, row=""):
    21         """获取指定的单元格值"""
    22         return self.getCell(column, row).value
    23 
    24     def writeCell(self, column: str, row, value):
    25         """给指定单元格写入值"""
    26         self.sheet[column + str(row)] = str(value)
    27 
    28     def clearCell(self, column: str, row):
    29         """清空单元格的值"""
    30         self.writeCell(column, row, "")
    31 
    32     def getMaxRow(self):
    33         """获取最大行"""
    34         return self.sheet.max_row
    35 
    36     def getMaxColumn(self):
    37         """获取最大列"""
    38         return self.sheet.max_column
    39 
    40     def saveFile(self):
    41         self.workbook.save(self.filepath)

     

  • 相关阅读:
    docker
    opencart
    Why is setTimeout(fn, 0) sometimes useful?
    linux下php环境配置
    xampp for linux
    Where to go from here
    freefcw/hustoj Install Guide
    khan academy js
    SDWebImage
    基于OpenCV 的iOS开发
  • 原文地址:https://www.cnblogs.com/XhyTechnologyShare/p/14316935.html
Copyright © 2011-2022 走看看