diff --git a/pandas/formats/excel.py b/pandas/formats/excel.py index 3de9a66f988b9..bd9da74965eef 100644 --- a/pandas/formats/excel.py +++ b/pandas/formats/excel.py @@ -209,19 +209,26 @@ def build_font(self, props): assert size.endswith('pt') size = float(size[:-2]) - # TODO: - # re.search(r'''(?x) - # ( - # "(?:[^"]|\\")+" - # | - # '(?:[^']|\\')+' - # | - # [^'"]+ - # )(?=,|\s*$) - # ''') - font_names = [name.strip() - for name in props.get('font-family', '').split(',') - if name.strip()] + font_names_tmp = re.findall(r'''(?x) + ( + "(?:[^"]|\\")+" + | + '(?:[^']|\\')+' + | + [^'",]+ + )(?=,|\s*$) + ''', props.get('font-family', '')) + font_names = [] + for name in font_names_tmp: + if name[:1] == '"': + name = name[1:-1].replace('\\"', '"') + elif name[:1] == '\'': + name = name[1:-1].replace('\\\'', '\'') + else: + name = name.strip() + if name: + font_names.append(name) + family = None for name in font_names: if name == 'serif': diff --git a/pandas/tests/formats/test_to_excel.py b/pandas/tests/formats/test_to_excel.py index 5c99b7a3784e4..ff345de4c3456 100644 --- a/pandas/tests/formats/test_to_excel.py +++ b/pandas/tests/formats/test_to_excel.py @@ -12,13 +12,17 @@ # FONT # - name ('font-family: foo,bar', {'font': {'name': 'foo'}}), - pytest.mark.xfail(('font-family: "foo bar",baz', - {'font': {'name': 'foo bar'}})), + ('font-family: "foo bar",baz', {'font': {'name': 'foo bar'}}), ('font-family: foo,\nbar', {'font': {'name': 'foo'}}), ('font-family: foo, bar, baz', {'font': {'name': 'foo'}}), ('font-family: bar, foo', {'font': {'name': 'bar'}}), + ('font-family: \'foo bar\', baz', {'font': {'name': 'foo bar'}}), + ('font-family: \'foo \\\'bar\', baz', {'font': {'name': 'foo \'bar'}}), + ('font-family: "foo \\"bar", baz', {'font': {'name': 'foo "bar'}}), + ('font-family: "foo ,bar", baz', {'font': {'name': 'foo ,bar'}}), # - family ('font-family: serif', {'font': {'name': 'serif', 'family': 1}}), + ('font-family: Serif', {'font': {'name': 'serif', 'family': 1}}), ('font-family: roman, serif', {'font': {'name': 'roman', 'family': 1}}), ('font-family: roman, sans-serif', {'font': {'name': 'roman', 'family': 2}}),