简体   繁体   中英

Is there a method to change the number format of 100 excel files with 20 sheets each from 2 decimal to 6 decimal using Python?

import pandas as pd
import os
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile

path=os.getcwd()
files=os.listdir(path)
for i in range(len(files)):
    filename = files[i]
    filepath=os.path.join(path,filename)
    
print(filepath)
df=pd.ExcelFile(filepath)  # read sheet name 
sheet = df.sheet_names
print(sheet)
df=pd.read_excel(filepath, sheet_name=sheet,skiprows = 5,  nrows=15, usecols = 'E:L')

print(df)

when I click on the number it show 6 digits in the header but I want to change it into the excel also

import openpyxl

wb = openpyxl.load_workbook('ds.xlsx')
ws = wb.active

for row in ws.iter_rows():
    for cell in row:
        # only relevant column and without header
        if cell.column_letter == 'A' and cell.row > 1: # put the cell number from where to where you want to check
            ws[cell.coordinate].number_format = '0.0000000' # it will change the number format

wb.save('ds2.xlsx')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM