增加translate_bytes 方法

This commit is contained in:
xunbu
2025-05-11 08:33:59 +08:00
parent 2bc8749441
commit 69bc0aea55
3 changed files with 64 additions and 44 deletions

88
.idea/workspace.xml generated
View File

@@ -6,7 +6,8 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="6b18b44a-df57-4212-a857-9e291ebe5dd2" name="更改" comment=""> <list default="true" id="6b18b44a-df57-4212-a857-9e291ebe5dd2" name="更改" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/uv.lock" beforeDir="false" afterPath="$PROJECT_DIR$/uv.lock" afterDir="false" /> <change beforePath="$PROJECT_DIR$/docutranslate/translater.py" beforeDir="false" afterPath="$PROJECT_DIR$/docutranslate/translater.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pyproject.toml" beforeDir="false" afterPath="$PROJECT_DIR$/pyproject.toml" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -32,47 +33,47 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"DefaultHtmlFileTemplate": "HTML File", &quot;DefaultHtmlFileTemplate&quot;: &quot;HTML File&quot;,
"JavaScript 调试.output.html (1).executor": "Run", &quot;JavaScript 调试.output.html (1).executor&quot;: &quot;Run&quot;,
"JavaScript 调试.output.html.executor": "Run", &quot;JavaScript 调试.output.html.executor&quot;: &quot;Run&quot;,
"JavaScript 调试.regex_中文.html.executor": "Run", &quot;JavaScript 调试.regex_中文.html.executor&quot;: &quot;Run&quot;,
"JavaScript 调试.test2.html.executor": "Run", &quot;JavaScript 调试.test2.html.executor&quot;: &quot;Run&quot;,
"JavaScript 调试.test2_英文.html.executor": "Run", &quot;JavaScript 调试.test2_英文.html.executor&quot;: &quot;Run&quot;,
"JavaScript 调试.test4-1_中文.html.executor": "Run", &quot;JavaScript 调试.test4-1_中文.html.executor&quot;: &quot;Run&quot;,
"JavaScript 调试.互联网认证授权机制.html.executor": "Run", &quot;JavaScript 调试.互联网认证授权机制.html.executor&quot;: &quot;Run&quot;,
"JavaScript 调试.互联网认证授权机制_英文.html.executor": "Run", &quot;JavaScript 调试.互联网认证授权机制_英文.html.executor&quot;: &quot;Run&quot;,
"JavaScript 调试.毕业论文_英文.html.executor": "Run", &quot;JavaScript 调试.毕业论文_英文.html.executor&quot;: &quot;Run&quot;,
"ModuleVcsDetector.initialDetectionPerformed": "true", &quot;ModuleVcsDetector.initialDetectionPerformed&quot;: &quot;true&quot;,
"Python 测试.Python 测试 (markdown_mask.py 内).executor": "Run", &quot;Python 测试.Python 测试 (markdown_mask.py 内).executor&quot;: &quot;Run&quot;,
"Python 测试.markdown_mask.Test.test_basic_link_masking 的 Python 测试.executor": "Run", &quot;Python 测试.markdown_mask.Test.test_basic_link_masking 的 Python 测试.executor&quot;: &quot;Run&quot;,
"Python.PDFtranslater (1).executor": "Run", &quot;Python.PDFtranslater (1).executor&quot;: &quot;Run&quot;,
"Python.PDFtranslater (2).executor": "Run", &quot;Python.PDFtranslater (2).executor&quot;: &quot;Run&quot;,
"Python.agent.executor": "Debug", &quot;Python.agent.executor&quot;: &quot;Debug&quot;,
"Python.agent_utils.executor": "Run", &quot;Python.agent_utils.executor&quot;: &quot;Run&quot;,
"Python.convert.executor": "Run", &quot;Python.convert.executor&quot;: &quot;Run&quot;,
"Python.markdown_splitter.executor": "Debug", &quot;Python.markdown_splitter.executor&quot;: &quot;Debug&quot;,
"Python.markdown_utils.executor": "Run", &quot;Python.markdown_utils.executor&quot;: &quot;Run&quot;,
"Python.test.executor": "Run", &quot;Python.test.executor&quot;: &quot;Run&quot;,
"Python.test1.executor": "Run", &quot;Python.test1.executor&quot;: &quot;Run&quot;,
"Python.test2.executor": "Run", &quot;Python.test2.executor&quot;: &quot;Run&quot;,
"Python.test3.executor": "Run", &quot;Python.test3.executor&quot;: &quot;Run&quot;,
"Python.test4.executor": "Run", &quot;Python.test4.executor&quot;: &quot;Run&quot;,
"Python.translater.executor": "Run", &quot;Python.translater.executor&quot;: &quot;Run&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"RunOnceActivity.git.unshallow": "true", &quot;RunOnceActivity.git.unshallow&quot;: &quot;true&quot;,
"git-widget-placeholder": "main", &quot;git-widget-placeholder&quot;: &quot;main&quot;,
"last_opened_file_path": "C:/Users/jxgm/Desktop/FileTranslate/tests", &quot;last_opened_file_path&quot;: &quot;C:/Users/jxgm/Desktop/FileTranslate/tests&quot;,
"node.js.detected.package.eslint": "true", &quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
"node.js.detected.package.tslint": "true", &quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
"node.js.selected.package.eslint": "(autodetect)", &quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
"node.js.selected.package.tslint": "(autodetect)", &quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
"nodejs_package_manager_path": "npm", &quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
"settings.editor.selected.configurable": "preferences.general", &quot;settings.editor.selected.configurable&quot;: &quot;preferences.general&quot;,
"vue.rearranger.settings.migration": "true" &quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
} }
}]]></component> }</component>
<component name="RecentsManager"> <component name="RecentsManager">
<key name="CopyFile.RECENT_KEYS"> <key name="CopyFile.RECENT_KEYS">
<recent name="C:\Users\jxgm\Desktop\FileTranslate\tests" /> <recent name="C:\Users\jxgm\Desktop\FileTranslate\tests" />
@@ -383,7 +384,10 @@
<workItem from="1746851812490" duration="334000" /> <workItem from="1746851812490" duration="334000" />
<workItem from="1746864516470" duration="2291000" /> <workItem from="1746864516470" duration="2291000" />
<workItem from="1746877808294" duration="1364000" /> <workItem from="1746877808294" duration="1364000" />
<workItem from="1746882518324" duration="4599000" /> <workItem from="1746882518324" duration="4691000" />
<workItem from="1746887311759" duration="14000" />
<workItem from="1746894355924" duration="651000" />
<workItem from="1746923054682" duration="383000" />
</task> </task>
<servers /> <servers />
</component> </component>

