V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
regmach
V2EX  ›  问与答

请问有什么把 word 文档转换成 clean 的 HTML 的方法?

  •  
  •   regmach · 2014 年 9 月 15 日 · 4813 次点击
    这是一个创建于 4135 天前的主题,其中的信息可能已经有所发展或是发生改变。
    主要是图片和表格.
    只要保留结构和表格的线框就好
    >_<<
    一直没有找到靠谱的方案啊
    14 条回复    2014-09-16 01:22:04 +08:00
    jtn007
        1
    jtn007  
       2014 年 9 月 15 日
    直接另存为html?
    Automan
        2
    Automan  
       2014 年 9 月 15 日   ❤️ 1
    之前做杂志App的时候也遇到过这个问题,稿件是 word,杂志的内容格式是 HTML
    我的解决方法是另存为,再用正则替换掉所有不需要的代码
    regmach
        3
    regmach  
    OP
       2014 年 9 月 15 日
    @jtn007 冗余代码太多.
    regmach
        4
    regmach  
    OP
       2014 年 9 月 15 日
    @Automan 可有详解?
    lincanbin
        5
    lincanbin  
       2014 年 9 月 15 日   ❤️ 1
    @regmach 如果你是PHP用户,可以直接用strip_tags。
    Python的话显然就比较麻烦了,我没找到比较好用的模块,一般都是用re 一个个白名单匹配出来
    loading
        6
    loading  
       2014 年 9 月 15 日 via iPad
    rtf格式,不过没图
    darkmi
        7
    darkmi  
       2014 年 9 月 15 日   ❤️ 1
    Aspose.Words,完美方案,价格不菲。
    regmach
        8
    regmach  
    OP
       2014 年 9 月 15 日
    @darkmi 在用word cleaner...@_@ 实在不行就用这个吧.
    Automan
        9
    Automan  
       2014 年 9 月 15 日   ❤️ 1
    @regmach 我找找不知道放在哪了。。其实就是一小段 php 代码
    em70
        10
    em70  
       2014 年 9 月 15 日 via Android   ❤️ 1
    只是为了网页显示目的吗
    regmach
        11
    regmach  
    OP
       2014 年 9 月 16 日
    @em70
    是的.
    只是为了显示,搞不定即时图片上传啊....杯具.
    欢迎你,em70!
    印象深刻,嘿嘿嘿.

    @Automan
    谢谢

    to all,在用免费版的word cleaner,到期或者次数用尽就只能重装了.
    Automan
        12
    Automan  
       2014 年 9 月 16 日   ❤️ 1
    function rplace($filename) {

    $content = file_get_contents($filename);
    $content = str_replace('<meta name=Generator content="Microsoft Word 11 (filtered)">', ' <link rel="stylesheet" type="text/css" href="css/book.css">
    ', $content);

    $content = preg_replace('/<\/?SPAN[^>]*>/i', '', $content);
    // Class
    $content = preg_replace('/<(\w[^>]*) class=([^ |>]*)([^>]*)/i', "<$1$3", $content);
    // Style
    $content = preg_replace('/<(\w[^>]*) style=([^ |>]*)([^>]*)/i', "<$1$3", $content);
    // Lang
    $content = preg_replace('/<(\w[^>]*) lang=([^ |>]*)([^>]*)/i', "<$1$3", $content);
    $content = str_replace('<p>&nbsp;</p>', '', $content);
    $content = preg_replace('/<style>([\s\S]*)<\/style>/i', "", $content);

    $content = preg_replace('/<(\w[^>]*) height=([^ |>]*)([^>]*)/i', "<$1$3", $content);

    echo $content;
    file_put_contents($filename, $content);

    }
    因为我还要保留其他格式,所以只去除了一部分标签,如果你只要 Table,用strip_tags可能更好一点。
    比如 strip_tags($data, '<table><b><p><td><tr><th><tbody>');
    em70
        13
    em70  
       2014 年 9 月 16 日   ❤️ 1
    @regmach
    word的网页显示有2种办法
    1. 把word文档的每一页转换为图片,这种方法界面简洁,支持手机,平板浏览器,但对需要跨页的表格支持不好
    2. flashpager把word转换为swf,百度文库,豆丁,道客巴巴就是这种技术,不过百度好像现在用自己的显示引擎,不依赖flash了.这种技术的问题是需要flash支持
    regmach
        14
    regmach  
    OP
       2014 年 9 月 16 日
    @em70
    @Automan
    感谢 :)
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   Solana   ·   2801 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 08:22 · PVG 16:22 · LAX 00:22 · JFK 03:22
    ♥ Do have faith in what you're doing.