整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

如何免费将网页内容转成Word文档

们经常会在网上查找资料,而大多文档下载都需要付费,有哪些方法能免费将网页转换成Word文档呢?下面一起来看一看吧。

1、直接复制

最简单的方法就是直接将网页中所需段落或文字选中后,然后右击并选择“复制”,再新建一个Word文档粘贴进去就可以了。

2、另存为

有一些资料网站做了限制,文字无法选择,或者选择之后也复制不了,那么我们可以先将其以网页进行保存。在网页任意处右击并选择“网页另存为”;

接着将网页以HTML文件形式保存到电脑桌面后,双击打开这个网页,就可以随意进行复制并且粘贴到Word中了,只是这个网页加载会比较慢,不太建议使用。

3、截图转文字

如果有些网页资料限制多,以上两种方式都无法操作,那我们可以将需要的页面内容进行截图保存,使用一些聊天工具的截图工具或电脑自带的屏幕截取都可以。

然后再使用转换工具将图片识别成文字就可以啦。我们可以使用speedpdf在线转换工具的图片转Word功能,不仅能识别文字,如果有图片,也能以原有格式转换成Word。

首先搜索Speedpdf进入在线转换后,选择列表中转换格式中的“JPG to Word ”;然后将所有保存的图片批量全部上传进行转换,这样就能将内容转到同一个Word文档中。

转换完成之后直接下载就可以打开Word文档了,是不是很方便,而且转换也是免费的哦,强烈推荐这种处理方式呢。

  • pandoc 下载和基本使用
  • java 操作pandoc 批量转换为word文档
  • 使用screw生成数据库文档

场景概述

在项目开发时,平时喜欢用markdown写文档.

但项目交付给客户的时候,需要将markdown转换为更加正式的word文档进行交付.

于是用pandoc将.md文件转换为word文档.

pandoc 下载和基本使用

  • 官网下载pandoc: https://www.pandoc.org/installing.html

Pandoc 可以用于各种文档格式之间的转换。如: Markdown、Microsoft Word、HTML、EPUB、roff man、LaTeX、PDF。

  • pandoc命令行使用示例: https://www.pandoc.org/demos.html
// 1 .md 文件转换为word文档
pandoc test.md -o newtest.docx

// 2 多个md转换为一个word文档,
pandoc xx1.md xx2.md  -o 合并后的.docx 
  
/**3 自定义文档格模板格式**/
//3.1 首先获取pandoc 自带的doc模板文档muban.docx
pandoc.exe  -o muban.docx --print-default-data-file=reference.docx
//3.2 打开muban.docx,修改模板文件中样式(如下图,需要通过右键修改样式方式,模板才能生效)
//3.3 使用模板文档,进行转换
pandoc.exe  xx1.md xx2.md  -o 合并后的.docx  --reference-doc=C:\\reference.docx
  • 修改调整后的模板文档 muban.docx

https://javabus.oss-cn-beijing.aliyuncs.com/reference.docx


需要通过右键修改样式方式,模板才能生效

  • markdown转word样式没处理好,生成word后新建样式手动处理

通过名称可以控制样式排序

段落背景色设置

前后缩进控制

java 操作pandoc 批量转换为word文档

待转换文档目录结构如下:

dev-docs
|__docs
|____docsify  
|____imgs		文档图片存放
|____core   
|____other
	     _sidebar.md  md文档目录
  • 效果图预览:

markdown文档

转换后word效果图

  • 代码如下
/**
 * 通过pandoc输出默认模板到custom.docx:   pandoc.exe  -o custom.docx --print-default-data-file=reference.docx
 * <p>
 * 使用指定的文件作为输出文件的格式参考。参考文件的内容被忽略,只使用其中的样式和文档属性(包括边界、页面尺寸、页眉页脚等) (--reference-doc=File(custom.docx)) 指定要生成的word模板
 */
public class Markdown2Docx_backup {
    //pandoc.exe 文件绝对路径
    private static final String PANDOC_PATH = "C:\\doc\\pandoc.exe ";
    //markdown文档(.md后缀格式) 所在路径
    private static final String DOCS_DIR = "C:\\doc\\docs\\docs";
    //文档依赖图片路径
    private static final String IMG_DIR = "C:\\doc\\docs\\docs\\imgs";
    //侧边栏_sidebar.md,根据侧边栏顺序转换文档
    private static final String _sidebar = "C:\\doc\\docs\\docs\\_sidebar.md";
    //docx模板  使用 pandoc 进行文档转换(markdown转word) http://t.zoukankan.com/kofyou-p-14932700.html
    private static final String reference_docx = "C:\\doc\\docs\\docs\\reference.docx";

    public static void main(String[] args) {
        copyImageDir();
        String mdFilePath = buildAllMdFilePath();
        convertmd2Docx(mdFilePath);
    }

