当我们的工程中、日常工作中遇到需要将 PPT 导出成图片的需求,借助一些 PPT 插件即可完成,但如果在 Linux 平台,或需要有大量 PPT 需要处理时,手动转换就会十分麻烦,我们可以借助 python 或 相应的程序,来自动地调用
office PowerPoint(Windows)
或Libreoffice(Linux)
来帮助我们完成需求
windows平台
windows平台下实现自动化比较简单,只需要使用第三方库调用 office PowerPoint
即可,需要借助 win32com
库实现,可通过 pip3 install pywin32
安装,转换的关键代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30import os import sys import win32com.client class WinConverter(): def __init__(self): self._ppt_engine = client.Dispatch('PowerPoint.Application') self._ppt_engine.Visible = True # 将单个ppt转换成图片 def _ppt2png(ppt_path, imgs_path, ppt_engine): ppt_obj = ppt_engine.Presentations.Open(ppt_path) ppt_obj.SaveAs(imgs_path, 17) ppt_obj.close() # 获取所有路径下所有ppt def _get_all_ppts(path): ppts_path_list = [] for root,folder,files in os.walk(path): for f in files: suffix = os.path.splitext(f)[-1].lower() if 'ppt' in suffix: ppt_path = os.path.join(root,f) ppts_path_list.append(ppt_path) return ppts_path_list def convert(self,root_path): ppts = self._get_all_ppts(root_path) for ppt in ppts: self._ppt2png(ppt) self._ppt_engine.Quit() converter = WinConverter() converter.convert(path)
注1:注意需要import win32com.client,不能 from win32com import client
注2:win32com 库也可以用于操作Excel,Word,Outlook软件,进行数据读取、文档解析、邮件发送等功能,是办公自动化中经常使用的库
Linux平台
Linux 平台下经过多种方法尝试,最终使用比较顺畅的方法是,先借助 libreoffice
将 ppt
文件转成 pdf
,再借助 imagemagick
将 pdf
转成图片
1.安装 libreoffice
1
2
3
4$ apt-get update $ apt-get upgrade $ apt-get install libreoffice
2. PPT 转 PDF
1
2$ libreoffice --headless --language=zh-CN --convert-to pdf ppt_path
若转出 pdf
中文为乱码,因为 Linux
平台下没有安装中文字体包,需要将 windows
上 PowerPoint
对应的常用字体包安装在 linux
上
1
2
3
4
5
6
7# 1.查看是否安装中文字体 $ fc-list :lang=zh # 2.将 windows 下的 C:WindowsFonts 下的中文自己拷贝到 /usr/share/fonbts 下,刷新字体缓存 $ fc-cache -f # 3.查看是否安装成功 $ fc-list :lang=zh
3.PDF 转 图片
1
2
3$ apt-get install imagemagick $ convert pdfPath 自定义字符%d.JPG
全平台通用脚本
可到 gitee仓库 下载
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134# -*- coding: UTF-8 -*- import os import sys def get_all_ppts(path): # 获取所有 ppt 文件 ppt_paths = [] for root, _, files in os.walk(path): for f in files: suffix = os.path.splitext(f)[-1].lower() if 'ppt' in suffix: ppt_paths.append(os.path.join(root,f)) return ppt_paths class LinuxConverter(): ''' Linux 平台下转换工具 借助 libreoffice 和 imagemagick ''' def _run_cmd(self, cmd): try: os.system(cmd) except Exception as e: print('[ERROR] ', e) return False else: return True def _ppt_to_imgs(self, ppt_path): # ppt - pdf - jpg # libreoffice 多进程会卡死,后续优化 cmd = 'libreoffice --headless --language=zh-CN ' cmd += '--convert-to pdf {}>>/dev/null'.format(ppt_path) success = self._run_cmd(cmd) if not success: print('[ERROR] ppt2pdf: {}'.format(ppt_path)) return success suffix = os.path.splitext(ppt_path)[-1] pdf_path = ppt_path.replace(suffix, 'pdf').split('/')[-1] success, _ = self._pdf_to_imgs(pdf_path) if not success: print('[ERROR] pdf2imgs: {}'.format(ppt_path)) return success def _pdf_to_imgs(self, pdf_path): imgs_folder = os.path.splitext(pdf_path)[0] cmd = 'mkdir {}'.format(imgs_folder) success = self._run_cmd(cmd) if not success: print('[ERROR] mkdir: {}'.format(pdf_path)) return success, '' cmd = 'convert {} {}/幻灯片%d.JPG'.format(pdf_path, imgs_folder) success = self._run_cmd(cmd) return success, imgs_folder def convert(self, ppts_path_list, total_count): error_count = 0 success_count = 0 for idx in range(total_count): ppt_path = ppts_path_list[idx] print('[ {}/{} ] {}'.format(idx+1, total_count, ppt_path)) success, _ = self._ppt_to_imgs(ppt_path) if not success: error_count += 1 continue success_count += 1 return error_count, success_count class WinConverter(): ''' Windows 平台下转换工具 借助 office PowerPoint ''' def __init__(self): try: # 必须以该形式导入 `from win32com import client` 会报错 import win32com.client except ImportError: print('当前为windows平台,缺少 win32com 库,请执行 `pip install pywin32` 安装') exit(0) self._ppt_engine = win32com.client.Dispatch('PowerPoint.Application') self._ppt_engine.Visible = True def _ppt2jpg(self, ppt_path, imgs_path): ppt_path = os.path.abspath(ppt_path) imgs_path = os.path.abspath(imgs_path) try: ppt = self._ppt_engine.Presentations.Open(ppt_path) ppt.SaveAs(imgs_path, 17) ppt.Close() except Exception as e: print('[ERROR] ppt2imgs: {}'.format(ppt_path)) return False else: return True def convert(self, ppts_path_list, total_count): error_count = 0 success_count = 0 for idx in range(total_count): ppt_path = ppts_path_list[idx] print('[ {}/{} ] {}'.format(idx+1, total_count, ppt_path)) suffix = os.path.splitext(ppt_path)[-1] imgs_path = ppt_path.replace(suffix,'.JPG') success = self._ppt2jpg(ppt_path,imgs_path) if not success: error_count += 1 continue success_count += 1 self._ppt_engine.Quit() return error_count, success_count def convert_ppts_to_imgs(path): if os.path.isdir(path): ppts_path_list = get_all_ppts(path) elif os.path.isfile(path): ppts_path_list = [path] if not ppts_path_list: print('该路径下未找到 ppt 文件') exit(0) plat = sys.platform if 'linux' in plat: converter = LinuxConverter() elif 'win' in plat: converter = WinConverter() total_count = len(ppts_path_list) print('[BEGIN] 共 {} 个 ppt 文件'.format(total_count)) error_count, success_count = converter.convert(ppts_path_list, total_count) print('[END] error:{} success:{}'.format(error_count, success_count)) if __name__ == '__main__': path = '.' convert_ppts_to_imgs(path)
最后
以上就是暴躁大米最近收集整理的关于python脚本1 - PPT导出为图片(Win&Linux)的全部内容,更多相关python脚本1内容请搜索靠谱客的其他文章。
发表评论 取消回复