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

Add passing style_map kwarg to Mammoth when converting docx to allow keeping comments #38

Merged
merged 3 commits into from
Dec 16, 2024

Conversation

VillePuuska
Copy link
Contributor

Mammoth, which is used for converting docx, allows specifying custom mappings for docx styles => html elements; see https://pypi.org/project/mammoth/#custom-style-map By default, Mammoth ignores comments in docx files. These custom style maps allow including comments in the generated html; see https://pypi.org/project/mammoth/#comments

This PR adds passing this style_map kwarg to Mammoth when converting a docx file. Tests are also added for specifically converting comments using a style map.

For example, using style_map="comment-reference => ", the two comments added to the test file are added to the converted markdown as follows:

... build LLM applications[[1]](#comment-0) via ...
| 1 | 2 | 3 | 4 | 5 | 6 |
| --- | --- | --- | --- | --- | --- |
| 7 | 8 | 9 | 10 | 11 | 12 |
| 13 | 14 | 49e168b7-d2ae-407f-a055-2167576f39a1 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23[[2]](#comment-1) |
| 24 | 25 | 26 | 27 | 28 | 29 |

Comment [1]

This is a test comment. 12df-321a [↑](#comment-ref-0)

Comment [2]

Yet another comment in the doc. 55yiyi-asd09 [↑](#comment-ref-1)

Note: The new test file was created by adding comments to the existing file test.docx using LibreOffice. I'm not sure if using LibreOffice for this messes with the document in some way or if MS Word comments would be somehow different so it might make sense for a maintainer to add a suitable test file instead or include comments directly to test.docx.

Resolves #31

@VillePuuska
Copy link
Contributor Author

@microsoft-github-policy-service agree

@afourney afourney self-requested a review December 16, 2024 22:25
Copy link
Member

@afourney afourney left a comment

Choose a reason for hiding this comment

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

This is pretty cool. I wonder if it's worth making this default. Will decide that later.

@afourney afourney merged commit 978c876 into microsoft:main Dec 16, 2024
3 checks passed
@VillePuuska VillePuuska deleted the support-comments-in-docx branch December 17, 2024 14:58
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.

Comments in DOCX files are not preserved during conversion
2 participants