支持csv

This commit is contained in:
xunbu
2025-08-23 18:07:18 +08:00
parent 111599926d
commit 1a27efb3cd
4 changed files with 148 additions and 143 deletions

View File

@@ -33,7 +33,7 @@ from docutranslate.workflow.html_workflow import HtmlWorkflow, HtmlWorkflowConfi
# --- HTML WORKFLOW IMPORT END ---
from docutranslate.workflow.interfaces import DocxExportable, EpubExportable
from docutranslate.workflow.interfaces import HTMLExportable, MDFormatsExportable, TXTExportable, JsonExportable, \
XlsxExportable, SrtExportable
XlsxExportable, SrtExportable, CsvExportable
from docutranslate.workflow.json_workflow import JsonWorkflow, JsonWorkflowConfig
from docutranslate.workflow.md_based_workflow import MarkdownBasedWorkflow, MarkdownBasedWorkflowConfig
from docutranslate.workflow.srt_workflow import SrtWorkflow, SrtWorkflowConfig
@@ -849,6 +849,25 @@ async def service_release_task(task_id: str):
}
}
},
"completed_xlsx": {
"summary": "已完成 (XLSX)",
"value": {
"task_id": "d7e8f9a0",
"is_processing": False,
"status_message": "翻译成功!用时 18.99 秒。",
"error_flag": False,
"download_ready": True,
"original_filename_stem": "sales_data",
"original_filename": "sales_data.xlsx",
"task_start_time": 1678889600.0,
"task_end_time": 1678889618.99,
"downloads": {
"xlsx": "/service/download/d7e8f9a0/xlsx",
"csv": "/service/download/d7e8f9a0/csv",
"html": "/service/download/d7e8f9a0/html"
}
}
},
"completed_docx": {
"summary": "已完成 (DOCX)",
"value": {
@@ -929,6 +948,8 @@ async def service_get_status(
downloads["json"] = f"/service/download/{task_id}/json"
if isinstance(workflow, XlsxExportable):
downloads["xlsx"] = f"/service/download/{task_id}/xlsx"
if isinstance(workflow, CsvExportable):
downloads["csv"] = f"/service/download/{task_id}/csv"
if isinstance(workflow, DocxExportable):
downloads["docx"] = f"/service/download/{task_id}/docx"
if isinstance(workflow, SrtExportable):
@@ -969,7 +990,7 @@ async def service_get_logs(task_id: str):
return JSONResponse(content={"logs": new_logs})
FileType = Literal["markdown", "markdown_zip", "html", "txt", "json", "xlsx", "docx", "srt", "epub"]
FileType = Literal["markdown", "markdown_zip", "html", "txt", "json", "xlsx", "csv", "docx", "srt", "epub"]
async def _get_content_from_workflow(task_id: str, file_type: FileType) -> tuple[bytes, str, str]:
@@ -1036,6 +1057,9 @@ async def _get_content_from_workflow(task_id: str, file_type: FileType) -> tuple
elif file_type == 'xlsx' and isinstance(workflow, XlsxExportable):
content_bytes = await asyncio.to_thread(workflow.export_to_xlsx)
media_type, filename = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", f"{filename_stem}_translated.xlsx"
elif file_type == 'csv' and isinstance(workflow, CsvExportable):
content_bytes = await asyncio.to_thread(workflow.export_to_csv)
media_type, filename = "text/csv; charset=utf-8", f"{filename_stem}_translated.csv"
elif file_type == 'docx' and isinstance(workflow, DocxExportable):
content_bytes = await asyncio.to_thread(workflow.export_to_docx)
media_type, filename = "application/vnd.openxmlformats-officedocument.wordprocessingml.document", f"{filename_stem}_translated.docx"
@@ -1065,6 +1089,7 @@ async def _get_content_from_workflow(task_id: str, file_type: FileType) -> tuple
"text/html; charset=utf-8": {"schema": {"type": "string"}},
"text/markdown; charset=utf-8": {"schema": {"type": "string"}},
"text/plain; charset=utf-8": {"schema": {"type": "string"}},
"text/csv; charset=utf-8": {"schema": {"type": "string"}},
"application/zip": {"schema": {"type": "string", "format": "binary"}},
"application/json": {"schema": {"type": "string", "format": "binary"}},
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
@@ -1082,7 +1107,7 @@ async def _get_content_from_workflow(task_id: str, file_type: FileType) -> tuple
async def service_download_file(
task_id: str = FastApiPath(..., description="已完成任务的ID", examples=["b2865b93"]),
file_type: FileType = FastApiPath(..., description="要下载的文件类型。",
examples=["html", "json", "docx", "srt", "epub"])
examples=["html", "json", "csv", "docx", "srt", "epub"])
):
content, media_type, filename = await _get_content_from_workflow(task_id, file_type)
headers = {"Content-Disposition": f"attachment; filename*=UTF-8''{quote(filename, safe='', encoding='utf-8')}"}
@@ -1135,7 +1160,7 @@ async def service_download_file(
async def service_content(
task_id: str = FastApiPath(..., description="已完成任务的ID", examples=["b2865b93"]),
file_type: FileType = FastApiPath(..., description="要获取内容的文件类型。",
examples=["html", "json", "docx", "srt", "epub"])
examples=["html", "json", "csv", "docx", "srt", "epub"])
):
content, _, filename = await _get_content_from_workflow(task_id, file_type)

View File

@@ -113,8 +113,8 @@ if __name__ == '__main__':
from pathlib import Path
# d = Document.from_path(r"C:\Users\jxgm\Desktop\mcp文件夹\学习笔记\互联网认证授权机制\互联网认证授权机制.md")
d = Document.from_path(r"C:\Users\jxgm\Desktop\matrixcookbook_translated.md")
d = Document.from_path(r"C:\Users\jxgm\Desktop\matrixcalc_translated.md")
# d = Document.from_path(r"C:\Users\jxgm\Downloads\3a8d8999-3e9d-4f32-a32c-5b0830bb4320\full.md")
exporter = MD2HTMLExporter()
d1 = exporter.export(d)
path = Path(r"C:\Users\jxgm\Desktop\a.html")

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long