Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ NEXT RELEASE
TBD
-------------------

* Minor nitpick to remove unused line (`#559`_ by `@KyleKing`_)
* Minor nitpick to remove unused line (`#562`_ by `@KyleKing`_)
* Utilize openpyxl for xlsx after xlrd dropped support (`#559`_ by `@KyleKing`_)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[dust] be sure to add links to the github issues and your username at the bottom of this file

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, I wasn't sure what this syntax was for and was just copying it. Would have eventually noticed when getting around to fixing RTD!


2.0.0
-------------------
Expand Down
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ dependencies = [
"extract-msg>=0.40.0",
"pdfminer.six>=20221105",
"python-pptx>=0.6.18",
"xlrd>=1.2.0,<2.0.0", # v2 drops support for xlsx. Requires openpyxl or alternative to support xlsx if upgrading xlrd. See: https://github.com/deanmalmgren/textract/pull/543#issuecomment-2684619988
"openpyxl>=3.0.0", # Replaces xlrd for Excel file support (xlrd 2.0+ dropped xlsx support)
"xlrd>=2.0.0", # Supports legacy .xls files (openpyxl does not support .xls)
]

[project.optional-dependencies]
Expand Down
29 changes: 27 additions & 2 deletions textract/parsers/xls_parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
from .xlsx_parser import Parser
import xlrd

__all__ = ["Parser"]
from .utils import BaseParser


class Parser(BaseParser):
"""Extract text from legacy Excel files (.xls)."""

def extract(self, filename, **kwargs):
workbook = xlrd.open_workbook(filename)
sheets_name = workbook.sheet_names()
output = "\n"
for names in sheets_name:
worksheet = workbook.sheet_by_name(names)
num_rows = worksheet.nrows
num_cells = worksheet.ncols

for curr_row in range(num_rows):
new_output = []
for index_col in range(num_cells):
value = worksheet.cell_value(curr_row, index_col)
if value:
if isinstance(value, (int, float)):
value = str(value)
new_output.append(value)
if new_output:
output += " ".join(new_output) + "\n"
return output
39 changes: 21 additions & 18 deletions textract/parsers/xlsx_parser.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
import xlrd
import openpyxl

from .utils import BaseParser


class Parser(BaseParser):
"""Extract text from Excel files (.xls/xlsx)."""
"""Extract text from Excel files (.xlsx)."""

def extract(self, filename, **kwargs):
workbook = xlrd.open_workbook(filename)
sheets_name = workbook.sheet_names()
workbook = openpyxl.load_workbook(filename, data_only=True)
output = "\n"
for names in sheets_name:
worksheet = workbook.sheet_by_name(names)
num_rows = worksheet.nrows
num_cells = worksheet.ncols
for sheet_name in workbook.sheetnames:
worksheet = workbook[sheet_name]

for row in worksheet.iter_rows(values_only=True):
non_empty_values = []
for value in row:
if value is None or value is False:
continue
if isinstance(value, bool):
value = "1"
elif isinstance(value, (int, float)):
# Convert to float to preserve decimal format (e.g., 83 -> 83.0)
value = str(float(value))
else:
value = str(value)
non_empty_values.append(value)
if non_empty_values:
output += " ".join(non_empty_values) + "\n"

for curr_row in range(num_rows):
new_output = []
for index_col in range(num_cells):
value = worksheet.cell_value(curr_row, index_col)
if value:
if isinstance(value, (int, float)):
value = str(value)
new_output.append(value)
if new_output:
output += " ".join(new_output) + "\n"
return output
31 changes: 27 additions & 4 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading