ubuntu下使用python将ppt转成图片_在Ubuntu下,python将PPT和word转换为PDF,ubuntu,pptword,pdf...

前言:

在我以前的博客中有这么两篇博客,一篇是:

python将ppt文件转换为jpg图片

. 这两篇博客都是讲如何将word,ppt转换为图片,但是这些方法都是在win系统中实现的,而在ubuntu下根本无法运行代码.原因时转换过程中借用的时win的接口库win32com. 这个库bantu压根不存在.所以以前的方法在ubuntu系统就不能实现了,只能另寻出路所幸,皇天不服有心人,在经过多日查找,终于找到解决方法.

ppt转换为pdf:

准备工作:安装 subprocess 这个库

打开终端,执行以下语句:

sudo pip3 install subprocess

安装库以后就好办了,执行一下代码便可以实现转换.

import subprocess

from subprocess import Popen, PIPE

def ppt_to_pdf(self, outfile, infile, timeout=None):

"""将ppt 转换为pdf

函数说明:将路径为infile的ppt文件转换为pdf,保存进路径为outfile的pdf文件.

参数: outfile(str):保存文件pdf 的路径.

参数: infile(str):ppt文件的路径.

参数: timeout:转换文件时的时间延迟.

"""

args = ['libreoffice', '--headless', '--convert-to', 'pdf', '--outdir',outfile, infile]

process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)

re.search('-> (.*?) using filter', process.stdout.decode())

同理,将word转换为pdf

import subprocess

from subprocess import Popen, PIPE

def word_to_pdf(self,outfile, infile, timeout=None):

"""将word 转换为pdf

函数说明:将路径为infile的word文件转换为pdf,保存进路径为outfile的pdf文件.

参数: outfile(str):保存文件pdf 的路径.

参数: infile(str):word文件的路径.

参数: timeout:转换文件时的时间延迟.

"""

args = ['libreoffice', '--headless', '--convert-to', 'pdf', '--outdir', outfile, infile]

process = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)

re.search('-> (.*?) using filter', process.stdout.decode())

将word,ppt转换为pdf 的方法是一样的,只不过是输入文件不一样罢了.

下面顺便也提一下将pdf转换为图片吧

首先要先安装 fitz 这个库,打开终端使用以下代码.

sudo pip3 install fitx

转换的代码:

from PyQt5 import QtGui

import fitz

pdf = fitz.open(file)

for pg in range(pdf.pageCount):

page = pdf.loadPage(pg) # 使用循环将所有转换为图片。

pagePixmap = page.getPixmap()

# 获取 image 格式

imageFormat = QtGui.QImage.Format_RGB888

# 生成 QImage 对象

pageQImage = QtGui.QImage(pagePixmap.samples, pagePixmap.width,pagePixmap.height, pagePixmap.stride, imageFormat)

pageQImage.save(file1 + '/image' + '%s.jpg' % (pg + 1))

pdf.close()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值