Python: Convert Excel to HTML and Vice Versa

2023-12-08 06:29:39 Written by  support iceblue
Rate this item
(0 votes)

If you need to display or interact with the contents of an Excel spreadsheet on a web page, converting Excel to HTML is a good choice. This conversion allows users to view and manipulate the table data directly on the web page without having to download the Excel file, providing a more convenient way to share and display the data. When needed, you can also convert the HTML file back to Excel format for better data editing. In this article, we will show you how to convert Excel to HTML and HTML to Excel in Python by using Spire.XLS for Python.

Install Spire.XLS for Python

This scenario requires Spire.XLS for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.

pip install Spire.XLS

If you are unsure how to install, please refer to this tutorial: How to Install Spire.XLS for Python on Windows

Convert Excel to HTML in Python

Spire.XLS for Python supports converting a specific Excel worksheet to HTML using Worksheet.SaveToHtml() method. Detailed steps are listed below.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Save the worksheet as an HTML file using Worksheet.SaveToHtml() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "C:/Users/Administrator/Desktop/Sample_1.xlsx"
outputFile = "C:/Users/Administrator/Desktop/ToHtml.html"

# Create a Workbook instance
workbook = Workbook()

# Load a sample Excel file
workbook.LoadFromFile(inputFile)

# Get the first sheet of this file
sheet = workbook.Worksheets[0]

# Save the worksheet to HTML
sheet.SaveToHtml(outputFile)
workbook.Dispose()

Python: Convert Excel to HTML and Vice Versa

Convert Excel to HTML with Images Embedded in Python

If the Excel file you want to convert contains images, you can embed the images into the HTML file by setting the ImageEmbedded property to "True". Detailed steps are listed below.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Create an HTMLOptions instance.
  • Set the ImageEmbedded as “True” to embed images to HTML.
  • Save the worksheet as an HTML file using Worksheet.SaveToHtml() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "C:/Users/Administrator/Desktop/Sample_2.xlsx"
outputFile = "C:/Users/Administrator/Desktop/ToHtmlwithImages.html"

# Create a Workbook instance
workbook = Workbook()

# Load a sample Excel file
workbook.LoadFromFile(inputFile)

# Get the first sheet of this file
sheet = workbook.Worksheets[0]

# Create an HTMLOptions instance
options = HTMLOptions()

# Embed images to HTML
options.ImageEmbedded = True

# Save the worksheet to HTML
sheet.SaveToHtml(outputFile, options)
workbook.Dispose()

Python: Convert Excel to HTML and Vice Versa

Convert HTML to Excel in Python

You are also allowed to convert an HTML back to an Excel file by calling the Workbook.SaveToFile() method provided by Spire.XLS for Python. Detailed steps are listed below.

  • Create a Workbook instance.
  • Load an HTML file from disk using Workbook.LoadFromFile() method.
  • Save the HTML file to an Excel file by using Workbook.SaveToFile() method.
  • Python
from spire.xls import *
from spire.xls.common import *

inputFile = "C:/Users/Administrator/Desktop/Sample.html"
outputFile = "C:/Users/Administrator/Desktop/ToExcel.xlsx"

# Create a Workbook instance
workbook = Workbook()

# Load an HTML file from disk
workbook.LoadFromHtml(inputFile)

# Save the HTML file to an Excel file
workbook.SaveToFile(outputFile, ExcelVersion.Version2013)
workbook.Dispose()

Python: Convert Excel to HTML and Vice Versa

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:12