0

I am trying to take multiple CSV files and merge them into one Excel workbook, but keeping each CSV file as its own sheet within the same workbook ((1)One Excel file/workbook, (3)three separate sheets). I am using a batch file to do it. The batch file I'm writing runs a python script I've written to download the CSV files, so would it be easier to create another python script for the batch file to run to create this single, multiple-sheet workbook? Any ideas on which would be easier and how it is done? It just feels like a mess now.

I have tried merging the 3 CSV files (they contain the same headers, just different data values for each) into one CSV file, but it just displays all of the data on a single sheet one listed after another without any label that tells you you're looking at another CSV file.

(I am just learning to code (started about a month ago) and the commented out parts are things I tried and didn't work)

::copy .\Output\*.csv .\Output\merged.csv
::"CLE_*"+"TOL_*"+"BUF_*" merged.csv
for /f "tokens=1-5 delims=/ " %%d in ("%date%") do set datevar=%%e-%%f-%%g
::for %%a in (*.xlsx) do start "" "%%a"
for %%a in (./Output/*%datevar%.csv) do start "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" "./Output/%%a"
::for /f "tokens=1-5 delims=/ " %%d in ("%date%") do start "C:\Program Files (x86)\Microsoft Office\root\Office16\EXCEL.EXE" ".\Output\*%%e-%%f-%%g.csv"
exit
2
  • 4
    A CSV file (which are specific text files) doesn't have tabs, and batch files can't interact with Excel… Commented Jun 12, 2020 at 19:17
  • 2
    You will need to pick a different scripting language. Should be able to do this within Vbscript, Python or Excel VBA Macro. Commented Jun 12, 2020 at 19:23

2 Answers 2

0

Just to get rid of misunderstandings. you don't want to merge the data to one table (worksheet), but you want to have separate worksheets with the same header for each of your csv - files inside one excel document?

Since you are downloading the csv files already with a python-script, you could use the csv module to read the files and afterwards copy them using the openpyxl module to separate worksheets in an excel document.

Below you have to change the 'data/' subfolder to your location and set the csv-delimiter to your csv's delimiter:

import os
import csv
import openpyxl as xl

files = ['data/'+ f for f in os.listdir('data')]

wb = xl.Workbook()
wb.remove_sheet(wb.active)


i = 0
for ff in files:
    with open(ff) as f:
        i += 1
        ws = wb.create_sheet(f'File {i}')
        reader = csv.reader(f, delimiter=',')
        for row in reader:
            ws.append(row)

wb.save('file.xlsx')

Sign up to request clarification or add additional context in comments.

Comments

0

one can load the .csv files from a directory and combine them all into one .xlsx excel file, each content of the.csv file on separate worksheets. Here's the code using Pandas:

import pandas as pd
import sys
import os
import glob
from pathlib import Path

extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]

writer = pd.ExcelWriter('fc15.xlsx') # Arbitrary output name
for csvfilename in all_filenames:

    # in case your locale settings use , instead of a dot
    txt = Path(csvfilename).read_text()
    txt = txt.replace(',', '.')

    text_file = open(csvfilename, "w")
    text_file.write(txt)
    text_file.close()
    
    print("Loading "+ csvfilename)
    df= pd.read_csv(csvfilename,sep=';', encoding='utf-8')

    df.to_excel(writer,sheet_name=os.path.splitext(csvfilename)[0])
    print("done")
writer.save()
print("task completed")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.