参考:

https://blog.csdn.net/ynyn2013/article/details/49120731

https://www.jianshu.com/p/f57cc64b9f5e

一、将 doc 转为 pdf

1、install 依赖

pip install pywin32

2、直接调用win32com接口打开文件,另存为pdf。SaveAs中的参数17代表村委pdf格式,完了关闭文件,关闭word。

 1     def doc2pdf(self):
2 try:
3 w = Dispatch("Word.Application")
4 doc = w.Documents.Open(self.docPath, ReadOnly=1)
5 doc.SaveAs(self.pdfPath, 17)
6 except:
7 traceback.print_exc()
8 finally:
9 doc.Close()
10 w.Quit()
11 self.checkFile(self.pdfPath, 'pdf')

以下为文件格式对应表

wdFormatDocument = 0
wdFormatDocument97 = 0
wdFormatDocumentDefault = 16
wdFormatDOSText = 4
wdFormatDOSTextLineBreaks = 5
wdFormatEncodedText = 7
wdFormatFilteredHTML = 10
wdFormatFlatXML = 19
wdFormatFlatXMLMacroEnabled = 20
wdFormatFlatXMLTemplate = 21
wdFormatFlatXMLTemplateMacroEnabled = 22
wdFormatHTML = 8
wdFormatPDF = 17
wdFormatRTF = 6
wdFormatTemplate = 1
wdFormatTemplate97 = 1
wdFormatText = 2
wdFormatTextLineBreaks = 3
wdFormatUnicodeText = 7
wdFormatWebArchive = 9
wdFormatXML = 11
wdFormatXMLDocument = 12
wdFormatXMLDocumentMacroEnabled = 13
wdFormatXMLTemplate = 14
wdFormatXMLTemplateMacroEnabled = 15
wdFormatXPS = 18

  

二、将pdf转为图片

1、install 依赖

1.1、pip isntall PyMuPDF

