[Python Study Notes]批量将wold转换为pdf
本文代码,由原ppt2pdf.py进行改写
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
>>文件: word2pdf.py
>>作者: liu yang
>>邮箱: liuyang0001@outlook.com
>>博客: www.cnblogs.com/liu66blog ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' #!/usr/bin/env python
# -*- coding: utf-8 -*- import sys, os
# 调用com组件包
import comtypes.client def init_word():
word = comtypes.client.CreateObject("Word.Application")
word.Visible = 1
return word # 第二步:找到该路径下的所有doc(x)文件,并将其路径添加到cwd
def convert_files_in_folder(word, folder):
# 将当前所有文件及文件夹添加进列表
files = os.listdir(folder)
# print('files:',files)
# 将所有以.doc(x)结尾的文件加入cwd path
pptfiles = [f for f in files if f.endswith((".doc", ".docx"))]
for pptfile in pptfiles:
# 加入判断,如果当前转换成的pdf已存在,就跳过不添加
print(pptfile)
if pptfile+'.pdf' in files :
break
# 加入cwd环境
fullpath = os.path.join(cwd, pptfile)
ppt_to_pdf(word, fullpath, fullpath) #第三步:将cwd路径下转换成pdf格式
def ppt_to_pdf(word, inputFileName, outputFileName, formatType = 17):
# 切片取后缀是否为pdf
if outputFileName[-3:] != 'pdf':
outputFileName = outputFileName + ".pdf"
# 调用接口进行转换
print(inputFileName)
deck = word.Documents.Open(inputFileName)
deck.SaveAs(outputFileName, formatType) # formatType = 17 for word to pdf
deck.Close() if __name__ == "__main__":
# 创建Word应用
word = init_word()
# 得到当前路径
cwd = os.getcwd()
# 打印当前路径
print(cwd)
# 调用Word进行转换cwd path下的doc(x)格式
convert_files_in_folder(word, cwd)
# 转换结束后关闭
word.Quit()
[Python Study Notes]批量将wold转换为pdf的更多相关文章
- [Python Study Notes]批量将ppt转换为pdf v1.0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- Python爬虫实战:将网页转换为pdf电子书
写爬虫似乎没有比用 Python 更合适了,Python 社区提供的爬虫工具多得让你眼花缭乱,各种拿来就可以直接用的 library 分分钟就可以写出一个爬虫出来,今天就琢磨着写一个爬虫,将廖雪峰的 ...
- [Python Study Notes]物体运动检测
基于opencv的cv2模块实现 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]匿名函数
Python 使用 lambda 来创建匿名函数. lambda这个名称来自于LISP,而LISP则是从lambda calculus(一种符号逻辑形式)取这个名称的.在Python中,lambda作 ...
- [Python Study Notes]字符串处理技巧(持续更新)
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]with的使用
在 Python 2.5 中, with 关键字被加入.它将常用的 try ... except ... finally ... 模式很方便的被复用.看一个最经典的例子: with open('fil ...
- [Python Study Notes]实现对键盘控制与监控
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]实现对鼠标控制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]WdSaveFormat 枚举
WdSaveFormat 枚举 指定要在保存文档时使用的格式. 版本信息 已添加版本: 名称 值 说明 wdFormatDocument 0 Microsoft Word 格式. wdFormatDO ...
随机推荐
- 动态查询:getBy字段名
http://www.php.cn/php/php-getBy.html 根据字段名动态查询:getBy字段名( ) 该方法很有意思,手册的说得很简略,我们根据源码来好好说道说道~~ 1. 功能:根据 ...
- mysql 批量更新与批量更新多条记录的不同值实现方法
作者: 字体:[增加 减小] 类型:转载 时间:2013-10-02 我要评论 在mysql中批量更新我们可能使用update,replace into来操作,下面小编来给各位同学详细介绍mysql ...
- u8g2库的相关资料
2017-12-1309:13:32更新51论坛上的帖子,大神自己写的库文件,待调试! http://www.51hei.com/bbs/forum.php?mod=viewthread&ti ...
- python3的一些改动常用到的
更多的内容会接下来说明,只举几个例子. https://docs.python.org/2/library/2to3.html 1. map的输出,要在前面加list转化 2. map(lambda ...
- python_面向对象
什么是面向对象? -- 一种主流编程范式,编程思维框架,世界主流两个方向,面向对象和面向过程. -- 面向是把关注点集中一个具体东西,比如看向手机,也叫面向手机,手机就是一个对象,我们 把手机的属性 ...
- springmvc配置文件配置的事务作用范围
作用于service,不是controller,也不是mapper.所以,要保证原子性,就放在一个serviceimpl里,而不要放在一个controller 里 第一次与数据库打交道时,事务开启,s ...
- 如何使用 volatile, synchronized, final 进行线程间通信
原文地址:https://segmentfault.com/a/1190000004487149.感谢作者的无私分享. 你是否真正理解并会用volatile, synchronized, final进 ...
- The server's host key is not cached in the registry. You have no guarantee that the server……
使用putty中的pscp.exe ,可以通过脚本方式实现windows向linux上传文件,但pscp.exe第一次运行时必须手工输入确认信息,本文主要解决掉初次运行时的人工交互,彻底实现静默运行. ...
- java常用类--系统相关
java提供了System类和Runtime类来与程序的运行平台进行交互. System类 System类代表java程序的运行平台,程序不能创建这个类的对象,System类提供了一些类field和方 ...
- git 安装使用
系统ubuntu14.04 1.安装:sudo apt-get install git 2.设置name and Email: git config --global user.name " ...