Skip to content

Instantly share code, notes, and snippets.

@turicas
Created March 11, 2026 15:30
Show Gist options
  • Select an option

  • Save turicas/ba676ea1403705a6fe3a3f51e1691f4d to your computer and use it in GitHub Desktop.

Select an option

Save turicas/ba676ea1403705a6fe3a3f51e1691f4d to your computer and use it in GitHub Desktop.
Converte XLS para CSV, eliminando linhas e colunas totalmente em branco
# pip install xlrd
import argparse
import csv
from functools import lru_cache
from pathlib import Path
import xlrd
@lru_cache(maxsize=64 * 1024)
def linha_vazia(valores):
return all(valor in ("", None) for valor in valores)
@lru_cache(maxsize=64 * 1024)
def formatar_valor(valor, tipo):
if tipo in (xlrd.XL_CELL_EMPTY, xlrd.XL_CELL_BLANK):
return ""
elif tipo == xlrd.XL_CELL_NUMBER: # Evita escrever "1.0" quando o valor é inteiro
if isinstance(valor, float) and valor.is_integer():
return str(int(valor))
return str(valor)
return str(valor)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("arquivo_xls", type=Path, help="Arquivo XLS a ser convertido")
parser.add_argument("sheet_name", type=str, help="Nome da sheet a ser convertida")
parser.add_argument("arquivo_csv", type=Path, help="Arquivo CSV que será gerado")
args = parser.parse_args()
arquivo_xls = args.arquivo_xls
sheet_name = args.sheet_name
arquivo_csv = args.arquivo_csv
workbook = xlrd.open_workbook(arquivo_xls)
sheet = workbook.sheet_by_name(sheet_name)
# Descobre a última coluna que tem algum conteúdo em qualquer linha
ultima_coluna_util = -1
for row_idx in range(sheet.nrows):
for col_idx in range(sheet.ncols):
cell = sheet.cell(row_idx, col_idx)
if formatar_valor(cell.value, cell.ctype) != "":
if col_idx > ultima_coluna_util:
ultima_coluna_util = col_idx
with arquivo_csv.open(mode="w", encoding="utf-8") as fobj:
writer = csv.writer(fobj)
if ultima_coluna_util >= 0:
for row_idx in range(sheet.nrows):
linha = []
for col_idx in range(ultima_coluna_util + 1):
cell = sheet.cell(row_idx, col_idx)
linha.append(formatar_valor(cell.value, cell.ctype))
if not linha_vazia(tuple(linha)):
writer.writerow(linha)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment