Skip to content

Instantly share code, notes, and snippets.

@horvatha
Last active November 7, 2024 19:57
Show Gist options
  • Save horvatha/0ac42748ad91118a8cf0dd7b7bb88934 to your computer and use it in GitHub Desktop.
Save horvatha/0ac42748ad91118a8cf0dd7b7bb88934 to your computer and use it in GitHub Desktop.
from openpyxl import Workbook
text = "￿" # Including three bytes: hexadecimal FFBFBF resulting ValueError
# text = '\x16' # resulting IllegalCharacterError
wb = Workbook()
ws = wb.active
ws.append([text])
wb.save("test.xlsx")
"""FFBFBF 3 bytes in text prevents excel to be saved"""
from io import BytesIO
import platform
import sys
import pandas as pd
print(pd.__version__)
print(platform.platform())
print(sys.version)
"""
pandas version: 1.5.3
Windows-10-10.0.22631-SP0
Python 3.11.0 (main, Oct 24 2022, 18:26:48) [MSC v.1933 64 bit (AMD64)]
and
openpyxl 3.1.2
pandas 2.2.1
Linux-6.11.5-200.fc40.x86_64-x86_64-with-glibc2.39 (Fedora)
Python 3.12.7 (main, Oct 1 2024, 00:00:00) [GCC 14.2.1 20240912 (Red Hat 14.2.1-3)]
"""
df = pd.DataFrame(data=dict(a=["text 1", "text￿2"]))
output = BytesIO()
writer = pd.ExcelWriter(output, engine='openpyxl')
df.to_excel(writer, index=False, sheet_name="first_sheet")
writer.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment