This commit is contained in:
2025-08-20 14:02:58 +08:00
2 changed files with 74 additions and 58 deletions

View File

@@ -93,50 +93,53 @@ def get_docx_content_by_pandoc(docx_file):
resize_images_in_directory('./static/Images/' + md5_value + '/media')
# 读取然后修改内容,输出到新的文件
img_idx = 0 # 图片索引
with open(temp_markdown, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
# 跳过图片高度描述行
if line.startswith('height=') and (line.endswith('in"}') or line.endswith('in"')):
continue
# height="1.91044072615923in"
# 使用find()方法安全地检查图片模式
is_img = line.find("![](") >= 0 and (
line.find(".png") > 0 or
line.find(".jpg") > 0 or
line.find(".jpeg") > 0
)
if is_img:
# ![](media/image3.png){width="3.1251607611548557in"
# height="3.694634733158355in"}
# ![](../static/Images/01b20e04085e406ea5375791da58a60f/media/image3.png){width="3.1251607611548557in"
pos = line.find(")")
q = line[:pos + 1]
q = q.replace("./static", ".")
# Modify by Kalman.CHENG ☆: 增加逻辑对图片路径处理,在(和static之间加上/
left_idx = line.find("(")
static_idx = line.find("static")
if left_idx == -1 or static_idx == -1 or left_idx > static_idx:
print("路径中不包含(+~+static的已知格式")
else:
between_content = q[left_idx+1:static_idx].strip()
if between_content:
q = q[:left_idx+1] + '\\' + q[static_idx:]
if os.path.exists(temp_markdown):
with open(temp_markdown, 'r', encoding='utf-8') as f:
for line in f:
line = line.strip()
if not line:
continue
# 跳过图片高度描述行
if line.startswith('height=') and (line.endswith('in"}') or line.endswith('in"')):
continue
# height="1.91044072615923in"
# 使用find()方法安全地检查图片模式
is_img = line.find("![](") >= 0 and (
line.find(".png") > 0 or
line.find(".jpg") > 0 or
line.find(".jpeg") > 0
)
if is_img:
# ![](media/image3.png){width="3.1251607611548557in"
# height="3.694634733158355in"}
# ![](../static/Images/01b20e04085e406ea5375791da58a60f/media/image3.png){width="3.1251607611548557in"
pos = line.find(")")
q = line[:pos + 1]
q = q.replace("./static", ".")
# Modify by Kalman.CHENG ☆: 增加逻辑对图片路径处理,在(和static之间加上/
left_idx = line.find("(")
static_idx = line.find("static")
if left_idx == -1 or static_idx == -1 or left_idx > static_idx:
print("路径中不包含(+~+static的已知格式")
else:
q = q[:static_idx] + '\\' + q[static_idx:]
print(f"q3{q}")
#q = q[4:-1]
#q='<img src="'+q+'" alt="我是图片">'
img_idx += 1
content += q + "\n"
else:
content += line.strip().replace("**", "") + "\n"
content = content.replace("\phantom", "")
# 将content回写到markdown文件
with open(temp_markdown, 'w', encoding='utf-8') as f:
f.write(content)
# 删除临时文件 output_file
# os.remove(temp_markdown)
return content.replace("\n\n", "\n").replace("\\", "/")
between_content = q[left_idx+1:static_idx].strip()
if between_content:
q = q[:left_idx+1] + '\\' + q[static_idx:]
else:
q = q[:static_idx] + '\\' + q[static_idx:]
print(f"q3{q}")
#q = q[4:-1]
#q='<img src="'+q+'" alt="我是图片">'
img_idx += 1
content += q + "\n"
else:
content += line.strip().replace("**", "") + "\n"
content = content.replace("\phantom", "")
# 将content回写到markdown文件
with open(temp_markdown, 'w', encoding='utf-8') as f:
f.write(content)
# 删除临时文件 output_file
# os.remove(temp_markdown)
return content.replace("\n\n", "\n").replace("\\", "/")
else:
return None