-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconvert_to_utf8.py
More file actions
65 lines (51 loc) · 2.06 KB
/
convert_to_utf8.py
File metadata and controls
65 lines (51 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
"""
Converte qualquer arquivo HTML para UTF-8 (in-place, com backup .bak).
Script usado para converter arquivos de grafo (grafo_final.html) para ser disponibilizado online no Github.
COMO USAR:
1) Abrir o Terminal do Windows
2) Ativar um ambiente virtual que contenha a biblioteca "chardet"
3) Digitar no Terminal:
python <caminho_para>\convert_to_utf8.py "<caminho_para>\grafo_final.html"
EXEMPLO DE SAÍDA NO TERMINAL:
[INFO] Codificação detectada: Windows-1252
[BACKUP] Criado: F:\SINARC\sinarc-main\grafo_final.html.bak
[OK] Arquivo convertido para UTF-8: F:\SINARC\sinarc-main\grafo_final.html
"""
from pathlib import Path
import chardet
import sys
def convert_html_to_utf8(input_path: Path) -> None:
raw_bytes = input_path.read_bytes()
# 1. Detecta a codificação (Windows-1252, ISO-8859-1, etc.)
detected = chardet.detect(raw_bytes)
encoding = detected.get("encoding") or "utf-8"
print(f"[INFO] Codificação detectada: {encoding}")
# 2. Decodifica com a codificação correta
try:
text = raw_bytes.decode(encoding)
except UnicodeDecodeError:
print("[WARN] Falha ao decodificar com", encoding, "→ usando latin1 como fallback")
text = raw_bytes.decode("latin1")
# 3. Força charset=utf-8 no HTML
text = (
text
.replace(f'charset={encoding.lower()}', 'charset=utf-8')
.replace(f'charset={encoding.upper()}', 'charset=utf-8')
)
# 4. Cria backup
backup = input_path.with_suffix(input_path.suffix + ".bak")
backup.write_bytes(raw_bytes)
print(f"[BACKUP] Criado: {backup}")
# 5. Salva em UTF-8
input_path.write_text(text, encoding="utf-8")
print(f"[OK] Arquivo convertido para UTF-8: {input_path}")
# ---- CLI ----
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Uso: python convert_to_utf8.py arquivo.html")
sys.exit(1)
file = Path(sys.argv[1])
if not file.exists():
print("Arquivo não encontrado:", file)
sys.exit(1)
convert_html_to_utf8(file)