Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

file_utils.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #
  2. # Copyright 2025 The InfiniFlow Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. import json
  17. from docx import Document # pip install python-docx
  18. from openpyxl import Workbook # pip install openpyxl
  19. from PIL import Image, ImageDraw # pip install Pillow
  20. from pptx import Presentation # pip install python-pptx
  21. from reportlab.pdfgen import canvas # pip install reportlab
  22. def create_docx_file(path):
  23. doc = Document()
  24. doc.add_paragraph("这是一个测试 DOCX 文件。")
  25. doc.save(path)
  26. return path
  27. def create_excel_file(path):
  28. wb = Workbook()
  29. ws = wb.active
  30. ws["A1"] = "测试 Excel 文件"
  31. wb.save(path)
  32. return path
  33. def create_ppt_file(path):
  34. prs = Presentation()
  35. slide = prs.slides.add_slide(prs.slide_layouts[0])
  36. slide.shapes.title.text = "测试 PPT 文件"
  37. prs.save(path)
  38. return path
  39. def create_image_file(path):
  40. img = Image.new("RGB", (100, 100), color="blue")
  41. draw = ImageDraw.Draw(img)
  42. draw.text((10, 40), "Test", fill="white")
  43. img.save(path)
  44. return path
  45. def create_pdf_file(path):
  46. if not isinstance(path, str):
  47. path = str(path)
  48. c = canvas.Canvas(path)
  49. c.drawString(100, 750, "测试 PDF 文件")
  50. c.save()
  51. return path
  52. def create_txt_file(path):
  53. with open(path, "w", encoding="utf-8") as f:
  54. f.write("这是测试 TXT 文件的内容。")
  55. return path
  56. def create_md_file(path):
  57. md_content = "# 测试 MD 文件\n\n这是一份 Markdown 格式的测试文件。"
  58. with open(path, "w", encoding="utf-8") as f:
  59. f.write(md_content)
  60. return path
  61. def create_json_file(path):
  62. data = {"message": "这是测试 JSON 文件", "value": 123}
  63. with open(path, "w", encoding="utf-8") as f:
  64. json.dump(data, f, indent=2)
  65. return path
  66. def create_eml_file(path):
  67. eml_content = (
  68. "From: sender@example.com\n"
  69. "To: receiver@example.com\n"
  70. "Subject: 测试 EML 文件\n\n"
  71. "这是一封测试邮件的内容。\n"
  72. )
  73. with open(path, "w", encoding="utf-8") as f:
  74. f.write(eml_content)
  75. return path
  76. def create_html_file(path):
  77. html_content = (
  78. "<html>\n"
  79. "<head><title>测试 HTML 文件</title></head>\n"
  80. "<body><h1>这是一个测试 HTML 文件</h1></body>\n"
  81. "</html>"
  82. )
  83. with open(path, "w", encoding="utf-8") as f:
  84. f.write(html_content)
  85. return path