Python : Fichiers Excel

De Justine's wiki
Aller à la navigation Aller à la recherche

OpenPyXl

Cette lib permet de lire les fichiers excel assez facilement.

<source lang="python">

  1. !/usr/bin/env python3
  2. coding: utf-8

import openpyxl

  1. Charge le chemin du fichier -_-'

excelfile = Path('.', 'triage-lvg.xlsx')

  1. Charge le workbook (assemblage de worksheets, un fichier excel ayant plusieurs pages)

wb_obj = openpyxl.load_workbook(excelfile)

  1. Charge la sheet

sheet = wb_obj.active

  1. Affiche le nom de la sheet

print(sheet)

  1. Afficher le contenu

for row in sheet.iter_rows(max_row=sheet.max_row):

   for cell in row:
       print(cell.value, end=" ")
   print()

  1. Fichier excel vers dictionnaire

content = {} i = 0

for row in sheet.iter_rows(max_row=sheet.max_row):

   content[i] = []
   for cell in row:
       content[i].append(cell.value)
   i += 1

for j in content.keys():

   print(content[j])

</source>