Skip to content

Commit

Permalink
ENH fmtxt.Table.save_docx(): respect cell width
Browse files Browse the repository at this point in the history
  • Loading branch information
christianbrodbeck committed Oct 17, 2021
1 parent aa3a89c commit 1df673b
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions eelbrain/fmtxt.py
Original file line number Diff line number Diff line change
Expand Up @@ -1737,11 +1737,13 @@ def save_docx(self, path: PathArg = None):
-----
Most style options are not implemented.
"""
from docx import Document
try:
from docx import Document
except ImportError:
raise ImportError("The python-docx package needs to be installed for this function")

if path is None:
path = ui.ask_saveas(
"Save Text File", filetypes=[("Word Document (*.docx)", "*.docx")])
path = ui.ask_saveas("Save Text File", filetypes=[("Word Document (*.docx)", "*.docx")])
if not path:
return
document = Document()
Expand All @@ -1754,10 +1756,13 @@ def save_docx(self, path: PathArg = None):
for row in self.rows:
if isinstance(row, str):
continue
d_row = table.add_row()
doc_row = table.add_row()
i = 0
for cell in row:
d_row.cells[i].text = str(cell)
doc_cell = doc_row.cells[i]
for j in range(i + 1, i + cell.width):
doc_cell.merge(doc_row.cells[j])
doc_cell.text = str(cell)
i += cell.width

document.save(path)
Expand Down

0 comments on commit 1df673b

Please sign in to comment.