Hi i am trying to convert .xlsx file to .csv file and then read this csv file in python. Below is my code in VBS:
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
This code is working properly. but when i read converted csv file in python the numbers get converted to string. example:
$ 266,74245.4545 -> '$266,74245'
Is there any way i can make changes in vbs code to get these numbers as it is in csv file. for my application i require whole number while reading.
I took above code from "https://stackoverflow.com/questions/1858195/convert-xls-to-csv-on-command-line"
I found solution to this question. I'm changing the currency($) format to a Numbers format in the opened worksheet, and then converting it to CSV.
This how the code looks:
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.ActiveSheet.Range(WScript.Arguments.Item(2)).Select
oExcel.Selection.NumberFormat = "0000000000.00000"
oBook.SaveAs WScript.Arguments.Item(1), 6
Related
I was asked to look for a procedure that convert an Excel file into a CSV one, however I have no knowledge on .bat. I've seen some people doing so with some commands and I would like to know if I can do the same but important: skipping the first five rows of the file.
I would appreciate any help you give me.
A very easy way, is to create a vbs script. Paste the below into a notepad and save as XlsToCsv.vbs. Make sure you give the full path of sourcexlsFile and also destinationcsvfile
To use: XlsToCsv.vbs [sourcexlsFile].xls [destinationcsvfile].csv
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.Sheets(1).Range("1:5").EntireRow.Delete 'this removes the first five rows everytime
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
i want to convert my XLXS file to CSV UTF-8 format using vb script or macros.
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"enter code here
The above script works fine for normal formats.
Please help me in converting in into UTF-8 format
i have also tries the below ,code but it converts into junk characters
Public Sub convert_UnicodeToUTF8()
Dim parF1, parF2 As String
parF1 = "C:\shrangi\SX_Hospital.xlsx"
parF2 = "C:\shrangi\SX_Hospital.csv"
Const adSaveCreateOverWrite = 2
Const adTypeText = 2
Dim streamSrc, streamDst ' Source / Destination
Set streamSrc = CreateObject("ADODB.Stream")
Set streamDst = CreateObject("ADODB.Stream")
streamDst.Type = adTypeText
streamDst.Charset = "UTF-8"
streamDst.Open
With streamSrc
.Type = adTypeText
.Charset = "UTF-8"
.Open
.LoadFromFile parF1
.copyTo streamDst
.Close
End With
streamDst.SaveToFile parF2, adSaveCreateOverWrite
streamDst.Close
Set streamSrc = Nothing
Set streamDst = Nothing
End Sub
Simply:
ActiveWorkbook.SaveAs Filename:="C:\yourPath\yourFileName.csv", FileFormat:=xlCSVUTF8
More Info:
MSDN: Workbook.SaveAs Method
Since you are converting an external file to an external file, you don't need to do it within Excel with VBA. That opens up some possibilities. With the OpenXML SDK you don't even need Excel.
OpenXML SDK is a bit hard to use so there are a few wrappers for it to optimize Workbook programming. EPPlus has a PowerShell wrapper around it called PSExcel. It makes this task really easy in PowerShell
One-time setup, typically as an Administrator:
Install-Module PSExcel
Once per PowerShell session:
Import-Module PSExcel
Then:
Import-XLSX 'C:\shrangi\SX_Hospital.xlsx' | Export-CSV 'C:\shrangi\SX_Hospital.csv' -Encoding UTF8
For a simple workbook, that's all you need.
Side note on CSV: Converting from xlsx to csv throws out almost all the metadata and introduces the need for more metadata. Along with the file, you need to communicate the character encoding, the data types of each column, whether there is a header row, the line terminator, the field separator (not always comma), the culture-specific numeric formatting, the quote character (aka "text qualifier"), and the quote character escape mechanism. You can see all of these question that Excel has to ask when you use its text import wizard.
I have VBscript code which opens XLS file and export it to CSV, see below:
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open("test.xls")
oBook.SaveAs "test.csv", 6
oBook.Close False
oExcel.Quit
It works fine but I need to do the same - convert XLS to CSV with already opened file "test.xls" on my computer which is different (edited by me) versus saved version. Is that possible?
Yes, you just need to connect the existing instance of Excel and set workbook object variable equal to the already open workbook instead:
Set oExcel = GetObject(, "Excel.Application")
Set oBook = oExcel.Workbooks("test.xls")
I am brand new to VBScripts and trying to use a script I found on SO. The script will convert an XLSX file to a CSV. That is working great but the CSV has empty rows and it is causing me some headaches.
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
oBook.Close False
oExcel.Quit
How can I modify the above script to remove empty rows before saving as CSV?
I read this would do it but it does not seem to work and throws a runtime error
oBook.UsedRange
I am using the following VBS script found on stackoverflow to convert xls to csv. It works fine. I want to run it with the batch file at the bottom. I don't know how to achieve what I want. The batch file gives the csv file the same name as the xls file. Because the xls file has two worksheets in it I need to produce two csv's for each xls file
if WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.SaveAs WScript.Arguments.Item(1), 6
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(1))
oBook.SaveAs WScript.Arguments.Item(2), 6
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
Here's the batch file
FOR /f "delims=" %%i IN ('DIR *.xlsx /b') DO ExcelToCSV.vbs "%%i" "%%i.csv"
I need to pass in 2 output .csv file names one should be nnnnn_1.csv the other should be nnnnn_2.csv to account for the 2 worksheets in the xls files.
Thanks for any help
Similarly to Scott's answer above, I would probably change the script to work this way, since this change will just work for all sheets in a given workbook and output each to a .csv file without worrying if there are 1, 2 or 10 sheets.
If WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
Dim oNewBook
shIndex = 1 ' start with 1
For shIndex = 1 To oBook.Worksheets.Count
oBook.Worksheets(shIndex).Copy
Set oNewBook = oExcel.ActiveWorkbook
oNewBook.SaveAs Replace(WScript.Arguments.Item(1),".csv","_" & shIndex & ".csv"), 6
oNewBook.Close False
Next
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
This script will save every worksheet in the workbook as [cvs base name] - [worksheet name].csv.
If WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
WScript.Quit
End If
Dim BaseName, oExcel, oBook, xlWorksheet, i
BaseName = Replace( WScript.Arguments.Item(0), ".cvs", "")
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open(WScript.Arguments.Item(0))
For Each xlWorksheet In oBook.Worksheets
xlWorksheet.Copy
oExcel.ActiveWorkbook.SaveAs BaseName & " - " & xlWorksheet.Name & ".csv"
oExcel.ActiveWorkbook.Close False
Next
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
If you change the VBS script to this it should work:
If WScript.Arguments.Count < 2 Then
WScript.Echo "Error! Please specify the source path and the destination. Usage: XlsToCsv SourcePath.xls Destination.csv"
Wscript.Quit
End If
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
Dim oBook
Set oBook = oExcel.Workbooks.Open(Wscript.Arguments.Item(0))
oBook.Worksheets(1).Copy
Dim oBookNew1
Set oBookNew1 = oExcel.ActiveWorkbook
oBookNew1.SaveAs Replace(WScript.Arguments.Item(1),".csv","_1.csv"), 6
oBookNew1.Close False
oBook.Worksheets(2).Copy
Dim oBookNew1
Set oBookNew1 = oExcel.ActiveWorkbook
oBookNew2.SaveAs Replace(WScript.Arguments.Item(1),".csv","_2.csv"), 6
oBookNew2.Close False
oBook.Close False
oExcel.Quit
WScript.Echo "Done"
I have written an alternative solution in Python that exports the present excel sheets of a workbook in CVS format.
You can find it here
Best,
Julian