|
| 1 | +.. _revisions: |
| 2 | + |
| 3 | +Working with Tracked Changes (Revisions) |
| 4 | +======================================== |
| 5 | + |
| 6 | +Word allows *track changes* (also known as *revisions*) to be enabled on a document. |
| 7 | +This feature records insertions and deletions made to the document, showing who made |
| 8 | +each change and when. This is commonly used for collaborative editing and review |
| 9 | +workflows. |
| 10 | + |
| 11 | +When track changes is enabled: |
| 12 | + |
| 13 | +- Inserted text is marked with the ``<w:ins>`` element |
| 14 | +- Deleted text is marked with the ``<w:del>`` element |
| 15 | +- Each revision records the author, date, and a unique revision ID |
| 16 | + |
| 17 | +.. note:: |
| 18 | + |
| 19 | + *python-docx* supports creating and reading tracked changes, as well as accepting |
| 20 | + or rejecting individual revisions programmatically. |
| 21 | + |
| 22 | + |
| 23 | +Enabling Track Changes |
| 24 | +---------------------- |
| 25 | + |
| 26 | +Track changes mode is controlled via the document settings:: |
| 27 | + |
| 28 | + >>> from docx import Document |
| 29 | + >>> document = Document() |
| 30 | + >>> document.settings.track_revisions = True |
| 31 | + >>> document.settings.track_revisions |
| 32 | + True |
| 33 | + |
| 34 | +When ``track_revisions`` is ``True``, Word will track any subsequent changes made in |
| 35 | +the Word application. Changes made programmatically via *python-docx* must be |
| 36 | +explicitly marked as tracked using the methods described below. |
| 37 | + |
| 38 | + |
| 39 | +Find and Replace with Track Changes |
| 40 | +----------------------------------- |
| 41 | + |
| 42 | +The most common use case is performing a find-and-replace operation where the changes |
| 43 | +are tracked. The :meth:`.Document.find_and_replace_tracked` method handles this:: |
| 44 | + |
| 45 | + >>> document = Document("contract.docx") |
| 46 | + >>> document.settings.track_revisions = True |
| 47 | + >>> count = document.find_and_replace_tracked( |
| 48 | + ... search_text="Acme Corp", |
| 49 | + ... replace_text="NewCo Inc", |
| 50 | + ... author="Legal Team", |
| 51 | + ... comment="Company name updated per merger agreement", |
| 52 | + ... ) |
| 53 | + >>> print(f"Replaced {count} occurrences") |
| 54 | + Replaced 15 occurrences |
| 55 | + >>> document.save("contract_revised.docx") |
| 56 | + |
| 57 | +This method: |
| 58 | + |
| 59 | +- Searches all paragraphs in the document body and tables |
| 60 | +- Replaces only the specific text (word-level), preserving surrounding formatting |
| 61 | +- Creates tracked deletions for the old text and tracked insertions for the new text |
| 62 | +- Optionally attaches a comment to each replacement explaining the change |
| 63 | + |
| 64 | +For more control, you can use :meth:`.Paragraph.replace_tracked` on individual |
| 65 | +paragraphs:: |
| 66 | + |
| 67 | + >>> for paragraph in document.paragraphs: |
| 68 | + ... if "confidential" in paragraph.text.lower(): |
| 69 | + ... paragraph.replace_tracked("draft", "final", author="Editor") |
| 70 | + |
| 71 | + |
| 72 | +Adding Tracked Insertions |
| 73 | +------------------------- |
| 74 | + |
| 75 | +To add new text as a tracked insertion:: |
| 76 | + |
| 77 | + >>> paragraph = document.add_paragraph("This is existing text. ") |
| 78 | + >>> tracked = paragraph.add_run_tracked( |
| 79 | + ... text="This was added later.", |
| 80 | + ... author="John Smith", |
| 81 | + ... ) |
| 82 | + >>> tracked |
| 83 | + <docx.revision.TrackedInsertion object at 0x...> |
| 84 | + >>> tracked.author |
| 85 | + 'John Smith' |
| 86 | + >>> tracked.text |
| 87 | + 'This was added later.' |
| 88 | + |
| 89 | +The ``add_run_tracked`` method wraps the new text in a ``<w:ins>`` element, marking |
| 90 | +it as inserted content that will appear in Word's track changes view. |
| 91 | + |
| 92 | + |
| 93 | +Creating Tracked Deletions |
| 94 | +-------------------------- |
| 95 | + |
| 96 | +To mark existing text as deleted (without actually removing it):: |
| 97 | + |
| 98 | + >>> paragraph = document.add_paragraph("Delete this text please.") |
| 99 | + >>> run = paragraph.runs[0] |
| 100 | + >>> tracked = run.delete_tracked(author="Editor") |
| 101 | + >>> tracked |
| 102 | + <docx.revision.TrackedDeletion object at 0x...> |
| 103 | + >>> tracked.text |
| 104 | + 'Delete this text please.' |
| 105 | + |
| 106 | +The text remains in the document but is wrapped in a ``<w:del>`` element. In Word, |
| 107 | +this text appears with strikethrough formatting. |
| 108 | + |
| 109 | + |
| 110 | +Iterating Over Revisions |
| 111 | +------------------------ |
| 112 | + |
| 113 | +To access tracked changes in a paragraph, use ``iter_inner_content`` with |
| 114 | +``include_revisions=True``:: |
| 115 | + |
| 116 | + >>> from docx.revision import TrackedInsertion, TrackedDeletion |
| 117 | + >>> for item in paragraph.iter_inner_content(include_revisions=True): |
| 118 | + ... if isinstance(item, TrackedInsertion): |
| 119 | + ... print(f"INSERTED by {item.author}: {item.text}") |
| 120 | + ... elif isinstance(item, TrackedDeletion): |
| 121 | + ... print(f"DELETED by {item.author}: {item.text}") |
| 122 | + ... else: |
| 123 | + ... print(f"Normal text: {item.text}") |
| 124 | + |
| 125 | + |
| 126 | +Accepting and Rejecting Changes |
| 127 | +------------------------------- |
| 128 | + |
| 129 | +Individual revisions can be accepted or rejected programmatically:: |
| 130 | + |
| 131 | + >>> # Accept an insertion (keeps the inserted text) |
| 132 | + >>> tracked_insertion.accept() |
| 133 | + |
| 134 | + >>> # Reject an insertion (removes the inserted text) |
| 135 | + >>> tracked_insertion.reject() |
| 136 | + |
| 137 | + >>> # Accept a deletion (removes the deleted text) |
| 138 | + >>> tracked_deletion.accept() |
| 139 | + |
| 140 | + >>> # Reject a deletion (restores the deleted text) |
| 141 | + >>> tracked_deletion.reject() |
| 142 | + |
| 143 | + |
| 144 | +TrackedInsertion and TrackedDeletion Properties |
| 145 | +----------------------------------------------- |
| 146 | + |
| 147 | +Both ``TrackedInsertion`` and ``TrackedDeletion`` objects provide these properties: |
| 148 | + |
| 149 | +``author`` |
| 150 | + The name of the author who made the change (read/write). |
| 151 | + |
| 152 | +``date`` |
| 153 | + The date and time of the change as a ``datetime`` object (read-only). |
| 154 | + |
| 155 | +``revision_id`` |
| 156 | + The unique identifier for this revision (read/write). |
| 157 | + |
| 158 | +``text`` |
| 159 | + The text content of the revision (read-only). |
| 160 | + |
| 161 | +``runs`` |
| 162 | + A list of ``Run`` objects contained in the revision. |
| 163 | + |
| 164 | +``is_run_level`` |
| 165 | + ``True`` if the revision contains runs (inline content). |
| 166 | + |
| 167 | +``is_block_level`` |
| 168 | + ``True`` if the revision contains paragraphs or tables (block content). |
| 169 | + |
| 170 | + |
| 171 | +Example: Processing a Document with Track Changes |
| 172 | +------------------------------------------------- |
| 173 | + |
| 174 | +Here's a complete example that processes an existing document with track changes:: |
| 175 | + |
| 176 | + >>> from docx import Document |
| 177 | + >>> from docx.revision import TrackedInsertion, TrackedDeletion |
| 178 | + |
| 179 | + >>> document = Document("reviewed_document.docx") |
| 180 | + |
| 181 | + >>> # Count revisions |
| 182 | + >>> insertions = 0 |
| 183 | + >>> deletions = 0 |
| 184 | + |
| 185 | + >>> for paragraph in document.paragraphs: |
| 186 | + ... for item in paragraph.iter_inner_content(include_revisions=True): |
| 187 | + ... if isinstance(item, TrackedInsertion): |
| 188 | + ... insertions += 1 |
| 189 | + ... print(f"[+] {item.author}: {item.text[:50]}...") |
| 190 | + ... elif isinstance(item, TrackedDeletion): |
| 191 | + ... deletions += 1 |
| 192 | + ... print(f"[-] {item.author}: {item.text[:50]}...") |
| 193 | + |
| 194 | + >>> print(f"\nTotal: {insertions} insertions, {deletions} deletions") |
| 195 | + |
| 196 | + |
| 197 | +Example: Bulk Accept All Changes |
| 198 | +-------------------------------- |
| 199 | + |
| 200 | +To accept all tracked changes in a document:: |
| 201 | + |
| 202 | + >>> from docx import Document |
| 203 | + >>> from docx.revision import TrackedInsertion, TrackedDeletion |
| 204 | + |
| 205 | + >>> document = Document("reviewed_document.docx") |
| 206 | + |
| 207 | + >>> for paragraph in document.paragraphs: |
| 208 | + ... for item in list(paragraph.iter_inner_content(include_revisions=True)): |
| 209 | + ... if isinstance(item, (TrackedInsertion, TrackedDeletion)): |
| 210 | + ... item.accept() |
| 211 | + |
| 212 | + >>> document.save("accepted_document.docx") |
| 213 | + |
| 214 | +Note the use of ``list()`` to materialize the iterator before modifying the document, |
| 215 | +as accepting/rejecting changes modifies the underlying XML. |
0 commit comments