test_odswriter.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from datetime import (
  2. date,
  3. datetime,
  4. )
  5. import re
  6. import pytest
  7. from pandas.compat import is_platform_windows
  8. import pandas as pd
  9. import pandas._testing as tm
  10. from pandas.io.excel import ExcelWriter
  11. odf = pytest.importorskip("odf")
  12. if is_platform_windows():
  13. pytestmark = pytest.mark.single_cpu
  14. @pytest.fixture
  15. def ext():
  16. return ".ods"
  17. def test_write_append_mode_raises(ext):
  18. msg = "Append mode is not supported with odf!"
  19. with tm.ensure_clean(ext) as f:
  20. with pytest.raises(ValueError, match=msg):
  21. ExcelWriter(f, engine="odf", mode="a")
  22. @pytest.mark.parametrize("engine_kwargs", [None, {"kwarg": 1}])
  23. def test_engine_kwargs(ext, engine_kwargs):
  24. # GH 42286
  25. # GH 43445
  26. # test for error: OpenDocumentSpreadsheet does not accept any arguments
  27. with tm.ensure_clean(ext) as f:
  28. if engine_kwargs is not None:
  29. error = re.escape(
  30. "OpenDocumentSpreadsheet() got an unexpected keyword argument 'kwarg'"
  31. )
  32. with pytest.raises(
  33. TypeError,
  34. match=error,
  35. ):
  36. ExcelWriter(f, engine="odf", engine_kwargs=engine_kwargs)
  37. else:
  38. with ExcelWriter(f, engine="odf", engine_kwargs=engine_kwargs) as _:
  39. pass
  40. def test_book_and_sheets_consistent(ext):
  41. # GH#45687 - Ensure sheets is updated if user modifies book
  42. with tm.ensure_clean(ext) as f:
  43. with ExcelWriter(f) as writer:
  44. assert writer.sheets == {}
  45. table = odf.table.Table(name="test_name")
  46. writer.book.spreadsheet.addElement(table)
  47. assert writer.sheets == {"test_name": table}
  48. @pytest.mark.parametrize(
  49. ["value", "cell_value_type", "cell_value_attribute", "cell_value"],
  50. argvalues=[
  51. (True, "boolean", "boolean-value", "true"),
  52. ("test string", "string", "string-value", "test string"),
  53. (1, "float", "value", "1"),
  54. (1.5, "float", "value", "1.5"),
  55. (
  56. datetime(2010, 10, 10, 10, 10, 10),
  57. "date",
  58. "date-value",
  59. "2010-10-10T10:10:10",
  60. ),
  61. (date(2010, 10, 10), "date", "date-value", "2010-10-10"),
  62. ],
  63. )
  64. def test_cell_value_type(ext, value, cell_value_type, cell_value_attribute, cell_value):
  65. # GH#54994 ODS: cell attributes should follow specification
  66. # http://docs.oasis-open.org/office/v1.2/os/OpenDocument-v1.2-os-part1.html#refTable13
  67. from odf.namespaces import OFFICENS
  68. from odf.table import (
  69. TableCell,
  70. TableRow,
  71. )
  72. table_cell_name = TableCell().qname
  73. with tm.ensure_clean(ext) as f:
  74. pd.DataFrame([[value]]).to_excel(f, header=False, index=False)
  75. with pd.ExcelFile(f) as wb:
  76. sheet = wb._reader.get_sheet_by_index(0)
  77. sheet_rows = sheet.getElementsByType(TableRow)
  78. sheet_cells = [
  79. x
  80. for x in sheet_rows[0].childNodes
  81. if hasattr(x, "qname") and x.qname == table_cell_name
  82. ]
  83. cell = sheet_cells[0]
  84. assert cell.attributes.get((OFFICENS, "value-type")) == cell_value_type
  85. assert cell.attributes.get((OFFICENS, cell_value_attribute)) == cell_value