python openpyxl 批量更改excel的列宽、列高(多线程)

前言

我们从网上下载的批量excel文件中,有时候单元格是非常小的,不利于我们查阅,pandas可以很方便的读取excel表,但是它并不能改变行款、高,对此我们应该使用openpyxl批量修改宽、高。由于程序涉及大量的IO操作,我们可以使用多线程进行操作。

简介

get_all_excel(path): 获得path下的所有文件,获得一个list
split_list(all_list, count) : 对list进行切分,分成count个list,获得lists。count也是进程的数量
mutil_thread(lists) : 将lists传入,在此开启多线程
change_width_height(filelist, index, width=20, height=17) :对filelist下的所有文件进行修改,index是线程的索引值,宽高已经设置+默认值,可以传参数。

代码

import openpyxl
import os
import threading

def get_all_excel(path):
    type = ('.xlsx')
    filelist = []

    for a, b, c in os.walk(path):
        for name in c:
            fname = os.path.join(a, name)
            if fname.endswith(type):
                filelist.append(fname)

    return filelist


def split_list(all_list, count):
    end_list = []
    n = len(all_list) // count # 这里把一个列表切分成count个列表,count控制进程数
    for i in range(0, len(all_list), n):
        name = all_list[i:i + n]
        end_list.append(name)
    return end_list



def mutil_thread(lists):
    thread_list = []

    for i in range(len(lists)):
        t1 = threading.Thread(target=change_width_height, args=((lists[i]), i))
        thread_list.append(t1)

    for i in range(len(thread_list)):
        thread_list[i].start()

    for t in thread_list:
        t.join()

    print("程序结束")


def change_width_height(filelist, index, width=20, height=17):
    count = 1

    for excel in filelist:
        try:
            wb = openpyxl.load_workbook(excel)
            ws = wb[wb.sheetnames[0]]   #获取sheet

            for i in range(1, ws.max_column + 1):
                ws.column_dimensions[openpyxl.utils.get_column_letter(i)].width = width

            for i in range(1, ws.max_row + 1):
                ws.row_dimensions[i].height = height
                
            wb.save(excel)
            print("线程%d   " %index + excel + "   成功第%d次" % count + "已完成%.2f" % ((count / filelist.__len__()) * 100) + "%")
            
        except Exception as e:
            print(e)
        count += 1

if __name__ == '__main__':
    path = r"F:\XXXXX\XXXXXX"
    filelist = get_all_excel(path)
    filelists = split_list(filelist, 5) #5个线程

    mutil_thread(filelists)

总结

  • 善于利用try except,可以使程序持续运行
  • 多线程的使用应该在有大量IO操作的前提下进行,否则应优先使用单线程
  • openpyxl 可以对excel表做大量的格式操作,和pandas形成互补
openpyxl可以通过`row_dimensions`属性来读取和修改Excel表格中的行高。具体操作如下: 首先,需要导入相关函数: ```python from openpyxl import load_workbook ``` 然后,使用`load_workbook`函数加载Excel文件: ```python wb = load_workbook('mainbuilding33.xlsx') ``` 接下来,可以通过`wb.sheetnames`获取所有的工作表名称,并选择需要操作的工作表。假设选择第一个工作表: ```python sheet = wb\[wb.sheetnames\[0\]\] ``` 然后,可以使用`sheet.row_dimensions\[row_number\].height`来获取指定行的行高。其中,`row_number`是行号,从1开始计数。例如,获取第2行的行高: ```python row_height = sheet.row_dimensions\[2\].height ``` 最后,可以将获取到的行高打印出来或进行其他操作。 综上所述,使用openpyxl可以方便地读取Excel表格中的行高。 #### 引用[.reference_title] - *1* [python openpyxl 批量更改excel列高多线程)](https://blog.csdn.net/weixin_49328057/article/details/113885019)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [python openpyxl读取Excel文件](https://blog.csdn.net/y65184536/article/details/81777149)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值