    /**
     * 解析侧边栏 _sidebar.md  生成所有markdown文件路径,如:   xx1.md xx2.md
     * <p>
     * 侧边栏内容示例: -[文档说明](/job-debug.md)
     */
    private static final String buildAllMdFilePath() {
        StringBuilder mds = new StringBuilder();
        File sidebarFile = new File(_sidebar);
        if (!sidebarFile.exists()) {
            System.err.println("_sidebar.md 侧边栏文件不存在");
            return null;
        }
        //获取文件首行作为文件名
        List<String> contents = FileUtil.readTxtFile(_sidebar, "utf-8");
        for (String content : contents) {
            //content示例必须有()将md路径包含: -[文档说明](/job-debug.md)
            try {
                if (StringUtil.isNullOrEmpty(content.trim())) {
                    continue;
                }
                if (content.indexOf("](") < 0) {
                    System.out.println(content + ",  不是markdown 路径不进行转换");
                    continue;
                }
                //解析出 /job-debug.md
                String mdPath = content.split("]\\(")[1].replace(")", "").replace("/", "\\").trim();
                if (mdPath.endsWith(".md")) {
                    mds.append(DOCS_DIR).append("\\").append(mdPath).append(" ");
                } else {
                    mds.append(DOCS_DIR).append("\\").append(mdPath).append(".md").append(" ");
                }
            } catch (Exception e) {
                System.err.println("从文档中解析md文件路径失败");
            }
        }
        return mds.toString();
    }

    private static void convertmd2Docx(String mdFilePath) {
        String docxPath = DOCS_DIR + "\\开发手册.docx";
        Runtime rn = Runtime.getRuntime();
        try {
            //pandoc xx1.md xx2.md -o test.docx
            String command;
            File mubanDoc = new File(reference_docx);
            if (mubanDoc.exists()) {
                System.out.println("使用docx模板进行文档转换: " + reference_docx);
                command = PANDOC_PATH + " " + mdFilePath + " -o " + docxPath + " --toc-depth=3 --reference-doc=" + reference_docx;
            } else {
                //pandoc xx1.md xx2.md -o test.docx
                command = PANDOC_PATH + " " + mdFilePath + " -o " + docxPath + " ";
            }
            System.out.println(command);
            Process exec = rn.exec(command);
        } catch (Exception e) {
            System.out.println("调用服务生成word文档错误 " + e.getMessage());
        }
    }

    /**
     * 1 (pandoc test.md -o test.docx  无法识别../imgs ),将../imgs替换为/imgs绝对路径
     * <p>
     * 2 修改 markdown文档中的图片引入方式,手动将文档中所有../imgs 替换为/imgs   ![业务用例图](/imgs/complex-email.png)
     * <p>
     * 3 通过copyImageDir()方法,将/docs/imgs目录下的图片,复制到当前程序运行的根目录/的imgs下(/imgs)
     */
    private static void copyImageDir() {
        //文档中依赖图片文件路径
        File docImgsDir = new File(IMG_DIR);

        //解决pandoc 转换文件时,找不到图片路径../imgs问题
        File dir = new File("/imgs");
        if (!dir.exists()) {
            dir.mkdir();
            File[] files = docImgsDir.listFiles();
            for (File file : files) {
                String s = FileUtil.readToString(file);
                try {
                    FileUtil.writeToFile(dir + "\\" + file.getName(), s, "utf-8", false);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            System.out.println("复制文档图片到路径:" + dir.getAbsolutePath());
        } else {
            System.out.println("文档图片已存在路径:" + dir.getAbsolutePath());
        }
    }
}

参考文档

  • Pandoc使用技巧: https://blog.csdn.net/weixin_39617497/article/details/117897721
  • pandoc使用latex模板转pdf文件: https://blog.51cto.com/u_1472521/5202317
  • 使用 pandoc 进行文档转换(markdown转word) http://t.zoukankan.com/kofyou-p-14932700.html


使用screw生成数据库文档

  • 文档地址: https://gitee.com/leshalv/screw
  • 生成文档示例


自动生成文档目录

数据表和字段描述示例

  • 引入screw依赖
<!--文档地址 -->
<!--数据库文档生成工具 screw-->
<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <version>2.3.30</version>
</dependency>

<dependency>
    <groupId>cn.smallbun.screw</groupId>
    <artifactId>screw-core</artifactId>
    <version>1.0.3</version>
</dependency>
  • 编码实现生成文档

ava实现在线预览功能是一个大家在工作中也许会遇到的需求,如果公司有钱,直接使用付费的第三方软件或者云在线预览服务就可以了,例如永中office、office web 365(http://www.officeweb365.com/)他们都有云在线预览服务,就是要钱0.0

如果想要免费的,可以用openoffice,还需要借助其他的工具(例如swfTools、FlexPaper等)才行,可参考这篇文章http://blog.csdn.net/z69183787/article/details/17468039,写的挺细的,实现原理就是:

1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件;

2.通过swfTools将pdf文件转换成swf格式的文件;

3.通过FlexPaper文档组件在页面上进行展示。

当然如果装了Adobe Reader XI,那把pdf直接拖到浏览器页面就可以直接打开预览,这样就不需要步骤2、3了,前提就是客户装了Adobe Reader XI这个pdf阅读器。

我这里介绍通过poi实现word、excel、ppt转html,这样就可以放在页面上了。

###word转html

###excel转html

###ppt转html

其实只是ppt转图片,有了图片后放到页面上去,点击下一页就一张张显示就可以了。这里只介绍ppt转图片的过程。