2016-09-09 7 views
1

초보자 : Excel 파일이 100 개가 넘습니다. 각 시트에는 몇 가지 테이블과 차트가 있습니다.Python - Excel 파일마다 다른 Excel 파일 저장하기

모든 시트를 새 Excel 파일로 저장하고 싶습니다.

많은 파이썬 코드를 시도했지만 그 중 아무 것도 작동하지 않았습니다.

친절히 도와주세요. 감사!

편집 1 : 의견에 reponse, 이것은 내가 뭘하려 :

import pandas as pd 
import xlrd 

inputFile = 'D:\Excel\Complete_data.xlsx' 

#getting sheet names 
xls = xlrd.open_workbook(inputFile, on_demand=True) 
sheet_names = xls.sheet_names() 

path = "D:/Excel/All Files/" 

#create a new excel file for every sheet 
for name in sheet_names: 
     parsing = pd.ExcelFile(inputFile).parse(sheetname = name) 

     #writing data to the new excel file 
     parsing.to_excel(path+str(name)+".xlsx", index=False) 

는 정확히 말하면, 문제는 테이블과 차트를 복사에오고있다.

내가 너무 내 솔루션을 게시 할 예정이 문제를 통해 일한
+1

질문에 당신이 시도한 것을 포함시킬 수 있습니까? 그리고 당신에게 적합하지 않은 것은 무엇입니까? –

+0

저는 초보자이며 XLRD 모듈을 사용하여 기본 구문 분석을 시도했습니다. 새 Excel 파일에서 테이블이나 차트를 가져올 수 없었습니다. – Aditya

+0

@raderick이 (가) 내 코드를 추가했습니다. – Aditya

답변

2

, 나는 그것이 등 차트 특히 우아한하지만 나를 위해 잘 작동하지

import os 
import xlrd 
from xlutils.copy import copy 
import xlwt 

path = #place path where files to split up are 
targetdir = (path + "New_Files/") #where you want your new files 

if not os.path.exists(targetdir): #makes your new directory 
    os.makedirs(targetdir) 

for root,dir,files in os.walk(path, topdown=False): #all the files you want to split 
    xlsfiles=[f for f in files] #can add selection condition here 

for f in xlsfiles: 
    wb = xlrd.open_workbook(os.path.join(root, f), on_demand=True) 
    for sheet in wb.sheets(): #cycles through each sheet in each workbook 
     newwb = copy(wb) #makes a temp copy of that book 
     newwb._Workbook__worksheets = [ worksheet for worksheet in newwb._Workbook__worksheets if worksheet.name == sheet.name ] 
     #brute force, but strips away all other sheets apart from the sheet being looked at 
     newwb.save(targetdir + f.strip(".xls") + sheet.name + ".xls") 
     #saves each sheet as the original file name plus the sheet name 

에 영향을 쉽게 기능을 제공 할 방법을 모르겠어요. 누군가에게 유용하게 사용되기를 바랍니다.

관련 문제