_analyze_template.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const AdmZip = require('adm-zip');
  2. const zip = new AdmZip('C:\\Users\\15850\\Downloads\\DOP生产管理用户操作手册.docx');
  3. // Read styles.xml to understand style definitions
  4. const stylesXml = zip.readAsText('word/styles.xml');
  5. const styleIds = [];
  6. const styleIdRegex = /<w:style[^>]*w:styleId="([^"]+)"[^>]*>/g;
  7. let sm;
  8. while ((sm = styleIdRegex.exec(stylesXml)) !== null) {
  9. styleIds.push(sm[1]);
  10. }
  11. console.log('=== Style IDs ===');
  12. console.log(styleIds.join(', '));
  13. // Read document.xml
  14. const xml = zip.readAsText('word/document.xml');
  15. // Extract all text
  16. const allText = [];
  17. const textRegex = /<w:t[^>]*>([^<]*)<\/w:t>/g;
  18. let tm;
  19. while ((tm = textRegex.exec(xml)) !== null) {
  20. if (tm[1].trim()) allText.push(tm[1]);
  21. }
  22. console.log('\n=== All text (first 150) ===');
  23. console.log(allText.slice(0, 150).join(' | '));
  24. console.log('\n=== All text (total ' + allText.length + ' segments) ===');
  25. console.log(allText.join('\n'));
  26. // Find paragraph styles used
  27. const paraStyles = [];
  28. const pStyleRegex = /<w:pStyle w:val="([^"]+)"/g;
  29. let pm;
  30. while ((pm = pStyleRegex.exec(xml)) !== null) {
  31. paraStyles.push(pm[1]);
  32. }
  33. console.log('\n=== Paragraph styles used ===');
  34. console.log([...new Set(paraStyles)].join(', '));
  35. // Count tables
  36. const tableCount = (xml.match(/<w:tbl>/g) || []).length;
  37. console.log('\n=== Table count: ' + tableCount + ' ===');
  38. // Count images
  39. const imageCount = (xml.match(/<w:drawing>/g) || []).length;
  40. console.log('=== Image count: ' + imageCount + ' ===');