当前位置:网站首页>Qt笔记-利用QAxObject将文件夹内的所有docx转pdf
Qt笔记-利用QAxObject将文件夹内的所有docx转pdf
2022-07-29 15:24:00 【IT1995】
这里用Qt提供的AXContainer库就可以了,但这个只能在Windows平台上用。
首先要先了解几个知识点:
ExportAsFixedFormat是将文件导出为指定格式:这里关键的有2个参数:
FileName: 文件名;
FileType:为17代表是pdf
整体代码如下:
DocxToPdfDemo.pro
QT += axcontainer gui widgets
CONFIG += c++11 console
CONFIG -= app_bundle
# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
main.cpp
#include <QAxObject>
#include <QAxWidget>
#include <QDebug>
#include <QApplication>
#include <QFile>
#include <QDir>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QDir dir("D:\\数学");
if(!dir.exists()){
qDebug() << "dir error";
return 0;
}
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QFileInfoList list = dir.entryInfoList();
for(int i = 0; i < list.size(); i++){
QFileInfo fileInfo = list.at(i);
QString fileName = fileInfo.fileName();
QString filePath = fileInfo.absoluteFilePath();
QString dirName = dir.path();
QAxWidget word("Word.Application");
word.setProperty("Visible", false);
QAxObject * documents = word.querySubObject("Documents");
QAxObject * document = documents->querySubObject("Open(QString)", filePath);
QVariant OutputFileName(dirName + "/" + fileName.mid(0, fileName.lastIndexOf(".")) + ".pdf");
QVariant ExportFormat(17);
QVariant OpenAfterExport(false);
document->querySubObject("ExportAsFixedFormat(const QVariant&,const QVariant&,const QVariant&)",
OutputFileName,
ExportFormat,
OpenAfterExport);
document->dynamicCall("Close(boolean)",false);
word.dynamicCall("Quit (void)");
qDebug() << filePath + " to pdf success";
}
return a.exec();
}
程序运行截图如下:

源码打包下载地址:
Qt/DocxToPdfDemo at master · fengfanchen/Qt · GitHub
边栏推荐
猜你喜欢

【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)-- 组件UI

LeetCode·739.每日温度·递减栈

NLP自然语言处理-机器学习和自然语言处理介绍(一)

韦伯首批照片引发论文竞速大战:晚13秒即错失首发,科研党纷纷肝得起飞

【深度学习】深度学习刷SOTA的一堆trick

走高质量、可持续的保障亿万家庭之路 水滴公司发布2020·2021ESG报告

File management: the physical structure of files

Alibaba 开源内网高并发编程手册

QT连接Mysql数据库(详细成功版)

Micro combat | centralized configuration service center Config asymmetric encryption and security management
随机推荐
Linux下载安装mysql5.7版本教程最全详解
MySQL数据库安装配置保姆级教程(以8.0.29为例)有手就行
韦伯首批照片引发论文竞速大战:晚13秒即错失首发,科研党纷纷肝得起飞
将博客搬至CSDN
开源数据库连接池的使用及其工具类
DevOps的未来趋势
NTP服务器搭建
奇怪,为什么ArrayList初始化容量大小为10?
Micro combat | centralized configuration service center Config asymmetric encryption and security management
求连续90日每日的用户留存率 -- 全网唯一
货比三家 tb1.3
展讯模块读写IMEI
mysql的union和union all
Linux环境 redis完整配置及启动命令
CSP:重庆八中宏帆初级中学校新初二编程社C2024liuyanjia暑假一期集训总结(2/6))
You need to know about the digital transformation of eight guiding principles
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)-- 组件UI
LeetCode·每日一题·593.有效的正方形·数学
药品研发--质理研究人员绩效办法
手摸手实现Canal如何接入MySQL实现数据写操作监听