file-converter

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

File 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

文档

FromToRecommended Library
MarkdownHTML
markdown
or
mistune
HTMLMarkdown
markdownify
or
html2text
HTMLPDF
weasyprint
or
pdfkit
(requires wkhtmltopdf)
PDFText
pypdf
or
pdfplumber
DOCXMarkdown
mammoth
DOCXPDF
docx2pdf
(Windows/macOS) or LibreOffice CLI
MarkdownPDFConvert via HTML first, then to PDF
源格式目标格式推荐库
MarkdownHTML
markdown
mistune
HTMLMarkdown
markdownify
html2text
HTMLPDF
weasyprint
pdfkit
(需要wkhtmltopdf)
PDF文本
pypdf
pdfplumber
DOCXMarkdown
mammoth
DOCXPDF
docx2pdf
(Windows/macOS)或LibreOffice CLI
MarkdownPDF先转换为HTML,再转为PDF

Data Files

数据文件

FromToRecommended Library
JSONYAML
pyyaml
YAMLJSON
pyyaml
JSONCSV
pandas
or stdlib
csv
+
json
CSVJSON
pandas
or stdlib
csv
+
json
JSONTOML
tomli
/
tomllib
(read) +
tomli-w
(write)
XMLJSON
xmltodict
JSONXML
dicttoxml
or
xmltodict.unparse
源格式目标格式推荐库
JSONYAML
pyyaml
YAMLJSON
pyyaml
JSONCSV
pandas
或标准库
csv
+
json
CSVJSON
pandas
或标准库
csv
+
json
JSONTOML
tomli
/
tomllib
(读取) +
tomli-w
(写入)
XMLJSON
xmltodict
JSONXML
dicttoxml
xmltodict.unparse

Images

图片

FromToRecommended Library
PNG/JPG/WebP/GIFAny raster
Pillow
(PIL)
SVGPNG/JPG
cairosvg
or
svglib
+
reportlab
PNGSVG
potrace
(CLI) for tracing, limited fidelity
源格式目标格式推荐库
PNG/JPG/WebP/GIF任何光栅格式
Pillow
(PIL)
SVGPNG/JPG
cairosvg
svglib
+
reportlab
PNGSVG
potrace
(CLI)用于追踪,保真度有限

Workflow

工作流程

  1. Identify source format (from file extension or user statement)
  2. Identify target format
  3. Check
    references/
    for format-specific guidance
  4. Generate conversion code using recommended library
  5. Handle edge cases (encoding, transparency, nested structures)
  6. Execute conversion and report results
  1. 识别源格式(通过文件扩展名或用户描述)
  2. 识别目标格式
  3. 查看
    references/
    获取格式特定指南
  4. 使用推荐库生成转换代码
  5. 处理边缘情况(编码、透明度、嵌套结构)
  6. 执行转换并报告结果

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/
:
  • references/document-conversions.md
    - PDF handling, encoding issues, styling preservation
  • references/data-conversions.md
    - Schema handling, type coercion, nested structures
  • references/image-conversions.md
    - Quality settings, transparency, color profiles
Consult these references when handling edge cases or when the user has specific quality/fidelity requirements.
复杂转换的详细指南位于
references/
目录下:
  • references/document-conversions.md
    - PDF处理、编码问题、样式保留
  • references/data-conversions.md
    - 模式处理、类型转换、嵌套结构
  • references/image-conversions.md
    - 质量设置、透明度、颜色配置文件
处理边缘情况或用户有特定质量/保真度要求时,请参考这些文档。