Convert CSV to a HTML table format and store in a HTML file

后端 未结 3 1468
难免孤独
难免孤独 2020-12-22 06:08

I have tried few shell scripts to convert to a HTML table format but I didnt get desired output. Could anyone help here to convert CSV to a HTML table format using Python or

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-22 06:35

    This is easy with PowerShell. You can use the ConvertFrom-Csv cmdlet to convert your csv to a object array and use the ConvertTo-Html cmdlet to convert it to a html table. To store the html, use the Set-Content cmdlet:

    $myCsv = 
    @'
    Id, Name
    1, Hello
    2, World
    '@
    
    $myCsv | ConvertFrom-Csv | ConvertTo-Html | Set-Content -Path 'YOUR_PATH_HERE.csv'
    

    Output:

    
    
    
    HTML TABLE
    
    
    IdName
    1Hello
    2World

    Note: If you need to load the csv from a file, you can either use the Get-Content cmdlet to load it and convert it using the example above, or you can use the Import-Csv cmdlet.

提交回复
热议问题