Skip to content

Commit b82f370

Browse files
Update README, add find/replace with style to document (#12)
* Update README, add find/replace with style to document * Remove pytest import in example
1 parent 7fac64f commit b82f370

3 files changed

Lines changed: 43 additions & 37 deletions

File tree

README.md

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,4 @@
1-
[![DOI](https://zenodo.org/badge/657341621.svg)](https://zenodo.org/doi/10.5281/zenodo.10383685)
2-
3-
# CMI-DAIR Template Python Repository
4-
5-
Welcome to the CMI-DAIR Template Python Repository! This template is designed to streamline your project setup and ensure a consistent structure. To get started, follow these steps:
6-
7-
8-
- [x] Run `setup_template.py` to initialize the repository.
9-
- [ ] Replace the content of this `README.md` with details specific to your project.
10-
- [ ] Install the `pre-commit` hooks to ensure code quality on each commit.
11-
- [ ] Revise SECURITY.md to reflect supported versions or remove it if not applicable.
12-
- [ ] Remove the placeholder src and test files, these are there merely to show how the CI works.
13-
- [ ] If it hasn't already been done for your organization/acccount, grant third-party app permissions for CodeCov.
14-
- [ ] To set up an API documentation website, after the first successful build, go to the `Settings` tab of your repository, scroll down to the `GitHub Pages` section, and select `gh-pages` as the source. This will generate a link to your API docs.
15-
- [ ] Update stability badge in `README.md` to reflect the current state of the project. A list of stability badges to copy can be found [here](https://github.com/orangemug/stability-badges). The [node documentation](https://nodejs.org/docs/latest-v20.x/api/documentation.html#documentation_stability_index) can be used as a reference for the stability levels.
16-
17-
# Project name
1+
# CMI-docx
182

193
[![Build](https://github.com/childmindresearch/cmi-docx/actions/workflows/test.yaml/badge.svg?branch=main)](https://github.com/childmindresearch/cmi-docx/actions/workflows/test.yaml?query=branch%3Amain)
204
[![codecov](https://codecov.io/gh/childmindresearch/cmi-docx/branch/main/graph/badge.svg?token=22HWWFWPW5)](https://codecov.io/gh/childmindresearch/cmi-docx)
@@ -23,38 +7,42 @@ Welcome to the CMI-DAIR Template Python Repository! This template is designed to
237
[![LGPL--2.1 License](https://img.shields.io/badge/license-LGPL--2.1-blue.svg)](https://github.com/childmindresearch/cmi-docx/blob/main/LICENSE)
248
[![pages](https://img.shields.io/badge/api-docs-blue)](https://childmindresearch.github.io/cmi-docx)
259

26-
What problem does this tool solve?
10+
`cmi-docx` is a Python library for manipulating .docx files built on top of `python-docx`. It extends the functionality of `python-docx` by providing additional features and utilities for working with .docx files.
2711

2812
## Features
2913

30-
- A few
31-
- Cool
32-
- Things
14+
- Find and replace.
15+
- Insert paragraphs in the middle of a document.
16+
- Manipulate styles and formatting of paragraphs' substrings.
3317

3418
## Installation
3519

36-
Install this package via :
20+
Install this package from pypi using your favorite package manager. For example, using `pip`:
3721

3822
```sh
39-
pip install cmi_docx
40-
```
41-
42-
Or get the newest development version via:
43-
44-
```sh
45-
pip install git+https://github.com/childmindresearch/cmi-docx
23+
pip install cmi-docx
4624
```
4725

4826
## Quick start
4927

50-
Short tutorial, maybe with a
28+
The following example demonstratesa few features of cmi-docx:
5129

5230
```Python
53-
import cmi_docx
31+
import docx
5432

55-
cmi_docx.short_example()
56-
```
33+
from cmi_docx import document
5734

58-
## Links or References
35+
doc = docx.Document()
36+
paragraph = doc.add_paragraph("Hello, world!")
37+
extend_document = document.ExtendDocument(doc)
38+
extend_paragraph = document.ExtendParagraph(paragraph)
5939

60-
- [https://www.wikipedia.de](https://www.wikipedia.de)
40+
# Find and replace text.
41+
extend_document.replace("Hello", "Hi", {"bold": True})
42+
43+
# Insert and image
44+
extend_document.insert_image(index=1, image_path="path/to/image.png")
45+
46+
# Reformat a paragraph
47+
extend_paragraph.format(italics=True)
48+
```

src/cmi_docx/document.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Extends a python-docx Word document with additional functionality."""
22

33
import pathlib
4+
from typing import Any
45

56
from docx import document
67
from docx.text import paragraph as docx_paragraph
@@ -44,12 +45,15 @@ def find_in_runs(self, needle: str) -> list[run.FindRun]:
4445
for finder in paragraph.ExtendParagraph(para).find_in_runs(needle)
4546
]
4647

47-
def replace(self, needle: str, replace: str) -> None:
48+
def replace(
49+
self, needle: str, replace: str, style: dict[str, Any] | None = None
50+
) -> None:
4851
"""Finds and replaces text in a Word document.
4952
5053
Args:
5154
needle: The text to find.
5255
replace: The text to replace.
56+
style: The style to apply to the replacement text.
5357
5458
"""
5559
run_finder = self.find_in_runs(needle)
@@ -58,7 +62,7 @@ def replace(self, needle: str, replace: str) -> None:
5862
)
5963

6064
for run_find in run_finder:
61-
run_find.replace(replace)
65+
run_find.replace(replace, style)
6266

6367
def insert_paragraph_by_text(
6468
self,

tests/test_document.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,20 @@ def test_replace(runs: list[str], needle: str, replace: str, expected: str) -> N
9090
assert doc.paragraphs[0].text == expected
9191

9292

93+
def test_replace_with_style() -> None:
94+
"""Test replacing text in a document with style."""
95+
doc = docx.Document()
96+
doc.add_paragraph("Hello, world!")
97+
extend_document = document.ExtendDocument(doc)
98+
99+
extend_document.replace("Hello", "Goodbye", {"bold": True})
100+
101+
assert doc.paragraphs[0].text == "Goodbye, world!"
102+
assert not doc.paragraphs[0].runs[0].bold
103+
assert doc.paragraphs[0].runs[1].bold
104+
assert doc.paragraphs[0].runs[1].text == "Goodbye"
105+
106+
93107
def test_insert_paragraph_by_object() -> None:
94108
"""Test inserting a paragraph into a document."""
95109
doc = docx.Document()

0 commit comments

Comments
 (0)