Skip to content

Commit 5e8318b

Browse files
Support full output path in doc2docx (CoderWanFeng#157)
* Support doc2docx output file paths * Document doc2docx output file path usage * Add doc2docx output path test
1 parent d9ee9b6 commit 5e8318b

3 files changed

Lines changed: 68 additions & 2 deletions

File tree

‎office/api/word.py‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
Project:
1515
https://www.python-office.com
1616
"""
17+
from pathlib import Path
18+
1719

1820
def _load_poword():
1921
try:
@@ -69,12 +71,17 @@ def doc2docx(input_path: str, output_path: str = r'./', output_name: str = None)
6971
7072
Args:
7173
input_path (str): input Doc file path / 输入Doc文件的路径
72-
output_path (str, optional): output Docx file path / 输出Docx文件的路径。Default / 默认: current directory / 当前目录
74+
output_path (str, optional): output Docx file path / 输出Docx文件的路径。Can be a directory or a .docx file path / 可以是目录或 .docx 文件路径。Default / 默认: current directory / 当前目录
7375
output_name (str, optional): output Docx file name / 输出Docx文件的名称。Default / 默认: original filename / 原文件名
7476
7577
Returns:
7678
None
7779
"""
80+
if output_name is None and Path(output_path).suffix.lower() == ".docx":
81+
output_file = Path(output_path)
82+
output_path = str(output_file.parent)
83+
output_name = output_file.name
84+
7885
poword = _load_poword()
7986
poword.doc2docx(input_path=input_path, output_path=output_path, output_name=output_name)
8087

‎skills/word/doc2docx/SKILL.md‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,23 @@ doc2docx(
2727
)
2828
```
2929

30+
也可以直接把目标文件路径传给 `output_path`:
31+
32+
```python
33+
from office.skills.word import doc2docx
34+
35+
doc2docx(
36+
input_path='./old.doc',
37+
output_path='./new/new.docx'
38+
)
39+
```
40+
3041
## 参数说明
3142

3243
| 参数 | 类型 | 必填 | 默认值 | 说明 |
3344
|------|------|------|--------|------|
3445
| `input_path` | str | 是 | - | 输入 Doc 文件的路径 |
35-
| `output_path` | str | 否 | `'./'` | 输出 Docx 文件的路径 |
46+
| `output_path` | str | 否 | `'./'` | 输出 Docx 文件的路径,可以是输出目录,也可以是完整的 `.docx` 文件路径 |
3647
| `output_name` | str | 否 | `None` | 输出 Docx 文件的名称。默认原文件名 |
3748

3849
## 返回值
@@ -46,6 +57,11 @@ from office.skills.word import doc2docx
4657
doc2docx(input_path='./old.doc', output_path='./new/')
4758
```
4859

60+
```python
61+
from office.skills.word import doc2docx
62+
doc2docx(input_path='./old.doc', output_path='./new/new.docx')
63+
```
64+
4965
## 原始函数
5066

5167
`office.api.word.doc2docx`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""Tests for doc2docx output path handling."""
2+
3+
import unittest
4+
from unittest.mock import Mock, patch
5+
6+
from office.api import word as word_api
7+
8+
9+
class TestDoc2DocxOutputPath(unittest.TestCase):
10+
def test_doc2docx_accepts_full_output_file_path(self):
11+
poword = Mock()
12+
13+
with patch.object(word_api, "_load_poword", return_value=poword):
14+
word_api.doc2docx(
15+
input_path="source.doc",
16+
output_path="converted/result.docx",
17+
)
18+
19+
poword.doc2docx.assert_called_once_with(
20+
input_path="source.doc",
21+
output_path="converted",
22+
output_name="result.docx",
23+
)
24+
25+
def test_doc2docx_keeps_directory_output_path(self):
26+
poword = Mock()
27+
28+
with patch.object(word_api, "_load_poword", return_value=poword):
29+
word_api.doc2docx(
30+
input_path="source.doc",
31+
output_path="converted",
32+
output_name="result.docx",
33+
)
34+
35+
poword.doc2docx.assert_called_once_with(
36+
input_path="source.doc",
37+
output_path="converted",
38+
output_name="result.docx",
39+
)
40+
41+
42+
if __name__ == "__main__":
43+
unittest.main()

0 commit comments

Comments
 (0)