admin管理员组文章数量:1593158
提示:node版本18.18.1 ,python版本3.11.5
文章目录
- @[TOC](文章目录)
- 前言
- 一、docx-pdf【node】
- 二、安装依赖应用程序--LibreOffice
- 三、exec执行cmd命令【node】
- 四、office-to-pdf【node】
- 五、Win32com【python】
- 六、python-docx【python 3.11.5】
- 总结
- @[TOC](文章目录)
前言
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')
样式也有问题,还需要进行配置,太费时间,没处理,不建议使用
总结
踩坑路漫漫长@~@
版权声明:本文标题:word转换---docx转pdf,【node】docx-pdf、LibreOffice,office-to-pdf【python】Win32com、python-docx 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.elefans.com/xitong/1728165710a1147993.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论