read-product-docs.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. // Read product docs (.docx) and emit trimmed plain text.
  2. const fs = require('fs');
  3. const path = require('path');
  4. const AdmZip = require('adm-zip');
  5. function docxText(p) {
  6. try {
  7. const z = new AdmZip(p);
  8. const x = z.readAsText('word/document.xml');
  9. if (!x) return '[empty]';
  10. return x
  11. .replace(/<w:tab\/>/g, ' ')
  12. .replace(/<w:br\/>/g, '\n')
  13. .replace(/<\/w:p>/g, '\n')
  14. .replace(/<[^>]+>/g, '')
  15. .replace(/\s+\n/g, '\n')
  16. .replace(/\n{3,}/g, '\n\n')
  17. .trim();
  18. } catch (e) {
  19. return '[ERR ' + e.message + ']';
  20. }
  21. }
  22. const targets = [
  23. {
  24. tag: 'probiotic-背景需求',
  25. p: 'E:\\文件资料\\产品创新2:儿童乳酸菌素片\\儿童乳酸菌素片-资料\\01 儿童装乳酸菌素片产品背景及需求0418.docx',
  26. },
  27. {
  28. tag: 'probiotic-说明书',
  29. p: 'E:\\文件资料\\产品创新2:儿童乳酸菌素片\\儿童乳酸菌素片-资料\\儿乳-乳酸菌素片说明书(0.2g).docx',
  30. },
  31. {
  32. tag: 'monkey-产品资料',
  33. p: 'E:\\文件资料\\产品创新2-江中猴菇饮产品资料.docx',
  34. },
  35. ];
  36. const outDir = path.resolve('data', 'product-docs');
  37. if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
  38. for (const t of targets) {
  39. console.log('====', t.tag, '====');
  40. if (!fs.existsSync(t.p)) {
  41. console.log('[missing]', t.p);
  42. continue;
  43. }
  44. const txt = docxText(t.p);
  45. const out = path.join(outDir, t.tag + '.txt');
  46. fs.writeFileSync(out, txt, 'utf-8');
  47. console.log('[chars]', txt.length, '-> ', out);
  48. }