word转换---docx转pdf,【node】docx-pdf、LibreOffice,office-to-pdf【python】Win32com、python-docx

提示:node版本18.18.1 ,python版本3.11.5

前言

word预览

在这里插入图片描述

项目结构预览
在这里插入图片描述

一、docx-pdf【node】

npm docx-pdf -S-D

index.js

const express = require('express');
const app = express();
app.listen(3000,()=>{
    console.log('http://localhost:3000');
});

const converter = require('docx-pdf');

const ConvertDocToPdf = async (inputPath, outputPath) => {
    
    converter(inputPath, outputPath, (err, result) => {
        if (err) {
            console.error('PDF转换失败:', err);
            return;
        }
        console.log('PDF转换成功:', result);
    })
}
// 注意这种方式转换,outputPath是文件,需要写到具体文件
ConvertDocToPdf('./source/test2.docx','./source/test2.pdf')

在这里插入图片描述
在这里插入图片描述
转换成功了,但是不支持.docx文件的表格转换

二、安装依赖应用程序–LibreOffice

libreoffice官网
1、安装
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
安装完成,测试下soffice命令,会调起libreOffice软件

soffice

在这里插入图片描述
在这里插入图片描述
如出现一下错误
在这里插入图片描述
去配置环境变量,path中加入libreOffice的安装路径/program

在这里插入图片描述

三、exec执行cmd命令【node】

index.js

const express = require('express');
const app = express();
app.listen(3000,()=>{
    console.log('http://localhost:3000');
});
const { exec } = require('child_process');
const docToPdf = async (inputPath, outputPath) => {
    
    exec(`soffice --headless --convert-to pdf:writer_pdf_Export ${inputPath} --outdir ${outputPath}`, (error, stdout, stderr) => {
        if (error) {
            console.error('PDF转换失败:', error);
            return;
        }
        console.log('PDF转换成功:', stdout, stderr);
    });
}
// 注意这种方式转换,outputPath是文件夹,不需要写到具体文件
docToPdf('./source/test2.docx','./source/')

在这里插入图片描述
在这里插入图片描述

依赖libreOffice应用程序解析的方式,导出的pdf格式都是一样的,跟直接打开.docx文件样式一样,都会有问题。贴一张libreOffice打开.docx文件样式图

在这里插入图片描述

网上会查到OpenOffice应用程序的方式转换,如果同时安装了libreOffice和OpenOffice,两个【应用程序】都是用soffice命令,确保【环境变量】配置的是想要用的【应用程序】,不然会爬很久的坑。
本人测试OpenOffice未成功,希望能帮助避坑,或者有大神可以指导下

四、office-to-pdf【node】

当前插件也依赖libreOffice

index.js

const express = require('express');
const app = express();
app.listen(3000,()=>{
    console.log('http://localhost:3000');
});
const toPdf = require("office-to-pdf");
const fs = require('fs');
const officeToPdf = (inputPath,outputPath)=>{
    fs.readFile(inputPath, (err, data) => {
        if (err) {
            console.error('读取文件错误:', err);
            return;
        }
        toPdf(data).then((pdfBuffer)=>{
            fs.writeFileSync(outputPath, pdfBuffer);
            console.log(err,'PDF转换成功')
        }).catch((err)=>{
            console.error(err,'PDF转换失败')
        })
    });
}
// 注意这种方式转换,outputPath是文件,需要写到具体文件
officeToPdf('./source/test2.docx','./source/test2.pdf')

在这里插入图片描述
在这里插入图片描述

依赖libreOffice应用程序解析的方式,导出的pdf格式都是一样的,跟直接打开.docx文件样式一样,都会有问题

五、Win32com【python】

在这里插入图片描述

pip install pypiwin32

1、目录结构
在这里插入图片描述
2、test.py

from win32com.client import Dispatch
import sys

def docToPdf(inputPath):
	word = Dispatch('Word.Application')
	# 后台运行
	word.Visible = 0
	# 禁止alert
	word.DisplayAlerts = 0  
	path = sys.path[0] + inputPath
	doc = word.Documents.Open(FileName=path, Encoding='gbk')
	#  txt=4 html=10 docx=16 pdf=17
	doc.SaveAs(path[:-5]+'.pdf', 17) 
	doc.Close()
	word.Quit()
	print("转换完成")

if __name__ == '__main__':
	docToPdf('/test2.docx')

在这里插入图片描述
在这里插入图片描述

用的office进行的转换,效果与office保持一致,缺点貌似只能在windows上用

六、python-docx【python 3.11.5】

 pip install python-docx
  pip install reportlab

test.py

from docx import Document
from reportlab.lib.pagesizes import letter
from reportlab.platypus import SimpleDocTemplate, Paragraph

def docxToPdf(inputPath, outputPath):
    # 打开.docx文件
    doc = Document(inputPath)
    data = []
    # 读取数据
    for paragraphs in doc.paragraphs:
        data.append(Paragraph(paragraphs.text))

    # 新建.pdf文件
    doc = SimpleDocTemplate(outputPath, pagesize=letter)
    # 写入数据
    doc.build(data)

# 调用函数转换文档
docxToPdf('test2.docx', 'test2.pdf')

在这里插入图片描述
在这里插入图片描述

样式也有问题,还需要进行配置,太费时间,没处理,不建议使用

总结

踩坑路漫漫长@~@

  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值