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

2014 年 9 月 15 日
 regmach
主要是图片和表格.
只要保留结构和表格的线框就好
>_<<
一直没有找到靠谱的方案啊
4966 次点击
所在节点    问与答
14 条回复
jtn007
2014 年 9 月 15 日
直接另存为html?
Automan
2014 年 9 月 15 日
之前做杂志App的时候也遇到过这个问题,稿件是 word,杂志的内容格式是 HTML
我的解决方法是另存为,再用正则替换掉所有不需要的代码
regmach
2014 年 9 月 15 日
@jtn007 冗余代码太多.
regmach
2014 年 9 月 15 日
@Automan 可有详解?
lincanbin
2014 年 9 月 15 日
@regmach 如果你是PHP用户,可以直接用strip_tags。
Python的话显然就比较麻烦了,我没找到比较好用的模块,一般都是用re 一个个白名单匹配出来
loading
2014 年 9 月 15 日
rtf格式,不过没图
darkmi
2014 年 9 月 15 日
Aspose.Words,完美方案,价格不菲。
regmach
2014 年 9 月 15 日
@darkmi 在用word cleaner...@_@ 实在不行就用这个吧.
Automan
2014 年 9 月 15 日
@regmach 我找找不知道放在哪了。。其实就是一小段 php 代码
em70
2014 年 9 月 15 日
只是为了网页显示目的吗
regmach
2014 年 9 月 16 日
@em70
是的.
只是为了显示,搞不定即时图片上传啊....杯具.
欢迎你,em70!
印象深刻,嘿嘿嘿.

@Automan
谢谢

to all,在用免费版的word cleaner,到期或者次数用尽就只能重装了.
Automan
2014 年 9 月 16 日
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
2014 年 9 月 16 日
@regmach
word的网页显示有2种办法
1. 把word文档的每一页转换为图片,这种方法界面简洁,支持手机,平板浏览器,但对需要跨页的表格支持不好
2. flashpager把word转换为swf,百度文库,豆丁,道客巴巴就是这种技术,不过百度好像现在用自己的显示引擎,不依赖flash了.这种技术的问题是需要flash支持
regmach
2014 年 9 月 16 日
@em70
@Automan
感谢 :)

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://study.congcong.us/t/133608

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX