openpyxl将Excel表格高位补0

import openpyxl

workbook = openpyxl.load_workbook(r"C:\Users\PC\Desktop\EDRMHelper2\TRDP显示故障清单设置_生成.xlsx") #打开工作间
worksheet = workbook.get_sheet_by_name('Sheet1') #打开该表格

for i in range(2,2774):
    fault_code = int(worksheet.cell(i,1).value)
    if fault_code < 10:
        print(worksheet.cell(i, 1).value + " < 10",'\n')
        worksheet.cell(i,1,'000' + str(fault_code))
    elif fault_code < 100:
        print(worksheet.cell(i, 1).value + " < 100", '\n')
        worksheet.cell(i, 1, '00' + str(fault_code))
    elif fault_code < 1000:
        print(worksheet.cell(i, 1).value + " < 1000", '\n')
        worksheet.cell(i, 1, '0' + str(fault_code))
workbook.save(r"C:\Users\PC\Desktop\EDRMHelper2\TRDP显示故障清单设置_生成.xlsx") #save 才有效

本文名称:openpyxl将Excel表格高位补0
文章分享:http://ybzwz.com/article/pcjhcp.html