Microsoft® Conversion du format Excel via Python

Importez et exportez des fichiers Excel aux formats tableur, Web, image et mise en page fixe

 

La bibliothèque Excel Python accélère les processus de programmation et de conversion des feuilles de calcul tout en prenant en charge les formats populaires, notamment XLS, XLSX, XLSM, XLSB, XLTX, XLTM, CSV, SpreadsheetML, ODS. Il permet également d’exporter des fichiers Excel vers PDF, XPS, HTML, MHTML, Plain Formats de texte et d’image populaires tels que TIFF, JPG, PNG, BMP et SVG.

Convertir Excel en XLSX, ODS, SXC et FODS

L’interconversion du format de feuille de calcul nécessite uniquement le chargement d’une feuille de calcul avec une instance de Cahier d’exercices et sauvegarder dans le format souhaité tout en sélectionnant la valeur appropriée dans EnregistrerFormat énumération.

Python Code pour la conversion du format de fichier Excel
// 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.sxc", SaveFormat.SXC);
workbook.save("output.fods", SaveFormat.FODS);
 

Convertir Excel en PDF, XPS, HTML et MD

Des cours spécialisés sont disponibles pour contrôler le processus de conversion pour des formats de sortie spécifiques tels que Options d’enregistrement PDF pour exporter des fichiers Excel sous le numéro PDF, XpsSaveOptions pour la conversion Excel en XPS, HtmlSaveOptions pour afficher Excel sous la forme HTML et MarkdownSaveOptions pour la conversion Excel vers Markdown.

Python Code pour Excel aux formats PDF et Web
// 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);
 

Convertir JSON en Excel et Excel en JSON

Les développeurs Python peuvent facilement charger et convertir des fichiers JSON en Excel en quelques lignes de code seulement. De même, les données Excel peuvent être exportées vers les données JSON.

Python Code pour la conversion JSON en Excel
//Load your source json file
workbook = Workbook("Data.json")
//save file to xlsx format
workbook.save("output.xlsx")
Code Python pour la conversion Excel vers JSON
//Load your source xlsx file
workbook = Workbook("input.xlsx")
//save file to json format
workbook.save("Data.json")
 

Convertir des feuilles de calcul Excel en JPG, BMP, PNG et GIF

Chaque feuille de calcul d’un fichier Excel peut être convertie en différents formats d’image, appelez Options ImageOuImpression .setImageFormat pour définir le format de l’image.

Python Code pour la conversion d\'Excel en image
// 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")
 

Convertir Excel en Word & PowerPoint

Il est possible de charger n’importe quelle feuille de calcul et de la convertir en fichiers Word DOCX et PowerPoint PPTX tout en utilisant DocxSaveOptions & PptxSaveOptions cours comme démontré ci-dessous.

Code Python pour la conversion Excel vers Word et PowerPoint
// 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)