Skip to content

feat: Add support for encoding parameter on read_excel #23448

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def read_excel(io,
skipfooter=0,
convert_float=True,
**kwds):

encoding = kwds.get("encoding")
# Can't use _deprecate_kwarg since sheetname=None has a special meaning
if is_integer(sheet_name) and sheet_name == 0 and 'sheetname' in kwds:
warnings.warn("The `sheetname` keyword is deprecated, use "
Expand All @@ -315,7 +315,7 @@ def read_excel(io,
"`sheet`")

if not isinstance(io, ExcelFile):
io = ExcelFile(io, engine=engine)
io = ExcelFile(io, engine=engine, encoding=encoding)

return io.parse(
sheet_name=sheet_name,
Expand Down Expand Up @@ -358,7 +358,7 @@ class ExcelFile(object):
def __init__(self, io, **kwds):

err_msg = "Install xlrd >= 0.9.0 for Excel support"

encoding = kwds.get("encoding")
try:
import xlrd
except ImportError:
Expand Down Expand Up @@ -400,9 +400,15 @@ def __init__(self, io, **kwds):
pass

data = io.read()
self.book = xlrd.open_workbook(file_contents=data)
if encoding:
self.book = xlrd.open_workbook(file_contents=data, encoding_override=encoding)
else:
self.book = xlrd.open_workbook(file_contents=data)
elif isinstance(self._io, compat.string_types):
self.book = xlrd.open_workbook(self._io)
if encoding:
self.book = xlrd.open_workbook(self._io, encoding_override=encoding)
else:
self.book = xlrd.open_workbook(self._io)
else:
raise ValueError('Must explicitly set engine if not passing in'
' buffer or path for io.')
Expand Down