E007 如何完成doc~docx互相转换,Word批量转换Pdf

Hi,How are you doing?

我是职场编码(CodeVoc)。

在E000中,我们介绍了Node.js、Ruby、Electron等工具下载安装。

这期,给你演示一下由Electron联合Ruby制作的小工具。

借助Electron官方Demo,我们很容易制作一个工具展示平台。

点击“View Demo”会弹出我们的工具界面。

一、项目需求

这个工具的主要目的是为了完成Word-doc~docx互相转换,Word转换Pdf。

doc转docx,需要下拉框选择“docx”,

docx转doc,需要下拉框选择“doc”,

Word转Pdf,需要下拉框选择“pdf”,

接着,点击“执行”按钮,

即可根据文件类型,完成doc~docx互相转换,Word批量转换Pdf。

二、界面设计

【html】

生成标题文字:<p></p>

生成表单容器:<form></form>

生成布局标签:<div></div>

生成行内标签:<span></span>

生成单行输入框:<input id="source_line">

生成文件选择按钮:<input type="file" id="goal_file">

生成下拉选择按钮:<select id="select_name"><option></option></select>

生成普通按钮:<input type="button" id="execute">

【css】

关注四点前白后绿气泡某杺平台,搜索“职场编码”查看源码。

【javascript】

根据ID,选中source_file按钮

var source_line=document.getElementById('source_line')

给source_file按钮,添加"change"事件

source_line.value=document.getElementById('source_file').files[0].path 

execute按钮添加单击事件

execute.addEventListener("click",function(){获取参数1,获取参数2,调用})

获取参数1

var select_name=document.getElementById('select_name').value

获取参数2

var source_line=document.getElementById('source_line').value

调用Ruby脚本

const { spawn } = require('child_process')
const ls = spawn('ruby', ['Ruby脚本完整路径',参数1,参数2])

三、逻辑梳理

=> 基础语法

引用Ruby标准库

require "win32ole"

创建主入口方法、分入口方法

def Main(name,pth_source)
def Doc_to_docx(pth_source)
def Docx_to_doc(pth_source)
def Word_to_pdf(pth_source)  

接收控制台传双参

Main(ARGV[0],ARGV[1])

对参数ARGV[0] => name进行判断,根据判断结果执行分入口方法

case name
    when "doc" then Docx_to_doc(pth_source)
    when "docx" then Doc_to_docx(pth_source)
    when "pdf" then Word_to_pdf(pth_source)
end  

提取指定文件

filename= Dir.glob(pth_source+'/*.docx' )
filename= Dir.glob(pth_source+'/*.doc' )
filename.each{|i|
}

文件路径‘\’替换为'/'

pth_source=File.dirname(pth_source).gsub('\\','/')

提取不含后缀文件名

name=File.basename(i,'.doc')
name=File.basename(i,'.docx')

=> 对象模型

创建可视化Word应用

@wap=WIN32OLE::new("word.application");@wap.visible=true 

打开Excel指定工作簿

wdc=@wap.documents.open(i)

执行另存Doc、Docx、Pdf操作

wdc.saveas(pth_source + '/' + name + '.doc') 
wdc.saveas(pth_source + '/' + name + '.docx',16)     wdc.exportasfixedformat(pth_source + '/' + name + '.pdf',17)

关注四点前白后绿气泡某杺平台,搜索“职场编码”查看源码。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值