Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
openpyxl
openpyxl
Commits
6613a2adfd10
Commit
d59c919a
authored
Nov 21, 2021
by
Anthony Hayward
Browse files
Add information to the ValueError when loading xlsx with invalid stylesheet
--HG-- branch : 3.2
parent
60f67ca114e3
Pipeline
#29342
passed with stage
in 2 minutes and 46 seconds
Changes
6
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
AUTHORS.rst
View file @
6613a2ad
...
...
@@ -15,6 +15,7 @@ Thanks to all those who participate in the project (in alphabetical order):
* Alexandre Fayolle
* Amin Mirzaee
* Anders Chrigstrom
* Anthony Hayward
* Bernt R. Brenna
* Brent Hoover
* Brice Gelineau
...
...
openpyxl/descriptors/base.py
View file @
6613a2ad
...
...
@@ -114,7 +114,7 @@ class MinMax(Min, Max):
class
Set
(
Descriptor
):
"""Value can only be from a set of know values"""
"""Value can only be from a set of know
n
values"""
def
__init__
(
self
,
name
=
None
,
**
kw
):
if
not
'values'
in
kw
:
...
...
@@ -125,7 +125,11 @@ class Set(Descriptor):
def
__set__
(
self
,
instance
,
value
):
if
value
not
in
self
.
values
:
raise
ValueError
(
self
.
__doc__
)
message
=
(
"Value for '{0}' must be one of {1}, but '{2}' was found"
.
format
(
self
.
name
,
self
.
values
,
value
)
if
self
.
name
else
"Value must be one of {0}, but '{1}' was found"
.
format
(
self
.
values
,
value
)
)
raise
ValueError
(
message
)
super
(
Set
,
self
).
__set__
(
instance
,
value
)
...
...
@@ -250,7 +254,7 @@ class MatchPattern(Descriptor):
if
((
self
.
allow_none
and
value
is
not
None
)
or
not
self
.
allow_none
):
if
not
self
.
test_pattern
.
match
(
value
):
raise
ValueError
(
'Value does not match pattern {
0
}'
.
format
(
self
.
pattern
))
raise
ValueError
(
'Value
"{0}"
does not match pattern {
1
}'
.
format
(
value
,
self
.
pattern
))
super
(
MatchPattern
,
self
).
__set__
(
instance
,
value
)
...
...
openpyxl/reader/excel.py
View file @
6613a2ad
...
...
@@ -121,7 +121,7 @@ class ExcelReader:
Read an Excel package and dispatch the contents to the relevant modules
"""
def
__init__
(
self
,
fn
,
read_only
=
False
,
keep_vba
=
KEEP_VBA
,
def
__init__
(
self
,
fn
,
read_only
=
False
,
keep_vba
=
KEEP_VBA
,
data_only
=
False
,
keep_links
=
True
):
self
.
archive
=
_validate_archive
(
fn
)
self
.
valid_files
=
self
.
archive
.
namelist
()
...
...
@@ -278,16 +278,19 @@ class ExcelReader:
def
read
(
self
):
self
.
read_manifest
()
self
.
read_strings
()
self
.
read_workbook
()
self
.
read_properties
()
self
.
read_theme
()
apply_stylesheet
(
self
.
archive
,
self
.
wb
)
self
.
read_worksheets
()
self
.
parser
.
assign_names
()
if
not
self
.
read_only
:
self
.
archive
.
close
()
try
:
self
.
read_manifest
()
self
.
read_strings
()
self
.
read_workbook
()
self
.
read_properties
()
self
.
read_theme
()
apply_stylesheet
(
self
.
archive
,
self
.
wb
)
self
.
read_worksheets
()
self
.
parser
.
assign_names
()
if
not
self
.
read_only
:
self
.
archive
.
close
()
except
ValueError
as
e
:
raise
ValueError
(
"Could not load Excel file {0}"
.
format
(
self
.
archive
.
filename
))
from
e
def
load_workbook
(
filename
,
read_only
=
False
,
keep_vba
=
KEEP_VBA
,
...
...
openpyxl/styles/stylesheet.py
View file @
6613a2ad
...
...
@@ -100,7 +100,10 @@ class Stylesheet(Serialisable):
attrs
=
dict
(
node
.
attrib
)
for
k
in
attrs
:
del
node
.
attrib
[
k
]
return
super
(
Stylesheet
,
cls
).
from_tree
(
node
)
try
:
return
super
(
Stylesheet
,
cls
).
from_tree
(
node
)
except
ValueError
as
e
:
raise
ValueError
(
"Invalid stylesheet XML"
)
from
e
def
_merge_named_styles
(
self
):
...
...
openpyxl/styles/tests/test_fonts.py
View file @
6613a2ad
...
...
@@ -74,3 +74,17 @@ class TestFont:
xml
=
fromstring
(
src
)
ft
=
Font
.
from_tree
(
xml
)
assert
ft
==
Font
(
bold
=
True
,
underline
=
"single"
)
def
test_u_val_invalid
(
self
,
Font
):
src
=
"""
<font xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<b />
<u val="an invalid value" />
<vertAlign />
</font>
"""
xml
=
fromstring
(
src
)
pattern
=
"[^a-zA-Z]u[^a-zA-Z].*an invalid value"
with
pytest
.
raises
(
ValueError
,
match
=
pattern
):
Font
.
from_tree
(
xml
)
openpyxl/styles/tests/test_stylesheet.py
View file @
6613a2ad
...
...
@@ -247,6 +247,29 @@ class TestStylesheet:
assert
stylesheet
.
cellStyles
.
cellStyle
[
-
1
].
xfId
<
stylesheet
.
cellStyleXfs
.
count
def
test_invalid_font_u_style
(
self
,
datadir
,
Stylesheet
):
node
=
fromstring
(
r
"""
<styleSheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<numFmts count="0" />
<fonts count="1">
<font>
<name val="Calibri"></name>
<family val="2"></family>
<color theme="1"></color>
<sz val="11"></sz>
<u val="bad value"/>
<scheme val="minor"></scheme>
</font>
</fonts>
<fills count="0" />
<borders count="0" />
</styleSheet>
"""
)
with
pytest
.
raises
(
ValueError
):
Stylesheet
.
from_tree
(
node
)
def
test_no_stylesheet
():
from
..stylesheet
import
apply_stylesheet
wb1
=
wb2
=
Workbook
()
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment