亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python+Xlwings 刪除Excel的行和列

 更新時(shí)間:2020年12月19日 10:29:57   作者:Peanut_C  
這篇文章主要介紹了Python+Xlwings 刪除Excel的行和列的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下

一、需求:

  某公司管理的多個(gè)資管計(jì)劃每天生成A表,業(yè)務(wù)人員需手工打開每個(gè)A表,將某些行、列刪除后方可打印上報(bào)。

  現(xiàn)擬采用程序代替手工操作。

二、分析:

  1、應(yīng)在原始文件的副本上操作,因此需拷貝文件夾內(nèi)所有Excel至目標(biāo)目錄;

  解答:使用shutil.copy()

  2、需打開excel并刪除指定的行和列;

  解答:openpyxl不支持xls格式,xlwt無法刪除行和列,最終選擇xlwings;

三、代碼實(shí)現(xiàn):

#!/usr/bin/env python
# _*_ coding:utf-8 _*_
 
"""
 
@Time    : 2019-12-27 17:16
@Author  : Peanut_C
@FileName: excel_converter.py
 
"""
 
 
import os
import shutil
import xlwings as xw
 
current_dir = os.getcwd()
src_dir = os.path.join(current_dir, 'src_dir')
dst_dir = os.path.join(current_dir, 'dst_dir')
exist_list = ['YYYY', 'XXXX']  # 要保留行的A列關(guān)鍵字
 
 
def file_copy(source_dir, destination_dir):
    os.chdir(source_dir)
    for file in os.listdir(source_dir):
        shutil.copy(file, destination_dir)
    print('INFO ===>>> 文件拷貝完成!')
 
 
def excel_modifier(wk_dir):
    os.chdir(wk_dir)
    for file in os.listdir(wk_dir):
        # 檢查文件格式是否為xls
        # print(type(os.path.splitext(file)[1]))
        if os.path.splitext(file)[1] != '.xls':
            print(file, '===>>>文件格式不正確,請(qǐng)檢查!')
        else:
            print('開始處理===>>>', file)
            # 創(chuàng)建app,打開工作表
            app = xw.App(visible=False, add_book=False)
            app.screen_updating = False
            app.display_alerts = False
            load_wb = app.books.open(file)
            load_ws = load_wb.sheets.active
            print('\t已打開工作表……')
 
            # 獲取總行數(shù)(列數(shù)固定不需要獲取)
            rows = load_ws.api.UsedRange.Rows.count
            # cols = load_ws.api.UsedRange.Columns.count
 
            # 獲取需要處理的A列范圍
            a_range = load_ws.range('A1:A'+str(rows-4))  # 得到range對(duì)象
 
            # 將range中每行對(duì)象存放到列表中并倒序
            print('\t開始獲取標(biāo)志列……')
            cell_list = []
            for cell in a_range:
                cell_list.append(cell)
            cell_list.reverse()
            # print(cell_list)
 
            # 將表頭拆分、重新合并,為插入的值騰地方
            print('\t開始調(diào)整合并單元格……')
            load_ws.range('H3:J3').api.unmerge()  # 拆分單元格
            load_ws.range('H3:I3').api.merge()  # 合并單元格
            load_ws.range('J3').value = 'xxx'  # 插入值
 
            # 設(shè)定將A列每個(gè)值與要保留列表比對(duì),比對(duì)不上則刪除整行
            print('\t開始調(diào)整行和列……')
            for cell in cell_list:
                if cell.value is not None:  # 單元格不為空則開始比對(duì)
                    find_flag = 0  # 匹配標(biāo)志
                    for exist_value in exist_list:
                        if cell.value.find(exist_value) != -1:
                            find_flag = 1  # 匹配則將標(biāo)志置為1
                            break  # 一個(gè)單元格只要匹配就不再比對(duì)保留列表剩下的值
                        else:
                            continue  # 匹配不上則繼續(xù)
                    if find_flag == 0:  # 沒匹配上的刪除整行
                        cell_to_del = cell.address
                        # print(cell_to_del)
                        load_ws.range(cell_to_del).api.EntireRow.Delete()
                else:  # 單元格為空直接刪除
                    cell_to_del = cell.address
                    # print(cell_to_del)
                    load_ws.range(cell_to_del).api.EntireRow.Delete()
 
            # 處理列,將指定列從大到小刪除(避免先刪除小列導(dǎo)致后續(xù)列號(hào)變動(dòng))
            load_ws.api.columns('K').delete
            load_ws.api.columns('G').delete
            load_ws.api.columns('B').delete
            # 美化處理后的Excel
            print('\t開始美化表格……')
            load_ws.range('A1:H24').columns.autofit()
            # 處理完畢,保存、關(guān)閉、退出Excel
            load_wb.save()
            load_wb.close()
            app.quit()
            print('處理完畢===>>>', file, '\n\n')
 
 
if __name__ == '__main__':
    file_copy(src_dir, dst_dir)
    excel_modifier(dst_dir)
    print('任務(wù)結(jié)束,請(qǐng)至dst_dir目錄查看文件!\n\n')
    os.system('pause')

四、運(yùn)行情況:

  腳本測(cè)試完畢后,使用pyinstaller -F excel_converter.py -i icon.ico打包成為exe文件。

  將可執(zhí)行程序拷貝至業(yè)務(wù)人員電腦可直接執(zhí)行,原始文件拖入src_dir,處理后文件輸出至dst_dir。

  經(jīng)測(cè)試excel2013使用正常,excel2007無法連接。

以上就是Python+Xlwings 刪除Excel的行和列的詳細(xì)內(nèi)容,更多關(guān)于python 刪除Excel的行和列的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論