• PHP在linux读取word文档


    几天帮朋友解决一个技术问题,在Linux下,将word文档中的内容读取,然后使用正则匹配,拼成sql入库

    查阅了外文资料和google之后,步骤如下:

    #wget http://www.winfield.demon.nl/linux/antiword-0.37.tar.gz
    #tar zxvf antiword-0.37.tar.gz
    #cd antiword-0.37
    #make
    #make install 

    antiword
    cp /root/bin/*antiword /usr/local/bin/
    mkdir /usr/share/antiword
    cp -R /root/.antiword/* /usr/share/antiword/
    chmod 777 /usr/local/bin/*antiword
    chmod 755 /usr/share/antiword/*

    安装完成之后,如果要在web上查看的话,需要使用root执行 make global_install

        <?php  
        header("Content-type: text/html; charset=utf-8");  
          
          
        $filename = 'test.doc';  
        #$content = shell_exec('antiword '.$filename);  
        $content = shell_exec('antiword -mUTF-8 '.$filename);   
          
          
        echo '<pre>';  
        print_r ($content);  
        echo '</pre>';  
    
    #coding=utf-8
    #usage python <script_name> <docFilePath>
    #pip install python-docx [安装一下扩展库]
    import sys
    import os
    
    from docx import Document
    
    #获取当前脚本得名称
    argv0_list = sys.argv[0].split("\");
    script_name = argv0_list[len(argv0_list) - 1]; 
    usage = "
     Usage python <"+script_name+"> <docFilePath>"
    
    if len(sys.argv) != 2:
    	print "Warning:
     docx file is empty" + usage
    	sys.exit()
    docx_path = sys.argv[1]
    if not os.path.exists(docx_path):
    	print "Warning:
     docx file is not exist" + usage
            sys.exit()
    
    #打开文档
    document = Document(docx_path)
    #读取每段资料
    l = [ paragraph.text.encode('utf8') for paragraph in document.paragraphs];
    #输出并观察结果,也可以通过其他手段处理文本即可
    for i in l:
        print i
    #读取表格材料,并输出结果
    tables = [table for table in document.tables];
    for table in tables:
        for row in table.rows:
            for cell in row.cells:
                print cell.text.encode('utf8'),'	',
    
  • 相关阅读:
    js获取项目根路径
    js金额转换大写
    jQuery css() 方法
    jquery控制css的display(控制元素的显示与隐藏)
    HTML <base> 标签
    MySql服务器的启动和关闭
    linux脚本^M: bad interpreter:解决方法
    linux文档编辑
    Properties 转换成Map
    java中获取ServletContext常见方法
  • 原文地址:https://www.cnblogs.com/lixiuran/p/7101615.html
Copyright © 2020-2023  润新知