在Python中将多个CSV文件合并到电子表格的单独选项卡中

时间:2022-04-13 22:57:36

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:

我有一个代码,可以在目录中生成多个CSV文件。我想在Excel中生成一个报告,该报告将包含CSV文件作为单独的选项卡。我使用了以下代码:

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?

但它给出了以下错误:AttributeError:'generator'对象没有属性'to_excel'不确定我哪里出错了。我是否需要导入任何特定的库来解决问题?

Python Version is 2.7

Python版本是2.7

3 个解决方案

#1


2  

There are two issues here:

这里有两个问题:

  1. Your generator expression allows you to lazily iterate dataframe objects. You can't export a generator expression to an Excel file.
  2. 您的生成器表达式允许您懒惰地迭代数据框对象。您无法将生成器表达式导出到Excel文件。

  3. Your sheet_name parameter is a constant. To export to multiple worksheets, you need to specify a different name for each worksheet.
  4. sheet_name参数是常量。要导出到多个工作表,您需要为每个工作表指定不同的名称。

You can use a simple for loop for this purpose:

为此,您可以使用简单的for循环:

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))

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:

您的工作表将命名为data0.csv,data1.csv等。如果您需要文件名作为工作表名称,则可以重构逻辑并使用os模块从路径中提取文件名:

import os

for f in all_files:
    df = pd.read_csv(f)
    df.to_excel(writer, sheet_name=os.path.basename(f))

#2


0  

you can user the pandas concate method

你可以使用pandas concate方法

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

axis是用户合并的方向

#3


0  

Although Python requires many fewer lines of code compared to VBA, I would probably use VBA for this kind of task.

尽管与VBA相比,Python需要更少的代码行,但我可能会将VBA用于此类任务。

' 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

#1


2  

There are two issues here:

这里有两个问题:

  1. Your generator expression allows you to lazily iterate dataframe objects. You can't export a generator expression to an Excel file.
  2. 您的生成器表达式允许您懒惰地迭代数据框对象。您无法将生成器表达式导出到Excel文件。

  3. Your sheet_name parameter is a constant. To export to multiple worksheets, you need to specify a different name for each worksheet.
  4. sheet_name参数是常量。要导出到多个工作表,您需要为每个工作表指定不同的名称。

You can use a simple for loop for this purpose:

为此,您可以使用简单的for循环:

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))

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:

您的工作表将命名为data0.csv,data1.csv等。如果您需要文件名作为工作表名称,则可以重构逻辑并使用os模块从路径中提取文件名:

import os

for f in all_files:
    df = pd.read_csv(f)
    df.to_excel(writer, sheet_name=os.path.basename(f))

#2


0  

you can user the pandas concate method

你可以使用pandas concate方法

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

axis是用户合并的方向

#3


0  

Although Python requires many fewer lines of code compared to VBA, I would probably use VBA for this kind of task.

尽管与VBA相比,Python需要更少的代码行,但我可能会将VBA用于此类任务。

' 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