Reputation: 441
I have a code which generates multiple CSV files in a directory. I want to generate a report in excel which will consist of the CSV files as separate tabs. I have used the below code for the same:
import pandas as pd
import os
import csv
import glob
path = "/MyScripts"
all_files = glob.glob(os.path.join(path, "*.csv"))
df_from_each_file = (pd.read_csv(f) for f in all_files)
df_from_each_file.to_excel(writer, sheet_name='ReturnData.csv')
writer.save()
But it gives below error: AttributeError: 'generator' object has no attribute 'to_excel' Not sure where i am going wrong. Do i need to import any specific library to solve the issue?
Python Version is 2.7
Upvotes: 8
Views: 26895
Reputation: 164613
There are two issues here:
sheet_name
parameter is a constant. To export to multiple worksheets, you need to specify a different name for each worksheet.You can use a simple for
loop for this purpose:
writer = pd.ExcelWriter('out.xlsx', engine='xlsxwriter')
df_from_each_file = (pd.read_csv(f) for f in all_files)
for idx, df in enumerate(df_from_each_file):
df.to_excel(writer, sheet_name='data{0}.csv'.format(idx))
writer.save()
Your worksheets will be named data0.csv
, data1.csv
, etc. If you need the filename as your sheet name, you can restructure your logic and use the os
module to extract the filename from path:
import os
writer = pd.ExcelWriter('out.xlsx', engine='xlsxwriter')
for f in all_files:
df = pd.read_csv(f)
df.to_excel(writer, sheet_name=os.path.basename(f))
writer.save()
Upvotes: 12
Reputation: 1209
Here is the complete source code from jpp solution:
import os
import pandas as pd
import glob
path = './'
all_files = glob.glob(os.path.join(path, "*.csv"))
writer = pd.ExcelWriter('out.xlsx', engine='xlsxwriter')
for f in all_files:
df = pd.read_csv(f)
df.to_excel(writer, sheet_name=os.path.splitext(os.path.basename(f))[0], index=False)
writer.save()
Upvotes: 6
Reputation: 20302
Although Python requires many fewer lines of code compared to VBA, I would probably use VBA for this kind of task.
' Merge data from multiple sheets into separate sheets
Sub R_AnalysisMerger2()
Dim WSA As Worksheet
Dim bookList As Workbook
Dim SelectedFiles As Variant
Dim NFile As Long
Dim FileName As String
Dim Ws As Worksheet, vDB As Variant, rngT As Range
Dim vFn, myFn As String
Application.ScreenUpdating = False
SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Files (*.csv*), *.csv*", MultiSelect:=True)
If IsEmpty(SelectedFiles) Then Exit Sub
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
FileName = SelectedFiles(NFile)
vFn = Split(FileName, "\")
myFn = vFn(UBound(vFn))
myFn = Replace(myFn, ".csv", "")
Set bookList = Workbooks.Open(FileName, Format:=2)
Set WSA = bookList.Sheets(1)
vDB = WSA.UsedRange
bookList.Close (0)
Set Ws = Sheets.Add(after:=Sheets(Sheets.Count))
ActiveSheet.Name = myFn
Ws.Range("a1").Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
Next
Application.ScreenUpdating = True
End Sub
' Merge data from multime files into one sheet.
Sub R_AnalysisMerger()
Dim WSA As Worksheet
Dim bookList As Workbook
Dim SelectedFiles() As Variant
Dim NFile As Long
Dim FileName As String
Dim Ws As Worksheet, vDB As Variant, rngT As Range
Application.ScreenUpdating = False
Set Ws = ThisWorkbook.Sheets(1)
Ws.UsedRange.Clear
'change folder path of excel files here
SelectedFiles = Application.GetOpenFilename(filefilter:="Excel Files (*.csv*), *.csv*", MultiSelect:=True)
For NFile = LBound(SelectedFiles) To UBound(SelectedFiles)
FileName = SelectedFiles(NFile)
Set bookList = Workbooks.Open(FileName, Format:=2)
Set WSA = bookList.Sheets(1)
With WSA
vDB = .UsedRange
Set rngT = Ws.Range("a" & Rows.Count).End(xlUp)(2)
If rngT.Row = 2 Then Set rngT = Ws.Range("a1")
rngT.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB
bookList.Close (0)
End With
Next
Application.ScreenUpdating = True
Ws.Range("A1").Select
End Sub
Upvotes: 0
Reputation: 3156
you can user the pandas concate method
csv1 = pd.read_csv(csv1_file_path)
csv2 = pd.read_csv(csv2_file_path)
merge_csv = pd.concat((csv1, csv2), axis=0)
axis is user for merge in which directions
Upvotes: -2