-1

I have following N number of invoice data in Excel and I want to create CSV of that file so that it can be imported whenever needed...so how can I archive this?

Here is a screenshot:

enter image description here

martineau
  • 119,623
  • 25
  • 170
  • 301
Alpesh Valaki
  • 1,611
  • 1
  • 16
  • 36

2 Answers2

1

Assuming you have a Folder "excel" full of Excel Files within your Project-Directory and you also have another folder "csv" where you intend to put your generated CSV Files, you could pretty much easily batch-convert all the Excel Files in the "excel" Directory into "csv" using Pandas.

It will be assumed that you already have Pandas installed on your System. Otherwise, you could do that via: pip install pandas. The fairly commented Snippet below illustrates the Process:

# IMPORT DATAFRAME FROM PANDAS AS WELL AS PANDAS ITSELF
from pandas import DataFrame
import pandas as pd
import os


# OUR GOAL IS:::
# LOOP THROUGH THE FOLDER: excelDir.....
# AT EACH ITERATION IN THE LOOP, CHECK IF THE CURRENT FILE IS AN EXCEL FILE,
# IF IT IS, SIMPLY CONVERT IT TO CSV AND SAVE IT:

for fileName in os.listdir(excelDir):
    #DO WE HAVE AN EXCEL FILE?
    if fileName.endswith(".xls") or fileName.endswith(".xlsx"):
        #IF WE DO; THEN WE DO THE CONVERSION USING PANDAS...
        targetXLFile    = os.path.join(excelDir,    fileName)
        targetCSVFile   = os.path.join(csvDir,      fileName) + ".csv"

        # NOW, WE READ "IN" THE EXCEL FILE
        dFrame          = pd.read_excel(targetXLFile)

        # ONCE WE DONE READING, WE CAN SIMPLY SAVE THE DATA TO CSV
        pd.DataFrame.to_csv(dFrame, path_or_buf=targetCSVFile)

Hope this does the Trick for you.....
Cheers and Good-Luck.

Poiz
  • 7,611
  • 2
  • 15
  • 17
0

Instead of putting total output into one csv, you could go with following steps.

  1. Convert your excel content to csv files or csv-objects.

  2. Each object will be tagged with invoice id and save into dictionary.

  3. your dictionary data structure could be like {'invoice-id': csv-object, 'invoice-id2': csv-object2, ...}
  4. write custom function which can reads your csv-object, and gives you name,product-id, qty, etc...

Hope this helps.

Hara
  • 1,467
  • 4
  • 18
  • 35