@qvac/skills 0.1.11 → 0.1.13

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.
@@ -1,25 +0,0 @@
1
- """List every paragraph in a Word document, headings included.
2
-
3
- Usage: list_paragraphs.py <input.docx>
4
- """
5
-
6
- import glob
7
- import sys
8
-
9
- from docx import Document
10
-
11
-
12
- # A call that stages the document but forgets scriptArgs still means that document.
13
- staged = glob.glob("*.docx")
14
- if len(sys.argv) == 1 and len(staged) == 1:
15
- sys.argv.append(staged[0])
16
- if len(sys.argv) != 2:
17
- sys.exit(f"usage: list_paragraphs.py <input.docx> — got {sys.argv[1:]}")
18
-
19
- source = sys.argv[1]
20
- doc = Document(source)
21
-
22
- for index, paragraph in enumerate(doc.paragraphs):
23
- text = paragraph.text.replace("\n", " ")
24
- print(f"{index}\t{paragraph.style.name}\t{text}")
25
- print(f"{len(doc.paragraphs)} paragraphs, {len(doc.tables)} table(s)")
@@ -1,58 +0,0 @@
1
- """Replace selected body paragraphs in a Word document.
2
-
3
- Usage: replace_paragraphs.py <input.docx> <output.docx> <index> <new text> [<index> <new text> ...]
4
- """
5
-
6
- import sys
7
-
8
- from docx import Document
9
-
10
- usage = "usage: replace_paragraphs.py <input.docx> <output.docx> <index> <new text> [<index> <new text> ...]"
11
- if len(sys.argv) < 5 or (len(sys.argv) - 3) % 2 != 0:
12
- sys.exit(f"{usage} — got {sys.argv[1:]}")
13
-
14
- source, target = sys.argv[1], sys.argv[2]
15
- if source == target:
16
- sys.exit(f"output {target} must be a new name, not the staged input")
17
-
18
- doc = Document(source)
19
- paragraphs = doc.paragraphs
20
- replacements = []
21
- seen = set()
22
-
23
- for position in range(3, len(sys.argv), 2):
24
- index_raw, new_text = sys.argv[position], sys.argv[position + 1]
25
- try:
26
- index = int(index_raw)
27
- except ValueError:
28
- sys.exit(f"index must be an integer, got: {index_raw}")
29
- if index < 0 or index >= len(paragraphs):
30
- sys.exit(f"index {index} is outside the document's {len(paragraphs)} paragraphs")
31
- if index in seen:
32
- sys.exit(f"index {index} was provided more than once")
33
- if not new_text:
34
- sys.exit(f"new text for index {index} must not be empty")
35
-
36
- paragraph = paragraphs[index]
37
- style_name = paragraph.style.name
38
- if style_name.startswith("Heading") or style_name == "Title":
39
- sys.exit(
40
- f"index {index} is the heading {paragraph.text!r} — headings are not facts/points; "
41
- "pick body paragraph indexes from list_paragraphs.py"
42
- )
43
- if new_text == paragraph.text:
44
- sys.exit(f"index {index} already reads exactly that")
45
-
46
- seen.add(index)
47
- replacements.append((index, new_text))
48
-
49
- before = len(paragraphs)
50
- for index, new_text in replacements:
51
- paragraphs[index].text = new_text
52
-
53
- assert len(doc.paragraphs) == before
54
- for index, new_text in replacements:
55
- assert doc.paragraphs[index].text == new_text
56
-
57
- doc.save(target)
58
- print(f"{len(replacements)} of {before} paragraphs replaced")