createAnomalyTemplate.mjs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import fs from "fs";
  2. import path from "path";
  3. import PizZip from "pizzip";
  4. const contentTypes = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  5. <Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
  6. <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
  7. <Default Extension="xml" ContentType="application/xml"/>
  8. <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
  9. </Types>`;
  10. const rels = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  11. <Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
  12. <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
  13. </Relationships>`;
  14. const documentXml = `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  15. <w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
  16. <w:body>
  17. <w:p><w:r><w:t>{field_name} 异常检测报告</w:t></w:r></w:p>
  18. <w:p><w:r><w:t>评估日期:{datatime}</w:t></w:r></w:p>
  19. <w:p><w:r><w:t>接入风机:{turbine_count} 台;异常风机:{abnormal_count} 台</w:t></w:r></w:p>
  20. <w:p><w:r><w:t>【自动插入:异常概览图】</w:t></w:r></w:p>
  21. <w:p><w:r><w:t>【自动插入:风机检测器图表】</w:t></w:r></w:p>
  22. <w:sectPr/>
  23. </w:body>
  24. </w:document>`;
  25. const zip = new PizZip();
  26. zip.file("[Content_Types].xml", contentTypes);
  27. zip.file("_rels/.rels", rels);
  28. zip.file("word/document.xml", documentXml);
  29. const outDir = path.join(process.cwd(), "templates");
  30. fs.mkdirSync(outDir, { recursive: true });
  31. const outPath = path.join(outDir, "anomaly-report-template.docx");
  32. fs.writeFileSync(outPath, zip.generate({ type: "nodebuffer" }));
  33. console.log("created:", outPath);