python-hwpx 2.16.0__py3-none-any.whl → 2.18.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- hwpx/document.py +119 -2
- hwpx/oxml/__init__.py +14 -0
- hwpx/oxml/_document_impl.py +252 -6
- hwpx/oxml/body.py +154 -5
- hwpx/oxml/header.py +199 -3
- hwpx/tools/exporter.py +46 -11
- hwpx/tools/mail_merge.py +10 -0
- hwpx/tools/pii.py +389 -0
- hwpx/tools/redline.py +186 -0
- {python_hwpx-2.16.0.dist-info → python_hwpx-2.18.0.dist-info}/METADATA +1 -1
- {python_hwpx-2.16.0.dist-info → python_hwpx-2.18.0.dist-info}/RECORD +16 -14
- {python_hwpx-2.16.0.dist-info → python_hwpx-2.18.0.dist-info}/WHEEL +0 -0
- {python_hwpx-2.16.0.dist-info → python_hwpx-2.18.0.dist-info}/entry_points.txt +0 -0
- {python_hwpx-2.16.0.dist-info → python_hwpx-2.18.0.dist-info}/licenses/LICENSE +0 -0
- {python_hwpx-2.16.0.dist-info → python_hwpx-2.18.0.dist-info}/licenses/NOTICE +0 -0
- {python_hwpx-2.16.0.dist-info → python_hwpx-2.18.0.dist-info}/top_level.txt +0 -0
hwpx/document.py
CHANGED
|
@@ -116,6 +116,7 @@ _FORM_FIELD_PARAM_NAMES = {
|
|
|
116
116
|
"guide",
|
|
117
117
|
}
|
|
118
118
|
_TEXT_ILLEGAL = re.compile(r"[\x00-\x08\x0b\x0c\x0e-\x1f\ufffe\uffff]")
|
|
119
|
+
_TRACKED_TEXT_ILLEGAL = re.compile(r"[\x00-\x08\x09\x0b\x0c\x0d\x0e-\x1f\ufffe\uffff]")
|
|
119
120
|
|
|
120
121
|
|
|
121
122
|
def _local_name(node_or_tag: Any) -> str:
|
|
@@ -131,6 +132,10 @@ def _sanitize_field_text(value: str) -> str:
|
|
|
131
132
|
return _TEXT_ILLEGAL.sub("", value)
|
|
132
133
|
|
|
133
134
|
|
|
135
|
+
def _sanitize_tracked_text(value: str) -> str:
|
|
136
|
+
return _TRACKED_TEXT_ILLEGAL.sub("", value)
|
|
137
|
+
|
|
138
|
+
|
|
134
139
|
def _field_type_tokens(*values: str | None) -> set[str]:
|
|
135
140
|
tokens: set[str] = set()
|
|
136
141
|
for value in values:
|
|
@@ -558,6 +563,111 @@ class HwpxDocument:
|
|
|
558
563
|
|
|
559
564
|
return self._root.track_change_author(author_id_ref)
|
|
560
565
|
|
|
566
|
+
def add_track_change(
|
|
567
|
+
self,
|
|
568
|
+
change_type: str,
|
|
569
|
+
*,
|
|
570
|
+
author_name: str = "AI Agent",
|
|
571
|
+
date: str | None = None,
|
|
572
|
+
) -> int:
|
|
573
|
+
"""Add tracked-change header metadata and return the new change id."""
|
|
574
|
+
|
|
575
|
+
return self._root.add_track_change(
|
|
576
|
+
change_type,
|
|
577
|
+
author_name=author_name,
|
|
578
|
+
date=date,
|
|
579
|
+
)
|
|
580
|
+
|
|
581
|
+
@staticmethod
|
|
582
|
+
def _paragraph_has_deletable_text(
|
|
583
|
+
paragraph: HwpxOxmlParagraph,
|
|
584
|
+
match: str | None,
|
|
585
|
+
) -> bool:
|
|
586
|
+
for run in paragraph.runs:
|
|
587
|
+
model = run.to_model()
|
|
588
|
+
run_text = "".join(span.text for span in model.text_spans)
|
|
589
|
+
if match is None:
|
|
590
|
+
if run_text:
|
|
591
|
+
return True
|
|
592
|
+
elif match in run_text:
|
|
593
|
+
return True
|
|
594
|
+
return False
|
|
595
|
+
|
|
596
|
+
def add_tracked_insert(
|
|
597
|
+
self,
|
|
598
|
+
paragraph: HwpxOxmlParagraph,
|
|
599
|
+
text: str,
|
|
600
|
+
*,
|
|
601
|
+
author: str = "AI Agent",
|
|
602
|
+
date: str | None = None,
|
|
603
|
+
char_pr_id_ref: str | int | None = None,
|
|
604
|
+
) -> int:
|
|
605
|
+
"""Append tracked inserted *text* to *paragraph* and return its change id."""
|
|
606
|
+
|
|
607
|
+
sanitized = _sanitize_tracked_text(text)
|
|
608
|
+
if not sanitized:
|
|
609
|
+
raise ValueError("tracked insert text must be non-empty")
|
|
610
|
+
change_id = self.add_track_change("Insert", author_name=author, date=date)
|
|
611
|
+
mark_id = self._root.next_track_change_mark_id()
|
|
612
|
+
paragraph.add_tracked_insert(
|
|
613
|
+
sanitized,
|
|
614
|
+
change_id=change_id,
|
|
615
|
+
mark_id=mark_id,
|
|
616
|
+
char_pr_id_ref=char_pr_id_ref,
|
|
617
|
+
)
|
|
618
|
+
return change_id
|
|
619
|
+
|
|
620
|
+
def add_tracked_delete(
|
|
621
|
+
self,
|
|
622
|
+
paragraph: HwpxOxmlParagraph,
|
|
623
|
+
*,
|
|
624
|
+
match: str | None = None,
|
|
625
|
+
author: str = "AI Agent",
|
|
626
|
+
date: str | None = None,
|
|
627
|
+
) -> int:
|
|
628
|
+
"""Wrap paragraph text or the first matching substring in delete marks."""
|
|
629
|
+
|
|
630
|
+
if match == "":
|
|
631
|
+
raise ValueError("match must be a non-empty string")
|
|
632
|
+
if not self._paragraph_has_deletable_text(paragraph, match):
|
|
633
|
+
if match is None:
|
|
634
|
+
raise ValueError("paragraph contains no text to delete")
|
|
635
|
+
raise ValueError("match text was not found in the paragraph")
|
|
636
|
+
|
|
637
|
+
change_id = self.add_track_change("Delete", author_name=author, date=date)
|
|
638
|
+
mark_id = self._root.next_track_change_mark_id()
|
|
639
|
+
paragraph.add_tracked_delete(
|
|
640
|
+
change_id=change_id,
|
|
641
|
+
first_mark_id=mark_id,
|
|
642
|
+
match=match,
|
|
643
|
+
)
|
|
644
|
+
return change_id
|
|
645
|
+
|
|
646
|
+
def add_tracked_replace(
|
|
647
|
+
self,
|
|
648
|
+
paragraph: HwpxOxmlParagraph,
|
|
649
|
+
old: str,
|
|
650
|
+
new: str,
|
|
651
|
+
*,
|
|
652
|
+
author: str = "AI Agent",
|
|
653
|
+
date: str | None = None,
|
|
654
|
+
) -> tuple[int, int]:
|
|
655
|
+
"""Represent a replacement as tracked delete of *old* plus tracked insert of *new*."""
|
|
656
|
+
|
|
657
|
+
delete_change_id = self.add_tracked_delete(
|
|
658
|
+
paragraph,
|
|
659
|
+
match=old,
|
|
660
|
+
author=author,
|
|
661
|
+
date=date,
|
|
662
|
+
)
|
|
663
|
+
insert_change_id = self.add_tracked_insert(
|
|
664
|
+
paragraph,
|
|
665
|
+
new,
|
|
666
|
+
author=author,
|
|
667
|
+
date=date,
|
|
668
|
+
)
|
|
669
|
+
return delete_change_id, insert_change_id
|
|
670
|
+
|
|
561
671
|
@property
|
|
562
672
|
def memos(self) -> list[HwpxOxmlMemo]:
|
|
563
673
|
"""Return all memo entries declared in every section."""
|
|
@@ -659,7 +769,11 @@ class HwpxDocument:
|
|
|
659
769
|
_append_element(parameters, f"{_HP}integerParam", {"name": "Number"}).text = str(max(1, number))
|
|
660
770
|
_append_element(parameters, f"{_HP}stringParam", {"name": "CreateDateTime"}).text = created_value
|
|
661
771
|
_append_element(parameters, f"{_HP}stringParam", {"name": "Author"}).text = author_value
|
|
662
|
-
|
|
772
|
+
# Hancom's own files use ``MemoShapeIDRef`` (65535 = the built-in default memo
|
|
773
|
+
# shape) — an empty/absent ref leaves the memo box unlinked.
|
|
774
|
+
_append_element(parameters, f"{_HP}stringParam", {"name": "MemoShapeIDRef"}).text = (
|
|
775
|
+
memo_shape_id or "65535"
|
|
776
|
+
)
|
|
663
777
|
|
|
664
778
|
sub_list = _append_element(
|
|
665
779
|
field_begin,
|
|
@@ -684,7 +798,10 @@ class HwpxDocument:
|
|
|
684
798
|
},
|
|
685
799
|
)
|
|
686
800
|
sub_run = _append_element(sub_para, f"{_HP}run", {"charPrIDRef": char_ref})
|
|
687
|
-
|
|
801
|
+
# The MEMO field's subList holds the comment TEXT — this is what Hancom shows
|
|
802
|
+
# in the margin memo box. (Previously this emitted ``memo.id``, so Hancom
|
|
803
|
+
# rendered the numeric id instead of the comment.)
|
|
804
|
+
_append_element(sub_run, f"{_HP}t").text = memo.text or ""
|
|
688
805
|
|
|
689
806
|
run_end = paragraph_element.makeelement(f"{_HP}run", {"charPrIDRef": char_ref})
|
|
690
807
|
ctrl_end = _append_element(run_end, f"{_HP}ctrl")
|
hwpx/oxml/__init__.py
CHANGED
|
@@ -10,10 +10,13 @@ from .body import (
|
|
|
10
10
|
Run,
|
|
11
11
|
Section,
|
|
12
12
|
TextSpan,
|
|
13
|
+
append_tracked_insert_to_run,
|
|
14
|
+
create_track_change_mark,
|
|
13
15
|
parse_paragraph_element,
|
|
14
16
|
parse_run_element,
|
|
15
17
|
parse_section_element,
|
|
16
18
|
parse_text_span,
|
|
19
|
+
wrap_tracked_delete_in_span,
|
|
17
20
|
)
|
|
18
21
|
from .common import GenericElement, parse_generic_element
|
|
19
22
|
|
|
@@ -74,7 +77,9 @@ from .header import (
|
|
|
74
77
|
TrackChangeAuthorList,
|
|
75
78
|
TrackChangeConfig,
|
|
76
79
|
TrackChangeList,
|
|
80
|
+
format_track_change_date,
|
|
77
81
|
memo_shape_from_attributes,
|
|
82
|
+
normalize_track_change_type,
|
|
78
83
|
parse_begin_num,
|
|
79
84
|
parse_bullet,
|
|
80
85
|
parse_bullet_para_head,
|
|
@@ -98,6 +103,8 @@ from .header import (
|
|
|
98
103
|
parse_track_change_author,
|
|
99
104
|
parse_track_change_authors,
|
|
100
105
|
parse_track_changes,
|
|
106
|
+
track_change_author_to_xml,
|
|
107
|
+
track_change_to_xml,
|
|
101
108
|
)
|
|
102
109
|
from .parser import element_to_model, parse_header_xml, parse_section_xml
|
|
103
110
|
from .schema import load_schema
|
|
@@ -173,7 +180,10 @@ __all__ = [
|
|
|
173
180
|
"TrackChangeList",
|
|
174
181
|
"TextSpan",
|
|
175
182
|
"XmlSource",
|
|
183
|
+
"append_tracked_insert_to_run",
|
|
184
|
+
"create_track_change_mark",
|
|
176
185
|
"element_to_model",
|
|
186
|
+
"format_track_change_date",
|
|
177
187
|
"load_schema",
|
|
178
188
|
"parse_begin_num",
|
|
179
189
|
"parse_bullet",
|
|
@@ -202,9 +212,13 @@ __all__ = [
|
|
|
202
212
|
"parse_track_change_author",
|
|
203
213
|
"parse_track_change_authors",
|
|
204
214
|
"parse_track_changes",
|
|
215
|
+
"normalize_track_change_type",
|
|
205
216
|
"parse_section_element",
|
|
206
217
|
"parse_section_xml",
|
|
207
218
|
"parse_text_span",
|
|
219
|
+
"track_change_author_to_xml",
|
|
220
|
+
"track_change_to_xml",
|
|
221
|
+
"wrap_tracked_delete_in_span",
|
|
208
222
|
]
|
|
209
223
|
|
|
210
224
|
logger = logging.getLogger(__name__)
|
hwpx/oxml/_document_impl.py
CHANGED
|
@@ -17,6 +17,7 @@ from . import body
|
|
|
17
17
|
from .common import GenericElement
|
|
18
18
|
from .header import (
|
|
19
19
|
Bullet,
|
|
20
|
+
Header,
|
|
20
21
|
MemoProperties,
|
|
21
22
|
MemoShape,
|
|
22
23
|
ParagraphProperty,
|
|
@@ -26,10 +27,14 @@ from .header import (
|
|
|
26
27
|
memo_shape_from_attributes,
|
|
27
28
|
parse_bullets,
|
|
28
29
|
parse_border_fills,
|
|
30
|
+
parse_header_element,
|
|
29
31
|
parse_paragraph_properties,
|
|
30
32
|
parse_styles,
|
|
33
|
+
parse_track_change_config,
|
|
31
34
|
parse_track_change_authors,
|
|
32
35
|
parse_track_changes,
|
|
36
|
+
track_change_author_to_xml,
|
|
37
|
+
track_change_to_xml,
|
|
33
38
|
)
|
|
34
39
|
from .namespaces import (
|
|
35
40
|
HWPML_COMPAT_ROOT_NAMESPACES,
|
|
@@ -1566,8 +1571,11 @@ class HwpxOxmlRun:
|
|
|
1566
1571
|
def apply_model(self, model: "body.Run") -> None:
|
|
1567
1572
|
new_node = body.serialize_run(model)
|
|
1568
1573
|
xml_bytes = LET.tostring(new_node)
|
|
1569
|
-
replacement = ET.fromstring(xml_bytes)
|
|
1570
1574
|
parent = self.paragraph.element
|
|
1575
|
+
if isinstance(parent, LET._Element):
|
|
1576
|
+
replacement = LET.fromstring(xml_bytes)
|
|
1577
|
+
else:
|
|
1578
|
+
replacement = ET.fromstring(xml_bytes)
|
|
1571
1579
|
run_children = list(parent)
|
|
1572
1580
|
index = run_children.index(self.element)
|
|
1573
1581
|
parent.remove(self.element)
|
|
@@ -3598,8 +3606,11 @@ class HwpxOxmlParagraph:
|
|
|
3598
3606
|
def apply_model(self, model: "body.Paragraph") -> None:
|
|
3599
3607
|
new_node = body.serialize_paragraph(model)
|
|
3600
3608
|
xml_bytes = LET.tostring(new_node)
|
|
3601
|
-
replacement = ET.fromstring(xml_bytes)
|
|
3602
3609
|
parent = self.section.element
|
|
3610
|
+
if isinstance(parent, LET._Element):
|
|
3611
|
+
replacement = LET.fromstring(xml_bytes)
|
|
3612
|
+
else:
|
|
3613
|
+
replacement = ET.fromstring(xml_bytes)
|
|
3603
3614
|
paragraph_children = list(parent)
|
|
3604
3615
|
index = paragraph_children.index(self.element)
|
|
3605
3616
|
parent.remove(self.element)
|
|
@@ -3628,6 +3639,99 @@ class HwpxOxmlParagraph:
|
|
|
3628
3639
|
"""Return the runs contained in this paragraph."""
|
|
3629
3640
|
return [HwpxOxmlRun(run, self) for run in self._run_elements()]
|
|
3630
3641
|
|
|
3642
|
+
def _last_text_run_for_tracked_insert(
|
|
3643
|
+
self,
|
|
3644
|
+
*,
|
|
3645
|
+
char_pr_id_ref: str | int | None = None,
|
|
3646
|
+
) -> HwpxOxmlRun:
|
|
3647
|
+
desired_char = None if char_pr_id_ref is None else str(char_pr_id_ref)
|
|
3648
|
+
runs = self.runs
|
|
3649
|
+
|
|
3650
|
+
if desired_char is not None:
|
|
3651
|
+
for run in reversed(runs):
|
|
3652
|
+
if run.char_pr_id_ref != desired_char:
|
|
3653
|
+
continue
|
|
3654
|
+
if run.to_model().text_spans:
|
|
3655
|
+
return run
|
|
3656
|
+
return self.add_run("", char_pr_id_ref=desired_char)
|
|
3657
|
+
|
|
3658
|
+
for run in reversed(runs):
|
|
3659
|
+
if run.to_model().text_spans:
|
|
3660
|
+
return run
|
|
3661
|
+
return self.add_run("", char_pr_id_ref=self.char_pr_id_ref or "0")
|
|
3662
|
+
|
|
3663
|
+
def add_tracked_insert(
|
|
3664
|
+
self,
|
|
3665
|
+
text: str,
|
|
3666
|
+
*,
|
|
3667
|
+
change_id: int,
|
|
3668
|
+
mark_id: int,
|
|
3669
|
+
char_pr_id_ref: str | int | None = None,
|
|
3670
|
+
) -> None:
|
|
3671
|
+
sanitized = _sanitize_text(text)
|
|
3672
|
+
if not sanitized:
|
|
3673
|
+
raise ValueError("tracked insert text must be non-empty")
|
|
3674
|
+
|
|
3675
|
+
run = self._last_text_run_for_tracked_insert(char_pr_id_ref=char_pr_id_ref)
|
|
3676
|
+
model = run.to_model()
|
|
3677
|
+
body.append_tracked_insert_to_run(
|
|
3678
|
+
model,
|
|
3679
|
+
sanitized,
|
|
3680
|
+
tc_id=change_id,
|
|
3681
|
+
mark_id=mark_id,
|
|
3682
|
+
)
|
|
3683
|
+
run.apply_model(model)
|
|
3684
|
+
|
|
3685
|
+
def add_tracked_delete(
|
|
3686
|
+
self,
|
|
3687
|
+
*,
|
|
3688
|
+
change_id: int,
|
|
3689
|
+
first_mark_id: int,
|
|
3690
|
+
match: str | None = None,
|
|
3691
|
+
) -> int:
|
|
3692
|
+
if match == "":
|
|
3693
|
+
raise ValueError("match must be a non-empty string")
|
|
3694
|
+
|
|
3695
|
+
next_mark_id = first_mark_id
|
|
3696
|
+
if match is not None:
|
|
3697
|
+
for run in self.runs:
|
|
3698
|
+
model = run.to_model()
|
|
3699
|
+
if match not in "".join(span.text for span in model.text_spans):
|
|
3700
|
+
continue
|
|
3701
|
+
for span in model.text_spans:
|
|
3702
|
+
if body.wrap_tracked_delete_in_span(
|
|
3703
|
+
span,
|
|
3704
|
+
tc_id=change_id,
|
|
3705
|
+
mark_id=next_mark_id,
|
|
3706
|
+
match=match,
|
|
3707
|
+
):
|
|
3708
|
+
run.apply_model(model)
|
|
3709
|
+
return next_mark_id + 1
|
|
3710
|
+
raise ValueError("match crosses inline markup and cannot be wrapped safely")
|
|
3711
|
+
raise ValueError("match text was not found in the paragraph")
|
|
3712
|
+
|
|
3713
|
+
modified = False
|
|
3714
|
+
for run in self.runs:
|
|
3715
|
+
model = run.to_model()
|
|
3716
|
+
changed = False
|
|
3717
|
+
for span in model.text_spans:
|
|
3718
|
+
if not span.text:
|
|
3719
|
+
continue
|
|
3720
|
+
body.wrap_tracked_delete_in_span(
|
|
3721
|
+
span,
|
|
3722
|
+
tc_id=change_id,
|
|
3723
|
+
mark_id=next_mark_id,
|
|
3724
|
+
)
|
|
3725
|
+
next_mark_id += 1
|
|
3726
|
+
changed = True
|
|
3727
|
+
if changed:
|
|
3728
|
+
run.apply_model(model)
|
|
3729
|
+
modified = True
|
|
3730
|
+
|
|
3731
|
+
if not modified:
|
|
3732
|
+
raise ValueError("paragraph contains no text to delete")
|
|
3733
|
+
return next_mark_id
|
|
3734
|
+
|
|
3631
3735
|
@property
|
|
3632
3736
|
def text(self) -> str:
|
|
3633
3737
|
"""Return the concatenated textual content of this paragraph."""
|
|
@@ -4710,6 +4814,15 @@ class HwpxOxmlHeader:
|
|
|
4710
4814
|
def attach_document(self, document: "HwpxOxmlDocument") -> None:
|
|
4711
4815
|
self._document = document
|
|
4712
4816
|
|
|
4817
|
+
def to_model(self) -> Header:
|
|
4818
|
+
return parse_header_element(self._convert_to_lxml(self._element))
|
|
4819
|
+
|
|
4820
|
+
@staticmethod
|
|
4821
|
+
def _coerce_serialized_child(parent: ET.Element, child: LET._Element) -> ET.Element:
|
|
4822
|
+
if isinstance(parent, LET._Element):
|
|
4823
|
+
return child
|
|
4824
|
+
return ET.fromstring(LET.tostring(child, encoding="utf-8"))
|
|
4825
|
+
|
|
4713
4826
|
def _begin_num_element(self, create: bool = False) -> ET.Element | None:
|
|
4714
4827
|
element = self._element.find(f"{_HH}beginNum")
|
|
4715
4828
|
if element is None and create:
|
|
@@ -5383,18 +5496,116 @@ class HwpxOxmlHeader:
|
|
|
5383
5496
|
return None
|
|
5384
5497
|
return ref_list.find(f"{_HH}styles")
|
|
5385
5498
|
|
|
5386
|
-
def _track_changes_element(self) -> ET.Element | None:
|
|
5387
|
-
ref_list = self._ref_list_element()
|
|
5499
|
+
def _track_changes_element(self, create: bool = False) -> ET.Element | None:
|
|
5500
|
+
ref_list = self._ref_list_element(create=create)
|
|
5388
5501
|
if ref_list is None:
|
|
5389
5502
|
return None
|
|
5390
5503
|
return ref_list.find(f"{_HH}trackChanges")
|
|
5391
5504
|
|
|
5392
|
-
def
|
|
5393
|
-
ref_list = self._ref_list_element()
|
|
5505
|
+
def _track_changes_element_or_create(self) -> ET.Element:
|
|
5506
|
+
ref_list = self._ref_list_element(create=True)
|
|
5507
|
+
if ref_list is None: # pragma: no cover - defensive branch
|
|
5508
|
+
raise RuntimeError("failed to create <refList> element")
|
|
5509
|
+
element = ref_list.find(f"{_HH}trackChanges")
|
|
5510
|
+
if element is None:
|
|
5511
|
+
element = ref_list.makeelement(f"{_HH}trackChanges", {"itemCnt": "0"})
|
|
5512
|
+
ref_list.append(element)
|
|
5513
|
+
self.mark_dirty()
|
|
5514
|
+
return element
|
|
5515
|
+
|
|
5516
|
+
def _track_change_authors_element(self, create: bool = False) -> ET.Element | None:
|
|
5517
|
+
ref_list = self._ref_list_element(create=create)
|
|
5394
5518
|
if ref_list is None:
|
|
5395
5519
|
return None
|
|
5396
5520
|
return ref_list.find(f"{_HH}trackChangeAuthors")
|
|
5397
5521
|
|
|
5522
|
+
def _track_change_authors_element_or_create(self) -> ET.Element:
|
|
5523
|
+
ref_list = self._ref_list_element(create=True)
|
|
5524
|
+
if ref_list is None: # pragma: no cover - defensive branch
|
|
5525
|
+
raise RuntimeError("failed to create <refList> element")
|
|
5526
|
+
element = ref_list.find(f"{_HH}trackChangeAuthors")
|
|
5527
|
+
if element is None:
|
|
5528
|
+
element = ref_list.makeelement(f"{_HH}trackChangeAuthors", {"itemCnt": "0"})
|
|
5529
|
+
ref_list.append(element)
|
|
5530
|
+
self.mark_dirty()
|
|
5531
|
+
return element
|
|
5532
|
+
|
|
5533
|
+
def _track_change_config_element(self, create: bool = False) -> ET.Element | None:
|
|
5534
|
+
for child in self._element:
|
|
5535
|
+
if tag_local_name(child.tag) in {"trackchageConfig", "trackchangeConfig"}:
|
|
5536
|
+
return child
|
|
5537
|
+
if not create:
|
|
5538
|
+
return None
|
|
5539
|
+
element = self._element.makeelement(f"{_HH}trackchageConfig", {"flags": "0"})
|
|
5540
|
+
self._element.append(element)
|
|
5541
|
+
self.mark_dirty()
|
|
5542
|
+
return element
|
|
5543
|
+
|
|
5544
|
+
@property
|
|
5545
|
+
def track_change_config(self):
|
|
5546
|
+
element = self._track_change_config_element()
|
|
5547
|
+
if element is None:
|
|
5548
|
+
return None
|
|
5549
|
+
return parse_track_change_config(self._convert_to_lxml(element))
|
|
5550
|
+
|
|
5551
|
+
def add_track_change(
|
|
5552
|
+
self,
|
|
5553
|
+
change_type: str,
|
|
5554
|
+
*,
|
|
5555
|
+
author_name: str = "AI Agent",
|
|
5556
|
+
date: str | None = None,
|
|
5557
|
+
) -> int:
|
|
5558
|
+
model = self.to_model()
|
|
5559
|
+
change_id = model.add_track_change(
|
|
5560
|
+
change_type,
|
|
5561
|
+
author_name=author_name,
|
|
5562
|
+
date=date,
|
|
5563
|
+
)
|
|
5564
|
+
if model.ref_list is None or model.ref_list.track_changes is None:
|
|
5565
|
+
raise RuntimeError("failed to create tracked-change metadata")
|
|
5566
|
+
|
|
5567
|
+
change = next(
|
|
5568
|
+
candidate
|
|
5569
|
+
for candidate in model.ref_list.track_changes.changes
|
|
5570
|
+
if candidate.id == change_id
|
|
5571
|
+
)
|
|
5572
|
+
changes_element = self._track_changes_element_or_create()
|
|
5573
|
+
changes_element.append(
|
|
5574
|
+
self._coerce_serialized_child(changes_element, track_change_to_xml(change))
|
|
5575
|
+
)
|
|
5576
|
+
changes_element.set("itemCnt", str(model.ref_list.track_changes.item_cnt or 0))
|
|
5577
|
+
|
|
5578
|
+
authors = model.ref_list.track_change_authors
|
|
5579
|
+
if authors is not None and change.author_id is not None:
|
|
5580
|
+
author = authors.author_by_id(change.author_id)
|
|
5581
|
+
if author is not None:
|
|
5582
|
+
authors_element = self._track_change_authors_element_or_create()
|
|
5583
|
+
existing_ids = {
|
|
5584
|
+
child.get("id")
|
|
5585
|
+
for child in authors_element.findall(f"{_HH}trackChangeAuthor")
|
|
5586
|
+
}
|
|
5587
|
+
author_ids = {str(author.id)} if author.id is not None else set()
|
|
5588
|
+
if author.raw_id is not None:
|
|
5589
|
+
author_ids.add(author.raw_id)
|
|
5590
|
+
if not existing_ids.intersection(author_ids):
|
|
5591
|
+
authors_element.append(
|
|
5592
|
+
self._coerce_serialized_child(
|
|
5593
|
+
authors_element,
|
|
5594
|
+
track_change_author_to_xml(author),
|
|
5595
|
+
)
|
|
5596
|
+
)
|
|
5597
|
+
authors_element.set("itemCnt", str(authors.item_cnt or 0))
|
|
5598
|
+
|
|
5599
|
+
config_element = self._track_change_config_element(create=True)
|
|
5600
|
+
if config_element is None: # pragma: no cover - defensive branch
|
|
5601
|
+
raise RuntimeError("failed to create <trackchageConfig> element")
|
|
5602
|
+
flags = 1
|
|
5603
|
+
if model.track_change_config is not None and model.track_change_config.flags is not None:
|
|
5604
|
+
flags = model.track_change_config.flags
|
|
5605
|
+
config_element.set("flags", str(flags | 1))
|
|
5606
|
+
self.mark_dirty()
|
|
5607
|
+
return change_id
|
|
5608
|
+
|
|
5398
5609
|
def find_basic_border_fill_id(self) -> str | None:
|
|
5399
5610
|
element = self._border_fills_element()
|
|
5400
5611
|
if element is None:
|
|
@@ -6341,6 +6552,41 @@ class HwpxOxmlDocument:
|
|
|
6341
6552
|
) -> TrackChangeAuthor | None:
|
|
6342
6553
|
return HwpxOxmlHeader._lookup_by_id(self.track_change_authors, author_id_ref)
|
|
6343
6554
|
|
|
6555
|
+
def add_track_change(
|
|
6556
|
+
self,
|
|
6557
|
+
change_type: str,
|
|
6558
|
+
*,
|
|
6559
|
+
author_name: str = "AI Agent",
|
|
6560
|
+
date: str | None = None,
|
|
6561
|
+
) -> int:
|
|
6562
|
+
if not self._headers:
|
|
6563
|
+
raise ValueError("document does not contain any headers")
|
|
6564
|
+
return self._headers[0].add_track_change(
|
|
6565
|
+
change_type,
|
|
6566
|
+
author_name=author_name,
|
|
6567
|
+
date=date,
|
|
6568
|
+
)
|
|
6569
|
+
|
|
6570
|
+
def next_track_change_mark_id(self) -> int:
|
|
6571
|
+
max_id = 0
|
|
6572
|
+
for section in self._sections:
|
|
6573
|
+
for element in section.element.iter():
|
|
6574
|
+
if tag_local_name(element.tag) not in {
|
|
6575
|
+
"insertBegin",
|
|
6576
|
+
"insertEnd",
|
|
6577
|
+
"deleteBegin",
|
|
6578
|
+
"deleteEnd",
|
|
6579
|
+
}:
|
|
6580
|
+
continue
|
|
6581
|
+
raw_id = element.get("Id")
|
|
6582
|
+
if raw_id is None:
|
|
6583
|
+
continue
|
|
6584
|
+
try:
|
|
6585
|
+
max_id = max(max_id, int(raw_id))
|
|
6586
|
+
except ValueError:
|
|
6587
|
+
continue
|
|
6588
|
+
return max_id + 1
|
|
6589
|
+
|
|
6344
6590
|
@property
|
|
6345
6591
|
def paragraphs(self) -> list[HwpxOxmlParagraph]:
|
|
6346
6592
|
paragraphs: list[HwpxOxmlParagraph] = []
|