1.2、Windows安装配置poppler
Windows用户必须为Windows安装poppler (http://blog.alivate.com.au/poppler-windows/),然后将bin/文件夹添加到PATH(开始>输入env>编辑系统环境变量>环境变量...>系统变量>Path)

安装完poppler需重启系统后生效。

 
2、将pdf转为图片
 1     def pdf2image(self):
2 # 建立图片文件夹
3 if not os.path.exists(self.imgFold):
4 os.mkdir(self.imgFold)
5
6 # 转存图片
7 pages = fitz.open(self.pdfPath)
8 for page in pages:
9 imgPath = os.path.join(self.imgFold, str(page.number)+'.jpg')
10 pix = page.getPixmap()
11 pix.writeImage(imgPath)
12 self.checkFile(imgPath, 'last img')

三、直接将word转为图片

方法:结合1,2

代码如下:

 1 '''
2 @Author: haikuoxin
3 @Date: 2019-11-11 11:21:12
4 @Last Modified by: haikuoxin
5 @Last Modified time: 2019-11-11 11:21:12
6 '''
7 import os
8 import time
9 import traceback
10 import fitz
11 from win32com.client import Dispatch
12 import comtypes.client
13
14
15 class Word2Pdf2Img():
16 def __init__(self, docPath):
17 # 初始化路径
18 self.docPath = docPath
19 self.fileName = os.path.basename(self.docPath).split('.')[0]
20 self.fileFold = os.path.dirname(self.docPath)
21 self.pdfPath = os.path.join(self.fileFold, self.fileName + '.pdf')
22 self.imgFold = os.path.join(self.fileFold, self.fileName)
23
24 @staticmethod
25 def checkFile(filePath, fileType=''):
26 if os.path.isfile(filePath):
27 print ('file {} existed!'.format(fileType))
28 else:
29 print ('file {} not existed!'.format(fileType))
30
31 def doc2pdf(self):
32 # try:
33 # doc = w.Documents.Open(self.docPath, ReadOnly=1)
34 # doc.SaveAs(self.pdfPath, 17)
35 # doc.Close()
36 # except:
37 # traceback.print_exc()
38 # finally:
39 # w.Quit()
40
41 # w = Dispatch("Word.Application")
42 # doc = w.Documents.Open(self.docPath, ReadOnly=1)
43 # doc.SaveAs(self.pdfPath, 17)
44 # doc.Close()
45 # w.Quit()
46
47 w = comtypes.client.CreateObject('Word.Application')
48 w.Visible = True
49 time.sleep(3)
50 doc = w.Documents.Open(self.docPath)
51 doc.SaveAs(self.pdfPath, FileFormat=17)
52 doc.Close()
53 w.Quit()
54 # os.system('taskkill /f /im WINWORD.EXE')
55 self.checkFile(self.pdfPath, 'pdf')
56
57 def pdf2image(self):
58 # 建立图片文件夹
59 if not os.path.exists(self.imgFold):
60 os.mkdir(self.imgFold)
61
62 # 转存图片
63 pages = fitz.open(self.pdfPath)
64 for page in pages:
65 imgPath = os.path.join(self.imgFold, str(page.number)+'.jpg')
66 pix = page.getPixmap()
67 pix.writeImage(imgPath)
68 self.checkFile(imgPath, 'last img')
69
70 def doc2image(self):
71 self.doc2pdf()
72 self.pdf2image()
73
74 def run():
75 dataFold = r'C:\Users\Administrator\Desktop\chatuClass\data\online'
76 docPaths = [os.path.join(dataFold, name) for name in os.listdir(dataFold)]
77 docCnt = len(docPaths)
78 errorCnt = 0
79 for i, docPath in enumerate(docPaths[:]):
80 if docPath=='':
81 continue
82
83 try:
84 cvter = Word2Pdf2Img(docPath)
85 print (docCnt, i, errorCnt, cvter.fileName)
86 cvter.doc2image()
87 except:
88 errorCnt += 1
89 traceback.print_exc()
90
91 if __name__ == "__main__":
92 run()
93 # docPath = r'C:\Users\Administrator\Desktop\chatuClass\data\test\b.docx'
94 # cvter = Word2Pdf2Img(docPath)
95 # cvter.doc2pdf()

自动化将 word 转为 pdf,再将pdf转为图片!的更多相关文章

  1. CAD转PDF再由pdf转jpg图片

    免费的PDF转JPG图片 https://www.gaitubao.com/pdf-to-jpg/

  2. 将Word转为带书签的PDF

    将word文档存为PDF可以带来很多便利,在这里就不多说了.下面讨论一下转换方法. 我现在使用的是Word2010+Acrobat9,所以这里仅讨论使用这种组合的转换方法. 在Word2010中有两种 ...

  3. Python将word文档批量转PDF

    前面有一篇<Python批量创建word文档(2)- 加图片和表格>的文章,利用这篇文章创建的word文档来批量转PDF文档.代码: 1 ''' 2 #python批量将word文档转换成 ...

  4. java 如何将 word,excel,ppt如何转pdf --openoffice (1)

    承上启下,可折叠 上一篇说的是:服务器是windows server时,用jacob将msoffice(指的是word,excel,ppt)转换成pdf. 若被部署项目的服务器是centOS等linu ...

  5. Jacob工具类使用文件互转服务 word转html html转excel word转pdf excel转pdf ppt转pdf

    前提条件  必须安装MS office 1.jdk使用jdk1.8 2.jacob.dll放在..\jdk1.8\jre\bin目录下 3.eclipse的jre版本要和jdk一致,window-&g ...

  6. word,excel,ppt,txt转换为 PDF

    /// <summary> /// 将word文档转换成PDF格式 /// </summary> /// <param name="sourcePath&quo ...

  7. 将Word,PDF文档转化为图片

    #region 将Word文档转化为图片 /// <summary> /// 将Word文档转化为图片 /// </summary> /// <param name=&q ...

  8. java实现word,ppt,excel,jpg转pdf

    word,excel,jpeg 转 pdf 首先下载相关jar包:http://download.csdn.net/detail/xu281828044/6922499 import java.io. ...

  9. PDF转WORD工具推荐:迅捷PDF转换器6.8版

    迅捷PDF转换器是一款功能强大的pdf转换成word转换器软件.它不仅支持pdf转换成word,word转pdf,还可以实现excel,ppt,jpg与pdf文件的一键式转换.有了它,你可以很轻松转换 ...

  10. 用java将简单的word文档换成pdf文档

    用java将简单的word文档换成pdf文档的方式很多,因为很多都没有实际测试过,所以这里就先泛泛的说一下 整体上来看分两种: 1.纯java代码实现,有很多优秀的开源软件可以用,比如poi,itex ...

随机推荐

  1. 【Oracle】instr()函数详解

    1)instr()函数的格式  (俗称:字符查找函数) 格式一:instr( string1, string2 )    /   instr(源字符串, 目标字符串) 格式二:instr( strin ...

  2. Web安全之CSRF(跨站请求伪造)

    CSRF(跨站请求伪造)概述 Cross-site request forgery 简称为"CSRF",在CSRF的攻击场景中攻击者会伪造一个请求(这个请求一般是一个链接),然后欺 ...

  3. 1 分钟上手,在容器中运行 Visual Studio Code

    https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers 这个插件允许我们在容器中运 ...

  4. 解决Python内CvCapture视频文件格式不支持问题

    解决Python内CvCapture视频文件格式不支持问题 在读取视频文件调用默认的摄像头cv.VideoCapture(0)会出现下面的视频格式问题 CvCapture_MSMF::initStre ...

  5. centos下解压rar文件,Linux解压tar.gz和tar.bz2的命令

    1.下载:根据主机系统下载合适的版本,当前64为centos系统演示下载: wget http://www.rarlab.com/rar/rarlinux-x64-5.3.0.tar.gz 2.解压安 ...

  6. Windows server 2008常用优化设置

    1. 如何取消开机按 CTRL+ALT+DEL登录? 控制面板→管理工具→本地安全策略→本地策略→安全选项→交互式登录:无须按CTRL+ALT+DEL→启用. 2. 如何取消关机时出现的关机理由选择项 ...

  7. SpringMVC Tomcat 启动时报错:java.lang.IllegalStateException: Error starting child

    大概原因如下: 1.Controller里RequestMapping("/test")前面没有"/"; 2.jar包冲突,比如我的将数据库连接版本由5.1.6 ...

  8. Covering Indexes in MySQL, PostgreSQL, and MongoDB

    Covering Indexes in MySQL, PostgreSQL, and MongoDB - Orange Matter https://orangematter.solarwinds.c ...

  9. VS Code 使用教程详解

    一.写在前面 1.为什么选择 \(VS\) \(code\) 一款非常好用的代码编辑器 标准化 \(Language\) \(Service\) \(Protocol\) 内置调试器和标准化 \(De ...

  10. SpringCloud-常用组件介绍

    SpringCloud-常用组件介绍 分布式系统开发用于分布式环境(多个服务器不在同一个机房,同一个业务服务在多台服务器运行) Spring Cloud 是基于Springboot的分布式云服务架构, ...