Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aspose-com-gists/0c21e6a7d6169ea84e1f5cc5fb527aa2 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/0c21e6a7d6169ea84e1f5cc5fb527aa2 to your computer and use it in GitHub Desktop.
Convert HTML to Excel in Python
from aspose.cells import Workbook, HtmlLoadOptions
# Step 1: Set HTML load options
load_options = HtmlLoadOptions()
load_options.auto_fit_cols_and_rows = True # Automatically adjusts columns and rows
# Step 2: Load HTML with options
workbook = Workbook("sample.html", load_options)
# Step 3: Save as Excel
workbook.save("table_advanced.xlsx")
from aspose.cells import Workbook
workbook = Workbook("sample.html")
# Save to XLS format
workbook.save("output.xls")
# Save to CSV format
workbook.save("output.csv")
# Save to PDF for reporting
workbook.save("output.pdf")
from aspose.cells import Workbook, HtmlLoadOptions
from io import BytesIO
# Step 1: Define HTML string
html_data = """
<table border='1'>
<tr><th>Product</th><th>Price</th><th>Quantity</th></tr>
<tr><td>Laptop</td><td>800</td><td>5</td></tr>
<tr><td>Phone</td><td>400</td><td>10</td></tr>
</table>
"""
# Step 2: Convert HTML string to bytes
html_bytes = BytesIO(html_data.encode('utf-8'))
# Step 3: Load HTML from memory
options = HtmlLoadOptions()
workbook = Workbook(html_bytes, options)
# Step 4: Save as Excel
workbook.save("from_string.xlsx")
from aspose.cells import Workbook
# Step 1: Define the input HTML file path
input_file = "sample.html"
# Step 2: Create a Workbook object and load the HTML
workbook = Workbook(input_file)
# Step 3: Save the file as Excel
workbook.save("output.xlsx")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment