1. 程式人生 > >Python常用操作之excle檔案的基本操作(功能後續更新)

Python常用操作之excle檔案的基本操作(功能後續更新)

廢話不多說,直接上程式碼:

#!/usr/bin/env python3
# encoding : utf-8
# @author  : 'Jonny'
# @location: '北京'
# @date    : '2018/11/12 17:37'
# @Email   : [email protected]
# @File    : ExcleHelp.py
# @Software: PyCharm
# @blog    : https://blog.csdn.net/m0_37338590
# @describe: 實現對excle檔案的讀寫操作,其他功能後續繼續新增


import pandas as pd
import xlwt
import xlrd
import os
from xlutils.copy import copy

class ExcleHelp(object):
    def __init__(self,file_path_name):
        self.file_path_name = file_path_name
        # self.read_cols_name = read_cols_name
        # self.write_cols_name = write_cols_name
        # self.w_e = xlwt.Workbook(encoding="utf8")
        # self.sheet = self.w_e.add_sheet(sheet_name)

    def read_excle_pandas(self,read_cols_name):
        '''
        利用pandas庫,實現對整個excle檔案的某一列的一次性讀取
        :param read_cols_name: 需要讀取的列號
        :return: 返回該列的資料
        '''
        df = pd.DataFrame(pd.read_excel(self.file_path_name))
        data = [[i[j] for j in range(len(read_cols_name))]  for i in df[read_cols_name].values]
        return data

    def write_excle_pandas(self,save_file,write_data,write_cols_name):
        '''
        對檔案的某一類資料一次性寫入
        :param write_data: 要寫入的資料
        :param write_cols_name:要寫入列的列號
        :param save_file: 要寫入的檔名
        :return: None
        '''
        df_out = pd.DataFrame(write_data,columns =write_cols_name)
        df_out.to_excel(save_file)

    def write_excle_xlwt(self,sheet_num,row_num,col_num,write_data):
        '''
        對excle檔案的某一單元格的資料進行寫入,測試部分可以同時對檔案的某一單元格進行讀取,
        :param sheet_num: 要寫入excle表名
        :param row_num: 寫入單元格的行號
        :param col_num: 寫入單元格的列號
        :param write_data: 要寫入的資料
        :return: None
        '''
        try:
            rb = xlrd.open_workbook(self.file_path_name)
        except Exception as e:
            print("[ExcleHelp-->write_excle_xlwt--->rb]",e)
            return None
        #************************測試(用於測試是否可以同時讀寫操作)*******************************
        # sheet = rb.sheet_by_name(sheet_num)
        # print(sheet.cell_value(rowx=row_num, colx=0))
        # *********************************************************
        wb = copy(rb)
        sheet = wb.get_sheet(sheet=sheet_num)
        sheet.write(r=row_num,c=col_num,label=write_data)
        os.remove(self.file_path_name)
        wb.save(self.file_path_name)

    def read_excle_xlrt(self,sheet_num,row_num,col_num):
        '''
         對excle檔案的某一單元格的資料進行讀取
        :param sheet_num: 要讀取excle表名
        :param row_num: 讀取單元格的行號
        :param col_num: 讀取單元格的列號
        :return: 讀取到的資料
        '''
        try:
            rb = xlrd.open_workbook(self.file_path_name)
        except Exception as e:
            print("[ExcleHelp-->read_excle_xlrt--->rb]",e)
            return None
        sheet = rb.sheet_by_name(sheet_num)
        nrows = sheet.nrows
        ncols = sheet.ncols
        if row_num < nrows+1 and col_num < ncols+1:
            return sheet.cell_value(rowx=row_num,colx=col_num)
        else:
            return None




if __name__ == '__main__':
    print(ExcleHelp('01.xlsx').write_excle_xlwt(sheet_num="haha",row_num=12,col_num=2,write_data="第二次也成功了"))