Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic table support #55

Closed
wants to merge 1 commit into from
Closed

Basic table support #55

wants to merge 1 commit into from

Conversation

srepmub
Copy link
Contributor

@srepmub srepmub commented Jul 31, 2020

hiya,

after these changes, an HTML document that I'm testing with comes out pretty nicely.

cheers,
mark.

@srepmub srepmub force-pushed the floeper branch 4 times, most recently from 19752aa to 0dc643e Compare July 31, 2020 12:53
@francoisfreitag
Copy link
Contributor

Hi,

Thanks for your contribution!

For a PR to be accepted, it needs to provide a set of tests to exercise the new code. It would help a lot in explaining the expected data, facilitate experimenting the proposed patch and prevent regressions in future updates.
Could you add some tests?

@srepmub
Copy link
Contributor Author

srepmub commented Jul 31, 2020

hi francois,

thanks for your quick response. I will see if I can add some basic tests.

@srepmub srepmub force-pushed the floeper branch 2 times, most recently from 1272ca8 to 4e9fb3e Compare August 4, 2020 09:46
@srepmub
Copy link
Contributor Author

srepmub commented Aug 4, 2020

okay, added some tests.

@srepmub
Copy link
Contributor Author

srepmub commented Aug 5, 2020

I also added support for internal ilnks/bookmarks.

@srepmub
Copy link
Contributor Author

srepmub commented Aug 5, 2020

fixed indentation for multi-line code tag (and improved the test)

Copy link
Contributor

@francoisfreitag francoisfreitag left a comment

Choose a reason for hiding this comment

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

Thanks for the suggested improvements! I think they all make sense, but would prefer to keep the review focused by splitting separate improvements in separate PRs (i.e. one PR for skip style data and one PR for add line breaks for <code>).
I only reviewed the “Basic table support” in this PR. Could you please open PRs for other improvements?

Additionally, isort changes should have been fixed with 345cf86 and can be dropped.

@francoisfreitag

This comment has been minimized.

@srepmub
Copy link
Contributor Author

srepmub commented Aug 25, 2020

sorry for the late response, I was on vacation. I will look at your comments and submit separate PR's, but it may take some time.. :-)

@srepmub
Copy link
Contributor Author

srepmub commented Oct 6, 2020

please have a look at this version. it adds several things as you requested, and it should also work more or less for nested tables.

if this can be merged (after further changes perhaps), I would be interested in looking into taking layout and color styling from the HTML side. also happy to cleanup the other smaller patches that I sent in earlier.

@srepmub srepmub changed the title Basic table support; skip style data; add line-breaks for code Basic table support Oct 8, 2020
Copy link
Contributor

@francoisfreitag francoisfreitag left a comment

Choose a reason for hiding this comment

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

Hey @srepmub,

Sorry for the delay. This patch is working well in my testing.

It’s sad that the table autofit option does not resize the columns to their optimal width.

Column widths are adjusted in either case if total column width exceeds page width.

We could abuse the documented behavior to automatically size columns by adding huge columns, but your solution is more straightforward.

I think this patch should be good to go once tests are ported to the existing test_html2docx.

col += 1
self.tables[-1] = (table, row, col)
if col >= len(table.columns):
table.add_column(0)
Copy link
Contributor

Choose a reason for hiding this comment

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

A comment explaining that columns must have a width to form a valid docx would be very helpful. It could also explain that the column width is computed in finish_table, because the total column count is needed to evenly spread the width.



def test_table():
html_path = os.path.join(TEST_DIR, "table.html")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can test_html2docx be extended to accept tables in the spec?

The specs would need to be updated to specify paragraphs and tables in a document.
Since table cells can contain other tables or multiple paragraphs, we could recursively validate paragraphs and tables in a container (for now, a document or table cell).

@@ -78,6 +78,8 @@ def _reset(self) -> None:

# Formatting options
self.pre = False
self.table_cell: Optional[Any] = None
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.table_cell: Optional[Any] = None
self.table_cell: Optional[_Cell] = None

@@ -78,6 +78,8 @@ def _reset(self) -> None:

# Formatting options
self.pre = False
self.table_cell: Optional[Any] = None
self.tables: List[Tuple[Any, int, int]] = []
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
self.tables: List[Tuple[Any, int, int]] = []
self.tables: List[Table[Any, int, int]] = []

Comment on lines +107 to +110
if self.table_cell is not None:
table = self.table_cell.add_table(rows=0, cols=0)
else:
table = self.doc.add_table(rows=0, cols=0)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if self.table_cell is not None:
table = self.table_cell.add_table(rows=0, cols=0)
else:
table = self.doc.add_table(rows=0, cols=0)
container = self.doc if self.table_cell is None else self.table_cell
table = container.add_table(rows=0, cols=0)

@@ -78,6 +78,8 @@ def _reset(self) -> None:

# Formatting options
self.pre = False
self.table_cell: Optional[Any] = None
self.tables: List[Tuple[Any, int, int]] = []
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the table is only appended to, we could only keep track of the column and always use the last row. NBD either way.

@robinchow
Copy link

Any chance this can be merged soon? I would super appreciate this feature!

@francoisfreitag
Copy link
Contributor

It seems this PR has stalled. Feel free to fork it and carry on!

@kbattersby
Copy link
Contributor

Closing because there has been no activity on this PR in over a year.

@kbattersby kbattersby closed this Jan 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants