云
昨天 bd9c8a5c560083bcb6d0a9c010f56e1f9cf83df6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import sys,json,hashlib,zipfile,re
from pathlib import Path
from lxml import etree as E
P=Path(__file__).parent;P.mkdir(exist_ok=True)
base=Path('D:/WechatRecord/xwechat_files/wxid_vmeut7wztkbp22_1546')
temp=base/'temp/RWTemp/2026-10/c40afdcfa98e2b4561823e6babbefa33'
names=['4-STP-SG-003-01有机黑果枸杞生产工艺规程.docx','4-STP-DY-007黑果枸杞生产工艺规程.docx','STP-SG-002-04冻干枸杞生产工艺规程.docx','4-STP-GT-015-04沙棘原浆罐头生产工艺规程(1).docx','4-STP-GT-016-04黑果枸杞原浆罐头生产工艺规程.docx','4-STP-GT-015-04沙棘原浆罐头生产工艺规程.docx','STP-GY-001-06即食藜麦粉R型生产工艺规程.docx','STP-DY-001-02黑果枸杞袋泡茶生产工艺规程.docx','STP-GT-009-07枸杞原浆罐头生产工艺规程.docx','STP-GY-002-05即食藜麦粉B型生产工艺规程.docx','4-STP-SG-001-09黑果枸杞生产工艺规程.docx','4-STP-GT-010-05黑果枸杞玫瑰果膏生产工艺规程(1).docx']
files=[temp/n for n in names]
root=base/'msg/attach/c40afdcfa98e2b4561823e6babbefa33/2026-10/Rec'
for folder,ns in [('f15a297ebf87a553',['4-STP-FB-005铁棍山药粉生产工艺规程.docx','4-STP-YL-006桑葚原浆生产工艺规程.docx','4-STP-YL-016红黑枸杞原浆生产工艺规程.docx','4-STP-YL-008益生元西梅饮生产工艺规程.docx','4-STP-YL-010γ-氨基丁酸酸枣仁饮品(桃子味)生.docx','4-STP-YL-016红黑枸杞原浆生产工艺规程.docx','4-STP-YL-015人参枸杞原浆生产工艺规程.docx','4-STP-YL-018黑果枸杞桑葚原浆生产工艺规程.docx']),('a679d4d8551b6d72',['4-STP-YL-009γ-氨基丁酸营养素饮品(蜜桃味)生.docx','4-STP-YL-012蓝莓叶黄素DHA饮品生产工艺规程.docx','4-STP-YL-017人参葛根枳椇子玉米低聚肽饮品生产.docx','4-STP-YL-011西梅原浆生产工艺规程.docx','4-STP-YL-007燕窝肽γ-氨基丁酸鱼胶原蛋白肽饮品.docx','4-STP-YL-013DHA藻油饮品生产工艺规程.docx'])]:
    for i,n in enumerate(ns,1 if folder=='f15a297ebf87a553' else 0):files.append(root/folder/'F'/str(i)/n)
out=[]
ns={'w':'http://schemas.openxmlformats.org/wordprocessingml/2006/main','a':'http://schemas.openxmlformats.org/drawingml/2006/main'}
for i,f in enumerate(files,1):
    assert f.is_file(),str(f)
    data=f.read_bytes(); rec={'id':i,'file':str(f),'name':f.name,'sha256':hashlib.sha256(data).hexdigest()}
    with zipfile.ZipFile(f) as z:
        xml=z.read('word/document.xml');rootx=E.fromstring(xml)
        paras=[]
        for p in rootx.xpath('//w:p',namespaces=ns):
            text=''.join(p.xpath('.//w:t/text()',namespaces=ns))
            if text.strip():paras.append(text)
        rec['paragraphs']=paras; rec['drawing_text']=rootx.xpath('//a:t/text()',namespaces=ns)
        media=[]
        for path in z.namelist():
            if path.startswith('word/media/'):
                dst=P/'media'/str(i)/Path(path).name;dst.parent.mkdir(parents=True,exist_ok=True);dst.write_bytes(z.read(path));media.append(str(dst))
        rec['media']=media
        (P/f'{i:02d}-document.xml').write_bytes(xml)
    out.append(rec)
    (P/f'{i:02d}-提取文本.txt').write_text('\n'.join(f'[{j}] {t}' for j,t in enumerate(paras)),encoding='utf8')
(P/'documents.json').write_text(json.dumps(out,ensure_ascii=False,indent=2),encoding='utf8')
print(json.dumps([{'id':x['id'],'name':x['name'],'paragraphs':len(x['paragraphs']),'media':len(x['media']),'hash':x['sha256'][:12]} for x in out],ensure_ascii=False))