indexdoc-converter 0.2.0__py3-none-any.whl → 0.2.2__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.
- indexdoc_converter/excel_to_md.py +1 -1
- indexdoc_converter/html_to_md.py +1 -1
- indexdoc_converter/pptx_to_md.py +126 -29
- indexdoc_converter/utils/FileUtil.py +45 -0
- indexdoc_converter/utils/IDUtil.py +40 -0
- indexdoc_converter/utils/__init__.py +0 -0
- indexdoc_converter/utils/img_to_base64.py +218 -0
- {indexdoc_converter-0.2.0.dist-info → indexdoc_converter-0.2.2.dist-info}/METADATA +1 -1
- indexdoc_converter-0.2.2.dist-info/RECORD +13 -0
- indexdoc_converter-0.2.0.dist-info/RECORD +0 -9
- {indexdoc_converter-0.2.0.dist-info → indexdoc_converter-0.2.2.dist-info}/WHEEL +0 -0
- {indexdoc_converter-0.2.0.dist-info → indexdoc_converter-0.2.2.dist-info}/top_level.txt +0 -0
indexdoc_converter/html_to_md.py
CHANGED
|
@@ -10,7 +10,7 @@ from PIL import Image
|
|
|
10
10
|
from readability import Document
|
|
11
11
|
import html2text
|
|
12
12
|
|
|
13
|
-
from utils.img_to_base64 import Image2Base64
|
|
13
|
+
from indexdoc_converter.utils.img_to_base64 import Image2Base64
|
|
14
14
|
|
|
15
15
|
# ============ 图片下载和处理类 ============
|
|
16
16
|
class ImageDownloader:
|
indexdoc_converter/pptx_to_md.py
CHANGED
|
@@ -1,79 +1,176 @@
|
|
|
1
1
|
import pptx2md
|
|
2
2
|
from pathlib import Path
|
|
3
|
+
import os
|
|
3
4
|
import tempfile
|
|
5
|
+
import shutil # 新增:用于清理临时目录
|
|
4
6
|
|
|
5
|
-
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
from indexdoc_converter.utils.img_to_base64 import OutputFormat
|
|
10
|
+
from indexdoc_converter.utils.img_to_base64 import Image2Base64
|
|
11
|
+
from indexdoc_converter.utils import IDUtil
|
|
12
|
+
|
|
13
|
+
def replace_img_with_base64(md_file: Path, img_dir: Path, del_temp_img: bool = True):
|
|
14
|
+
"""
|
|
15
|
+
终极修复版:解决URL编码(%5C)、Windows反斜杠、相对/绝对路径等所有匹配问题
|
|
16
|
+
:param md_file: 生成的MD文件路径
|
|
17
|
+
:param img_dir: pptx2md导出的临时图片目录
|
|
18
|
+
:param del_temp_img: 是否删除临时图片/目录(推荐True)
|
|
19
|
+
"""
|
|
20
|
+
import urllib.parse # 内置库,无需额外安装,处理URL编码/解码
|
|
21
|
+
|
|
22
|
+
if not md_file.exists() or not img_dir.is_dir():
|
|
23
|
+
print(f"⚠ 跳过Base64替换:MD文件或图片目录不存在")
|
|
24
|
+
return
|
|
25
|
+
|
|
26
|
+
# 1. 读取MD内容,保留原始编码格式
|
|
27
|
+
with open(md_file, 'r', encoding='utf-8') as f:
|
|
28
|
+
md_content = f.read()
|
|
29
|
+
|
|
30
|
+
img_path_2_b64 = {}
|
|
31
|
+
# 2. 遍历临时图片,生成所有需要匹配的路径格式 + Base64映射
|
|
32
|
+
for img_file in img_dir.glob("*.*"):
|
|
33
|
+
if not img_file.is_file():
|
|
34
|
+
continue
|
|
35
|
+
try:
|
|
36
|
+
# 调用你的Base64工具类生成MD格式的Base64(参数可按需求调整)
|
|
37
|
+
b64_md_str = Image2Base64.convert_file(
|
|
38
|
+
image_path=img_file,
|
|
39
|
+
max_dim=1200,
|
|
40
|
+
max_kb=200,
|
|
41
|
+
force_webp=True,
|
|
42
|
+
out_format=OutputFormat.MARKDOWN_ALT,
|
|
43
|
+
quality=80
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# 关键:生成5种需要替换的路径格式,覆盖pptx2md所有生成情况
|
|
47
|
+
img_dir_name = img_dir.name # 临时目录名(如4fb6cc7bfcbb11f0aefebc2411ecbf31_pptx_imgs)
|
|
48
|
+
img_name = img_file.name # 图片名(如测试文件_0.jpg)
|
|
49
|
+
# 格式1:绝对路径(C:\Temp\xxx_imgs\测试文件_0.jpg)
|
|
50
|
+
img_path_2_b64[str(img_file)] = b64_md_str
|
|
51
|
+
# 格式2:纯文件名(测试文件_0.jpg)
|
|
52
|
+
img_path_2_b64[img_name] = b64_md_str
|
|
53
|
+
# 格式3:Windows原始反斜杠相对路径(xxx_imgs\测试文件_0.jpg)
|
|
54
|
+
win_relative_path = f"{img_dir_name}\\{img_name}"
|
|
55
|
+
img_path_2_b64[win_relative_path] = b64_md_str
|
|
56
|
+
# 格式4:URL编码后的相对路径(xxx_imgs%5C测试文件_0.jpg)【核心修复项】
|
|
57
|
+
url_encoded_path = urllib.parse.quote(win_relative_path, safe='')
|
|
58
|
+
img_path_2_b64[url_encoded_path] = b64_md_str
|
|
59
|
+
# 格式5:正斜杠相对路径(xxx_imgs/测试文件_0.jpg)【兼容兜底】
|
|
60
|
+
slash_relative_path = f"{img_dir_name}/{img_name}"
|
|
61
|
+
img_path_2_b64[slash_relative_path] = b64_md_str
|
|
62
|
+
|
|
63
|
+
except Exception as e:
|
|
64
|
+
print(f"⚠ 单张图片转Base64失败: {img_file.name} | 错误: {e}")
|
|
65
|
+
continue
|
|
66
|
+
|
|
67
|
+
# 3. 批量替换:按「长路径优先」替换,避免短路径匹配冲突
|
|
68
|
+
# 排序:路径越长越先替换,防止纯文件名先匹配导致长路径替换失败
|
|
69
|
+
for raw_img_path in sorted(img_path_2_b64.keys(), key=len, reverse=True):
|
|
70
|
+
md_content = md_content.replace('', img_path_2_b64[raw_img_path])
|
|
71
|
+
|
|
72
|
+
# 4. 重写MD文件,写入Base64内嵌内容
|
|
73
|
+
with open(md_file, 'w', encoding='utf-8') as f:
|
|
74
|
+
f.write(md_content)
|
|
75
|
+
|
|
76
|
+
# 5. 清理临时图片目录,避免冗余
|
|
77
|
+
if del_temp_img:
|
|
78
|
+
try:
|
|
79
|
+
import shutil
|
|
80
|
+
shutil.rmtree(img_dir)
|
|
81
|
+
print(f"✅ 清理临时图片目录完成: {img_dir.name}")
|
|
82
|
+
except Exception as e:
|
|
83
|
+
print(f"⚠ 临时图片目录清理失败: {e}")
|
|
6
84
|
|
|
7
85
|
def pptx_to_md(pptx_file):
|
|
8
|
-
|
|
86
|
+
"""单文件PPT转MD:图片自动转为Base64内嵌"""
|
|
87
|
+
# 生成唯一的MD文件名和专属图片临时目录(避免多文件冲突)
|
|
88
|
+
uuid_str = IDUtil.get_uuid()
|
|
89
|
+
md_file_path = Path(tempfile.gettempdir()) / f"{uuid_str}.md"
|
|
90
|
+
temp_img_dir = Path(tempfile.gettempdir()) / f"{uuid_str}_pptx_imgs"
|
|
91
|
+
temp_img_dir.mkdir(parents=True, exist_ok=True) # 创建专属图片目录
|
|
92
|
+
|
|
93
|
+
# 配置pptx2md转换参数
|
|
9
94
|
config = pptx2md.ConversionConfig(
|
|
10
95
|
pptx_path=pptx_file,
|
|
11
96
|
output_path=md_file_path,
|
|
12
|
-
image_dir=
|
|
13
|
-
disable_notes=True
|
|
97
|
+
image_dir=temp_img_dir, # 图片导出到「专属临时目录」,而非全局临时目录
|
|
98
|
+
disable_notes=True # 根据需求调整:是否禁用PPT备注页转换
|
|
14
99
|
)
|
|
100
|
+
# 执行PPT转MD(此时MD中是图片路径,图片在临时目录)
|
|
15
101
|
pptx2md.convert(config)
|
|
102
|
+
# 核心步骤:调用替换函数,将MD中的图片路径改为Base64内嵌
|
|
103
|
+
replace_img_with_base64(md_file_path, temp_img_dir)
|
|
104
|
+
|
|
16
105
|
return md_file_path
|
|
17
106
|
|
|
107
|
+
|
|
18
108
|
def batch_pptx_to_md(input_dir: str, output_dir: str = None, image_dir: str = None):
|
|
19
109
|
"""
|
|
20
|
-
|
|
21
|
-
|
|
110
|
+
批量PPT转MD:图片自动转为Base64内嵌(原image_dir参数失效,无需传值)
|
|
22
111
|
Args:
|
|
23
112
|
input_dir (str): 包含 PPTX 文件的文件夹路径
|
|
24
113
|
output_dir (str, optional): 输出 Markdown 文件的文件夹。若为 None,则输出到与 PPTX 相同位置
|
|
25
|
-
image_dir (str, optional):
|
|
114
|
+
image_dir (str, optional): 兼容原参数,实际已失效(图片不再导出到该目录,直接内嵌Base64)
|
|
26
115
|
"""
|
|
27
116
|
input_path = Path(input_dir)
|
|
28
117
|
if not input_path.exists() or not input_path.is_dir():
|
|
29
118
|
raise ValueError(f"输入目录不存在或不是文件夹: {input_dir}")
|
|
30
119
|
|
|
31
|
-
#
|
|
120
|
+
# 处理输出目录
|
|
32
121
|
if output_dir is None:
|
|
33
122
|
output_path = input_path
|
|
34
123
|
else:
|
|
35
124
|
output_path = Path(output_dir)
|
|
36
125
|
output_path.mkdir(parents=True, exist_ok=True)
|
|
37
126
|
|
|
38
|
-
#
|
|
39
|
-
if image_dir:
|
|
40
|
-
img_path = Path(image_dir)
|
|
41
|
-
img_path.mkdir(parents=True, exist_ok=True)
|
|
42
|
-
else:
|
|
43
|
-
img_path = None
|
|
44
|
-
|
|
127
|
+
# 筛选所有pptx文件
|
|
45
128
|
pptx_files = list(input_path.glob("*.pptx"))
|
|
46
129
|
if not pptx_files:
|
|
47
130
|
print(f"在 {input_dir} 中未找到 .pptx 文件")
|
|
48
131
|
return
|
|
49
132
|
|
|
50
|
-
print(f"发现 {len(pptx_files)} 个 PPTX
|
|
133
|
+
print(f"发现 {len(pptx_files)} 个 PPTX 文件,开始转换(图片自动Base64内嵌)...\n")
|
|
51
134
|
|
|
135
|
+
# 批量转换
|
|
52
136
|
for pptx_file in pptx_files:
|
|
53
137
|
try:
|
|
54
138
|
md_file = output_path / f"{pptx_file.stem}.md"
|
|
139
|
+
# 为当前PPT创建专属临时图片目录(避免多PPT图片冲突)
|
|
140
|
+
uuid_str = IDUtil.get_uuid()
|
|
141
|
+
temp_img_dir = Path(tempfile.gettempdir()) / f"{uuid_str}_{pptx_file.stem}_imgs"
|
|
142
|
+
temp_img_dir.mkdir(parents=True, exist_ok=True)
|
|
55
143
|
|
|
144
|
+
# 配置pptx2md
|
|
56
145
|
config = pptx2md.ConversionConfig(
|
|
57
146
|
pptx_path=pptx_file,
|
|
58
147
|
output_path=md_file,
|
|
59
|
-
image_dir=
|
|
60
|
-
disable_notes=True
|
|
148
|
+
image_dir=temp_img_dir, # 图片导出到专属临时目录
|
|
149
|
+
disable_notes=True
|
|
61
150
|
)
|
|
62
151
|
|
|
63
|
-
print(f"正在转换: {pptx_file.name}
|
|
64
|
-
|
|
152
|
+
print(f"正在转换: {pptx_file.name}")
|
|
65
153
|
pptx2md.convert(config)
|
|
66
|
-
|
|
154
|
+
# 核心:替换图片路径为Base64
|
|
155
|
+
replace_img_with_base64(md_file, temp_img_dir)
|
|
156
|
+
print(f"✅ 转换成功: {md_file.name}\n")
|
|
157
|
+
|
|
67
158
|
except Exception as e:
|
|
68
|
-
print(f"❌
|
|
159
|
+
print(f"❌ 转换失败: {pptx_file.name} | 错误: {e}\n")
|
|
160
|
+
continue
|
|
69
161
|
|
|
70
|
-
print("
|
|
162
|
+
print("📌 批量转换完成!所有MD文件均为图片Base64内嵌格式")
|
|
71
163
|
|
|
72
164
|
|
|
73
165
|
if __name__ == "__main__":
|
|
74
|
-
#
|
|
75
|
-
input_folder = r'D:\测试目录_全面\ppt' #
|
|
76
|
-
output_folder = './markdown_out'
|
|
77
|
-
image_folder =
|
|
78
|
-
|
|
79
|
-
|
|
166
|
+
# 配置你的路径(image_folder无需传值,已失效)
|
|
167
|
+
input_folder = r'D:\测试目录_全面\ppt' # 你的PPT文件夹路径
|
|
168
|
+
output_folder = './markdown_out' # MD输出路径,设为None则输出到PPT同目录
|
|
169
|
+
# image_folder = None # 无需设置,图片直接Base64内嵌,该参数失效
|
|
170
|
+
|
|
171
|
+
# 执行批量转换
|
|
172
|
+
batch_pptx_to_md(
|
|
173
|
+
input_dir=input_folder,
|
|
174
|
+
output_dir=output_folder,
|
|
175
|
+
# image_dir无需传值,保留原参数仅为兼容
|
|
176
|
+
)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import os
|
|
2
|
+
def get_filepath_shortname_suffix(file_url):
|
|
3
|
+
"""
|
|
4
|
+
获取文件路径, 文件名, 后缀名
|
|
5
|
+
:param file_url:
|
|
6
|
+
:return:
|
|
7
|
+
"""
|
|
8
|
+
filepath, tmpfilename = os.path.split(file_url)
|
|
9
|
+
shotname, extension = os.path.splitext(tmpfilename)
|
|
10
|
+
return filepath, shotname, extension
|
|
11
|
+
|
|
12
|
+
def get_file_suffix(file_url):
|
|
13
|
+
return os.path.splitext(file_url)[-1].lower()
|
|
14
|
+
|
|
15
|
+
def get_file_name_without_suffix(file_url):
|
|
16
|
+
return os.path.splitext(file_url)[-2]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def detect_encoding(file_path: str) -> str:
|
|
20
|
+
"""
|
|
21
|
+
稳定版编码检测
|
|
22
|
+
核心思路:实际读取测试比理论检测更可靠
|
|
23
|
+
"""
|
|
24
|
+
encodings_to_try = [
|
|
25
|
+
'utf-8', # 必须包含
|
|
26
|
+
'gb18030', # 最全中文支持
|
|
27
|
+
'utf-8-sig', # 必须包含
|
|
28
|
+
'big5', # 台湾繁体,极少数情况
|
|
29
|
+
'utf-16', # 极少数情况
|
|
30
|
+
'cp1252', # Windows西欧
|
|
31
|
+
'latin1', # 最兼容的回退
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
for encoding in encodings_to_try:
|
|
35
|
+
try:
|
|
36
|
+
with open(file_path, 'r', encoding=encoding) as f:
|
|
37
|
+
# 读取前1KB测试,不消耗太多资源
|
|
38
|
+
f.read(1024)
|
|
39
|
+
# 如果测试读取成功,返回该编码
|
|
40
|
+
# print(encoding)
|
|
41
|
+
return encoding
|
|
42
|
+
except UnicodeDecodeError:
|
|
43
|
+
continue
|
|
44
|
+
#默认返回utf-8
|
|
45
|
+
return 'utf-8'
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import uuid
|
|
2
|
+
from datetime import datetime
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def get_long():
|
|
6
|
+
return get_long_id_by_time()
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def get_max_long():
|
|
10
|
+
return 9999999999999999
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_uuid():
|
|
14
|
+
return uuid.uuid1().hex
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
# 适合数据量不大的非集群应用,每秒插入不超过10000个
|
|
18
|
+
# 如果需要作集群,需要将此服务独立出来单独作为一个ID生成器进程。也可以定义一个标志位作为集群ID。
|
|
19
|
+
from datetime import datetime
|
|
20
|
+
|
|
21
|
+
last_time_id = 21010100000000
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def get_long_id_by_time():
|
|
25
|
+
global last_time_id
|
|
26
|
+
dt = datetime.now()
|
|
27
|
+
new_id = datetime.strftime(dt, '%y%m%d%H%M%S0000')
|
|
28
|
+
new_id = int(new_id) + int(dt.microsecond / 100)
|
|
29
|
+
if new_id <= last_time_id:
|
|
30
|
+
new_id = last_time_id + 1
|
|
31
|
+
last_time_id = new_id
|
|
32
|
+
return new_id
|
|
33
|
+
|
|
34
|
+
# 雪花算法,适合大数据量分布式应用
|
|
35
|
+
# import snowflake.client
|
|
36
|
+
# def get_long_id_by_snowflake():
|
|
37
|
+
# snowflake.client.Generator
|
|
38
|
+
# pass
|
|
39
|
+
|
|
40
|
+
# print(type(get_long_id_by_time()),get_long_id_by_time())
|
|
File without changes
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import io
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Union, Optional
|
|
5
|
+
from PIL import Image
|
|
6
|
+
import olefile #部分图像格式用到,必须保留
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class OutputFormat:
|
|
10
|
+
"""定义输出格式枚举"""
|
|
11
|
+
MARKDOWN_ALT = 'MARKDOWN_ALT' # 
|
|
12
|
+
MARKDOWN_NO_ALT = 'MARKDOWN_NO_ALT' # 
|
|
13
|
+
RAW_BASE64 = 'RAW_BASE64' # 纯 Base64 字符串
|
|
14
|
+
MIME_BASE64 = 'MIME_BASE64' # data:image/xxx;base64,...
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Image2Base64:
|
|
18
|
+
"""
|
|
19
|
+
智能图像转 Base64 工具类
|
|
20
|
+
特性:支持 HEIC/PSD/SVG、智能缩放、二分法压缩、透明度自动合成
|
|
21
|
+
"""
|
|
22
|
+
_SUPPORTED_EXTS = {
|
|
23
|
+
'.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp',
|
|
24
|
+
'.bmp', '.tiff', '.tif', '.ico', '.psd', '.heic', '.heif'
|
|
25
|
+
}
|
|
26
|
+
_NEED_CONVERSION = {'.bmp', '.tiff', '.tif', '.ico', '.psd', '.heic', '.heif'}
|
|
27
|
+
_MIME_MAP = {
|
|
28
|
+
'.png': 'image/png', '.jpg': 'image/jpeg', '.jpeg': 'image/jpeg',
|
|
29
|
+
'.gif': 'image/gif', '.svg': 'image/svg+xml', '.webp': 'image/webp',
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def convert_file(
|
|
34
|
+
cls,
|
|
35
|
+
image_path: Union[str, Path],
|
|
36
|
+
max_dim: int = 1200, # 限制长边像素
|
|
37
|
+
max_kb: int = 200, # 目标最大体积 (KB)
|
|
38
|
+
force_webp: bool = True, # 强制转为 WebP (推荐)
|
|
39
|
+
out_format: str = OutputFormat.MARKDOWN_ALT,
|
|
40
|
+
quality: int = 80 # 初始压缩质量
|
|
41
|
+
) -> str:
|
|
42
|
+
# 0. 前置校验
|
|
43
|
+
path = Path(image_path)
|
|
44
|
+
if not path.exists():
|
|
45
|
+
raise FileNotFoundError(f"Missing file: {path}")
|
|
46
|
+
|
|
47
|
+
ext = path.suffix.lower()
|
|
48
|
+
if ext not in cls._SUPPORTED_EXTS:
|
|
49
|
+
supported_list = ", ".join(sorted(cls._SUPPORTED_EXTS))
|
|
50
|
+
raise ValueError(f"Unsupported extension '{ext}'. Supported: {supported_list}")
|
|
51
|
+
|
|
52
|
+
final_data: bytes = b""
|
|
53
|
+
mime_type: str = ""
|
|
54
|
+
|
|
55
|
+
# 1. 矢量图处理 (SVG) - 保持矢量特性,不走 Pillow
|
|
56
|
+
if ext == '.svg':
|
|
57
|
+
with open(path, 'rb') as f:
|
|
58
|
+
final_data = f.read()
|
|
59
|
+
mime_type = "image/svg+xml"
|
|
60
|
+
|
|
61
|
+
# 2. 位图处理流程
|
|
62
|
+
else:
|
|
63
|
+
# HEIC 特殊加载支持
|
|
64
|
+
if ext in ['.heic', '.heif']:
|
|
65
|
+
try:
|
|
66
|
+
from pillow_heif import register_heif_opener
|
|
67
|
+
register_heif_opener()
|
|
68
|
+
except ImportError:
|
|
69
|
+
raise ImportError("Please install 'pillow-heif' to support HEIC files.")
|
|
70
|
+
|
|
71
|
+
with Image.open(path) as img:
|
|
72
|
+
# 确定目标格式与 MIME
|
|
73
|
+
final_data, mime_type = cls.convert_image(img, max_dim,max_kb,force_webp,quality )
|
|
74
|
+
# 3. 输出格式化
|
|
75
|
+
b64_str = base64.b64encode(final_data).decode('utf-8')
|
|
76
|
+
|
|
77
|
+
if out_format == OutputFormat.RAW_BASE64:
|
|
78
|
+
return b64_str
|
|
79
|
+
|
|
80
|
+
data_uri = f"data:{mime_type};base64,{b64_str}"
|
|
81
|
+
|
|
82
|
+
if out_format == OutputFormat.MIME_BASE64:
|
|
83
|
+
return data_uri
|
|
84
|
+
elif out_format == OutputFormat.MARKDOWN_NO_ALT:
|
|
85
|
+
return f""
|
|
86
|
+
else: # 默认 MARKDOWN_ALT
|
|
87
|
+
return f""
|
|
88
|
+
|
|
89
|
+
@classmethod
|
|
90
|
+
def convert_image(
|
|
91
|
+
cls,
|
|
92
|
+
img: Image.Image,
|
|
93
|
+
max_dim: int = 1200,
|
|
94
|
+
max_kb: int = 200,
|
|
95
|
+
force_webp: bool = True,
|
|
96
|
+
quality: int = 80
|
|
97
|
+
) -> tuple[bytes, str]:
|
|
98
|
+
"""
|
|
99
|
+
核心处理逻辑:接收 Pillow Image 对象,返回压缩后的字节流和 MIME 类型
|
|
100
|
+
"""
|
|
101
|
+
# 1. 确定格式
|
|
102
|
+
if force_webp:
|
|
103
|
+
target_format, mime_type = "WEBP", "image/webp"
|
|
104
|
+
else:
|
|
105
|
+
# 默认转 PNG 保证兼容性
|
|
106
|
+
target_format, mime_type = "PNG", "image/png"
|
|
107
|
+
|
|
108
|
+
# 2. 透明度处理
|
|
109
|
+
if target_format in ["JPEG", "WEBP"] or img.mode not in ["RGB", "RGBA", "L"]:
|
|
110
|
+
if img.mode in ["RGBA", "LA", "P"]:
|
|
111
|
+
background = Image.new('RGB', img.size, (255, 255, 255))
|
|
112
|
+
temp_rgba = img.convert('RGBA')
|
|
113
|
+
background.paste(temp_rgba, mask=temp_rgba.split()[-1])
|
|
114
|
+
img = background
|
|
115
|
+
else:
|
|
116
|
+
img = img.convert('RGB')
|
|
117
|
+
|
|
118
|
+
# 3. 等比缩放
|
|
119
|
+
if max(img.width, img.height) > max_dim:
|
|
120
|
+
img.thumbnail((max_dim, max_dim), Image.Resampling.LANCZOS)
|
|
121
|
+
|
|
122
|
+
# 4. 二分法压缩
|
|
123
|
+
raw_target_size = (max_kb * 1024) * 0.75
|
|
124
|
+
if target_format == "PNG":
|
|
125
|
+
buf = io.BytesIO()
|
|
126
|
+
img.save(buf, format="PNG", optimize=True)
|
|
127
|
+
return buf.getvalue(), mime_type
|
|
128
|
+
else:
|
|
129
|
+
low, high = 30, quality
|
|
130
|
+
best_data = None
|
|
131
|
+
for _ in range(3):
|
|
132
|
+
mid = (low + high) // 2
|
|
133
|
+
buf = io.BytesIO()
|
|
134
|
+
img.save(buf, format=target_format, quality=mid, optimize=True)
|
|
135
|
+
data = buf.getvalue()
|
|
136
|
+
if len(data) <= raw_target_size:
|
|
137
|
+
best_data, low = data, mid + 5
|
|
138
|
+
else:
|
|
139
|
+
high = mid - 5
|
|
140
|
+
return (best_data if best_data else buf.getvalue()), mime_type
|
|
141
|
+
|
|
142
|
+
@classmethod
|
|
143
|
+
def batch_convert(
|
|
144
|
+
cls,
|
|
145
|
+
input_dir: Union[str, Path],
|
|
146
|
+
output_md: Optional[Union[str, Path]] = None,
|
|
147
|
+
max_dim: int = 1200,
|
|
148
|
+
max_kb: int = 200,
|
|
149
|
+
force_webp: bool = True,
|
|
150
|
+
out_format: str = OutputFormat.MARKDOWN_ALT,
|
|
151
|
+
quality: int = 80
|
|
152
|
+
) -> dict:
|
|
153
|
+
"""
|
|
154
|
+
批量转换目录下所有支持的图片文件
|
|
155
|
+
:param input_dir: 输入目录路径
|
|
156
|
+
:param output_md: 可选,将结果保存到的 Markdown 文件路径
|
|
157
|
+
:return: 包含 {文件名: Base64结果} 的字典
|
|
158
|
+
"""
|
|
159
|
+
input_path = Path(input_dir)
|
|
160
|
+
if not input_path.is_dir():
|
|
161
|
+
raise ValueError(f"'{input_dir}' is not a valid directory.")
|
|
162
|
+
|
|
163
|
+
results = {}
|
|
164
|
+
# 获取所有支持的文件并排序
|
|
165
|
+
files = sorted([f for f in input_path.iterdir() if f.suffix.lower() in cls._SUPPORTED_EXTS])
|
|
166
|
+
|
|
167
|
+
if not files:
|
|
168
|
+
print(f"No supported images found in {input_dir}")
|
|
169
|
+
return results
|
|
170
|
+
|
|
171
|
+
print(f"Starting batch conversion of {len(files)} images...")
|
|
172
|
+
|
|
173
|
+
for file_path in files:
|
|
174
|
+
try:
|
|
175
|
+
# 调用单文件转换方法
|
|
176
|
+
res = cls.convert_file(
|
|
177
|
+
image_path=file_path,
|
|
178
|
+
max_dim=max_dim,
|
|
179
|
+
max_kb=max_kb,
|
|
180
|
+
force_webp=force_webp,
|
|
181
|
+
out_format=out_format,
|
|
182
|
+
quality=quality
|
|
183
|
+
)
|
|
184
|
+
results[file_path.name] = res
|
|
185
|
+
print(f"✓ Converted: {file_path.name}")
|
|
186
|
+
except Exception as e:
|
|
187
|
+
print(f"✗ Failed: {file_path.name} | Error: {e}")
|
|
188
|
+
|
|
189
|
+
# 如果指定了输出文件,则写入 Markdown
|
|
190
|
+
if output_md:
|
|
191
|
+
cls._save_to_markdown(results, output_md)
|
|
192
|
+
|
|
193
|
+
return results
|
|
194
|
+
|
|
195
|
+
@staticmethod
|
|
196
|
+
def _save_to_markdown(results: dict, output_path: Union[str, Path]):
|
|
197
|
+
"""内部辅助:将结果写入文件"""
|
|
198
|
+
output_path = Path(output_path)
|
|
199
|
+
with open(output_path, 'w', encoding='utf-8') as f:
|
|
200
|
+
f.write(f"# Image Conversion Report\n\n")
|
|
201
|
+
f.write(f"Total processed: {len(results)}\n\n---\n\n")
|
|
202
|
+
for name, content in results.items():
|
|
203
|
+
f.write(f"### {name}\n\n")
|
|
204
|
+
# 如果内容本身不是 markdown 格式,则包裹一下
|
|
205
|
+
if not content.startswith('!['):
|
|
206
|
+
f.write(f"```text\n{content}\n```\n\n")
|
|
207
|
+
else:
|
|
208
|
+
f.write(f"{content}\n\n")
|
|
209
|
+
print(f"\n★ All results saved to: {output_path.absolute()}")
|
|
210
|
+
|
|
211
|
+
if __name__ == '__main__':
|
|
212
|
+
Image2Base64.batch_convert(
|
|
213
|
+
input_dir= r"D:\测试目录_全面\img",
|
|
214
|
+
output_md="gallery.md",
|
|
215
|
+
max_kb=150, # 每个图片限制在 150KB 以内
|
|
216
|
+
force_webp=True,
|
|
217
|
+
out_format=OutputFormat.MARKDOWN_ALT
|
|
218
|
+
)
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
indexdoc_converter/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
+
indexdoc_converter/docx_to_md.py,sha256=nSMqmx8Bb-mnBtqYu1XFujJouYKYaoCh2rgusFvWhIU,2285
|
|
3
|
+
indexdoc_converter/excel_to_md.py,sha256=OIGnLDVu0Lq9A_hnGa_fPAXTHiKIJOZAfqCIgwn2SwQ,27186
|
|
4
|
+
indexdoc_converter/html_to_md.py,sha256=XJ54WcU2VOtQasAEp4BqxiBhAY2uG_IVAb9-2qd_uB4,17243
|
|
5
|
+
indexdoc_converter/pptx_to_md.py,sha256=C4YYSKMA7Qu58-FYo1fhN-Hp1zKUapcjteeVcyalNn4,7826
|
|
6
|
+
indexdoc_converter/utils/FileUtil.py,sha256=N0lTD50OkzidRpHP3ucb1EJU2Hn1q5OPxqz9iPBqEL8,1390
|
|
7
|
+
indexdoc_converter/utils/IDUtil.py,sha256=UKGbnLEkvUyMBIkfWRB8C2bZ-yvofRcKM945Z21P_dE,1018
|
|
8
|
+
indexdoc_converter/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
9
|
+
indexdoc_converter/utils/img_to_base64.py,sha256=KPhslxfWjze9KdKsQYqLApWWT0B4raav2Zz1MrwlziA,8287
|
|
10
|
+
indexdoc_converter-0.2.2.dist-info/METADATA,sha256=002u_GLvR5T6-WQVkUtI8RF2M_sVjRTzxpoFweuMEsU,3065
|
|
11
|
+
indexdoc_converter-0.2.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
12
|
+
indexdoc_converter-0.2.2.dist-info/top_level.txt,sha256=Tqm-Q3tVPYwA5wyVSClNovUi1IwLvw6M2vlhi-WbNMs,19
|
|
13
|
+
indexdoc_converter-0.2.2.dist-info/RECORD,,
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
indexdoc_converter/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
2
|
-
indexdoc_converter/docx_to_md.py,sha256=nSMqmx8Bb-mnBtqYu1XFujJouYKYaoCh2rgusFvWhIU,2285
|
|
3
|
-
indexdoc_converter/excel_to_md.py,sha256=oK2VFTmpCYqK-uXrCiaCXXWE_Q0Hbn5xTOPeMLKiE2U,27167
|
|
4
|
-
indexdoc_converter/html_to_md.py,sha256=CUX0TVoSdHAUybC4FZEz7Z_-verEV3HU5gp8IUFlgVE,17224
|
|
5
|
-
indexdoc_converter/pptx_to_md.py,sha256=IeVsQ22CE8bgfsBZT5lre8LghUDYNaV9rgbhrn9PQGw,2944
|
|
6
|
-
indexdoc_converter-0.2.0.dist-info/METADATA,sha256=fRAoj62nyd3NF2_-PGQwpLe1wAVfxI6ngJsjSdUytsQ,3065
|
|
7
|
-
indexdoc_converter-0.2.0.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
8
|
-
indexdoc_converter-0.2.0.dist-info/top_level.txt,sha256=Tqm-Q3tVPYwA5wyVSClNovUi1IwLvw6M2vlhi-WbNMs,19
|
|
9
|
-
indexdoc_converter-0.2.0.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|