View File

@@ -72,7 +72,7 @@ class FileTranslater:
} }
return result return result
def read(self, name: str, file: bytes, formula=False, code=False, save=False, def read_bytes(self, name: str, file: bytes, formula=False, code=False, save=False,
save_format: Literal["markdown", "html"] = "markdown", refine=False, save_format: Literal["markdown", "html"] = "markdown", refine=False,
refine_agent: Agent | None = None): refine_agent: Agent | None = None):
ds = DocumentStream(name=name, stream=BytesIO(file)) ds = DocumentStream(name=name, stream=BytesIO(file))
@@ -262,3 +262,19 @@ class FileTranslater:
filename = f"{file_path.stem}_{to_lang}.html" filename = f"{file_path.stem}_{to_lang}.html"
self.save_as_html(filename=filename, output_dir=output_dir) self.save_as_html(filename=filename, output_dir=output_dir)
return self return self
def translate_bytes(self, name:str,file: bytes, to_lang="中文", output_dir="./output",
formula=False,
code=False, output_format: Literal["markdown", "html"] = "markdown", refine=False,
refine_agent: Agent | None = None, translate_agent: Agent | None = None):
self.read_bytes(name=name,file=file, formula=formula, code=code)
if refine:
self.refine_markdown_by_agent(refine_agent)
self.translate_markdown_by_agent(translate_agent, to_lang=to_lang)
if output_format == "markdown":
filename = f"{name}_{to_lang}.md"
self.save_as_markdown(filename=filename, output_dir=output_dir)
elif output_format == "html":
filename = f"{name}_{to_lang}.html"
self.save_as_html(filename=filename, output_dir=output_dir)
return self

View File

@@ -1,6 +1,6 @@
[project] [project]
name = "docutranslate" name = "docutranslate"
version = "0.1.6" version = "0.1.7.dev"
description = "文件翻译工具" description = "文件翻译工具"
readme = "README.md" readme = "README.md"
requires-python = ">=3.10" requires-python = ">=3.10"