Microsoft® Excel Format Conversion via Python

Import & export Excel files as spreadsheet, web, image and fixed-layout formats

 

Python Excel Library speeds up spreadsheet programming and conversion processes while supporting popular formats including XLS, XLSX, XLSM, XLSB, XLTX, XLTM, CSV, SpreadsheetML, ODS. It also allows to export Excel files to PDF, XPS, HTML, MHTML, Plain Text and popular image formats such as TIFF, JPG, PNG, BMP and SVG.

Convert Excel to XLSX, ODS, SXC & FODS

Inter-conversion of spreadsheet format only requires loading a spreadsheet with an instance of Workbook and saving back in the desired format while selecting appropriate value from SaveFormat enumeration.

Python Code for Excel File Format Conversion
// load the template file
workbook = Workbook("Book1.xls")
  
// save as XLSX, ODS, SXC & FODS formats
workbook.save("output.xlsx", SaveFormat.XLSX);
workbook.save("output.ods", SaveFormat.ODS);
workbook.save("output.scx", SaveFormat.SXC);
workbook.save("output.fods", SaveFormat.FODS);
 

Convert Excel to PDF, XPS, HTML & MD

Specialized classes are available to control the conversion process for specific output formats such as PdfSaveOptions to export Excel files as PDF, XpsSaveOptions for Excel to XPS conversion, HtmlSaveOptions to render Excel as HTML and MarkdownSaveOptions for Excel to Markdown conversion.

Python Code for Excel to PDF and Web Formats
// load template Excel file from disc
book = Workbook("template.xlsx")

// save Excel in PDF_A_1_B format
pdfOptions = PdfSaveOptions()
pdfOptions.setCompliance(PdfCompliance.PDF_A_1_B)
book.save("output.pdf", pdfOptions);

// save Excel in XPS with 1 page per worksheet
xpsOptions = XpsSaveOptions()
xpsOptions.setOnePagePerSheet(True)
book.save("output.xps", xpsOptions);

// save Excel in HTML with images as Base64
htmlOptions = HtmlSaveOptions()
htmlOptions.setExportImagesAsBase64(True)
book.save("output.html", htmlOptions);

// save Excel in Markdown (MD) while retaining cell formatting
mdOptions = MarkdownSaveOptions()
mdOptions.setFormatStrategy(CellValueFormatStrategy.CELL_STYLE)
book.save("output.md", mdOptions);
 

Convert JSON to Excel & Excel to JSON

Python developers can easily load & convert JSON files to Excel in just a few lines of code. Similarly, Excel data can be exported to JSON data.

Python Code for JSON to Excel Conversion
//Load your source json file
workbook = Workbook("Data.json")
//save file to xlsx format
workbook.save("output.xlsx")
Python Code for Excel to JSON Conversion
//Load your source xlsx file
workbook = Workbook("input.xlsx")
//save file to json format
workbook.save("Data.json")
 

Convert Excel Worksheets to JPG, BMP, PNG & GIF

Each worksheet of an Excel file can be converted to different image formats, call ImageOrPrintOptions .setImageFormat to set the image format.

Python Code for Excel to Image Conversion
// load template spreadsheet
workbook = Workbook("template.xlsx")
// create & set an instance of ImageOrPrintOptions
options = ImageOrPrintOptions()
// set output image format
options.setImageFormat(ImageFormat.getPng())
// create SheetRender for first worksheet in the collection
sheet = workbook.getWorksheets().get(0)
sr = SheetRender(sheet, options)
// render worksheet to image
sr.toImage(0, "output.jpg")
 

Convert Excel to Word & PowerPoint

It is possible to load any spreadsheet and convert it to Word DOCX & PowerPoint PPTX files while using DocxSaveOptions & PptxSaveOptions classes as demonstrated below.

Python code for Excel to Word & PowerPoint Conversion
// load the template file
workbook = Workbook("template.xlsx")

// save spreadsheet as DOCX
docxOptions = DocxSaveOptions()
workbook.save("output.docx", docxOptions)

// save spreadsheet as PPTX
pptxOptions = PptxSaveOptions()
workbook.save("output.pptx", pptxOptions)