2016-09-09 23 views
1

初心者:100以上の異なるシートを持つExcelファイルがあります。各シートにはいくつかのテーブルとチャートが含まれています。Python - Excelファイルごとに異なるExcelファイルを保存する

すべてのシートを新しいExcelファイルとして保存します。

私は多くのPythonコードを試しましたが、どれも働いていませんでした。

こんにちは。ありがとう!

編集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 

ない特にエレガントが、私のためによく働いたと簡単な機能を提供します。うまくいけば誰かに便利です。

関連する問題