file-converter
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseFile Converter
文件转换器
Overview
概述
Convert files between formats across three categories: documents, data files, and images. Generate Python code dynamically for each conversion request, selecting appropriate libraries and handling edge cases.
可在文档、数据文件和图片三大类别之间进行文件格式转换。针对每个转换请求动态生成Python代码,选择合适的库并处理边缘情况。
Conversion Categories
转换类别
Documents
文档
| From | To | Recommended Library |
|---|---|---|
| Markdown | HTML | |
| HTML | Markdown | |
| HTML | | |
| Text | | |
| DOCX | Markdown | |
| DOCX | | |
| Markdown | Convert via HTML first, then to PDF |
| 源格式 | 目标格式 | 推荐库 |
|---|---|---|
| Markdown | HTML | |
| HTML | Markdown | |
| HTML | | |
| 文本 | | |
| DOCX | Markdown | |
| DOCX | | |
| Markdown | 先转换为HTML,再转为PDF |
Data Files
数据文件
| From | To | Recommended Library |
|---|---|---|
| JSON | YAML | |
| YAML | JSON | |
| JSON | CSV | |
| CSV | JSON | |
| JSON | TOML | |
| XML | JSON | |
| JSON | XML | |
| 源格式 | 目标格式 | 推荐库 |
|---|---|---|
| JSON | YAML | |
| YAML | JSON | |
| JSON | CSV | |
| CSV | JSON | |
| JSON | TOML | |
| XML | JSON | |
| JSON | XML | |
Images
图片
| From | To | Recommended Library |
|---|---|---|
| PNG/JPG/WebP/GIF | Any raster | |
| SVG | PNG/JPG | |
| PNG | SVG | |
| 源格式 | 目标格式 | 推荐库 |
|---|---|---|
| PNG/JPG/WebP/GIF | 任何光栅格式 | |
| SVG | PNG/JPG | |
| PNG | SVG | |
Workflow
工作流程
- Identify source format (from file extension or user statement)
- Identify target format
- Check for format-specific guidance
references/ - Generate conversion code using recommended library
- Handle edge cases (encoding, transparency, nested structures)
- Execute conversion and report results
- 识别源格式(通过文件扩展名或用户描述)
- 识别目标格式
- 查看获取格式特定指南
references/ - 使用推荐库生成转换代码
- 处理边缘情况(编码、透明度、嵌套结构)
- 执行转换并报告结果
Quick Patterns
快速示例
Data: JSON to YAML
数据:JSON转YAML
python
import json
import yaml
with open("input.json") as f:
data = json.load(f)
with open("output.yaml", "w") as f:
yaml.dump(data, f, default_flow_style=False, allow_unicode=True)python
import json
import yaml
with open("input.json") as f:
data = json.load(f)
with open("output.yaml", "w") as f:
yaml.dump(data, f, default_flow_style=False, allow_unicode=True)Data: CSV to JSON
数据:CSV转JSON
python
import csv
import json
with open("input.csv") as f:
reader = csv.DictReader(f)
data = list(reader)
with open("output.json", "w") as f:
json.dump(data, f, indent=2)python
import csv
import json
with open("input.csv") as f:
reader = csv.DictReader(f)
data = list(reader)
with open("output.json", "w") as f:
json.dump(data, f, indent=2)Document: Markdown to HTML
文档:Markdown转HTML
python
import markdown
with open("input.md") as f:
md_content = f.read()
html = markdown.markdown(md_content, extensions=["tables", "fenced_code"])
with open("output.html", "w") as f:
f.write(html)python
import markdown
with open("input.md") as f:
md_content = f.read()
html = markdown.markdown(md_content, extensions=["tables", "fenced_code"])
with open("output.html", "w") as f:
f.write(html)Image: PNG to WebP
图片:PNG转WebP
python
from PIL import Image
img = Image.open("input.png")
img.save("output.webp", "WEBP", quality=85)python
from PIL import Image
img = Image.open("input.png")
img.save("output.webp", "WEBP", quality=85)Image: SVG to PNG
图片:SVG转PNG
python
import cairosvg
cairosvg.svg2png(url="input.svg", write_to="output.png", scale=2)python
import cairosvg
cairosvg.svg2png(url="input.svg", write_to="output.png", scale=2)Resources
资源
Detailed guidance for complex conversions is in :
references/- - PDF handling, encoding issues, styling preservation
references/document-conversions.md - - Schema handling, type coercion, nested structures
references/data-conversions.md - - Quality settings, transparency, color profiles
references/image-conversions.md
Consult these references when handling edge cases or when the user has specific quality/fidelity requirements.
复杂转换的详细指南位于目录下:
references/- - PDF处理、编码问题、样式保留
references/document-conversions.md - - 模式处理、类型转换、嵌套结构
references/data-conversions.md - - 质量设置、透明度、颜色配置文件
references/image-conversions.md
处理边缘情况或用户有特定质量/保真度要求时,请参考这些文档。