linux ubuntu下怎样将pdf格式文件转换为doc格式文件,Ubuntu环境下把word文档转成pdf,把pdf文件转成jpg...

环境搭建

使用语言 python3

安装imagemagick(pdf转jpg是内部需要调用到此工具)

apt-get install imagemagick

安装libreoffice(此工具用于将word文档转化成pdf文件)

apt-get install libreoffice

安装python wand,PIL库

pip install wand

pip install PIL

PDF转JPG

先转png,再转jpg是为了避免出现黑色,透明等背景,造成转换出来的图片与pdf文件显示不一样

from PIL import Image as Image2

from wand.image import Image

from wand.color import Color

def convert_pdf_to_jpg(filename):

end_length = len(filename.split('.')[-1]) + 1

title = filename[0:-end_length]

title = title.split('/')[-1]

#resolution为分辨率,background为背景颜色

with Image(filename=filename, resolution=150, background=Color('White')) as img :

#页数

length = len(img.sequence)

#如果页数超过1页,生成的文件名会依次加上页码数

with img.convert('png') as converted:

path = 'static/local_images/%s.png' % title

converted.save(filename=path)

image_list = []

if length == 1:

path = 'static/local_images/%s.png' % title

image_list.append(path)

else:

for i in range(0, length):

path = 'static/local_images/%s-%d.png' % (title, i)

image_list.append(path)

jpg_list = []

for img in image_list:

image = Image2.open(img)

x,y = image.size

background = Image2.new('RGBA', image.size, (255,255,255))

try:

background.paste(image, (0, 0, x, y), image)

image = background.convert('RGB')

except:

image = image.convert('RGBA')

background.paste(image, (0, 0, x, y), image)

image = background.convert('RGB')

title = img.split('.')[0]

name = title + '.jpg'

image.save(name)

os.remove(img)

name = "%s/%s" %(static_host, name)

jpg_list.append(name)

return jpg_list

word文档转PDF

python没有直接把word转换成pdf文档的库,只能先安装libreoffice工具,然后利用os库系统调用libreoffice工具

import os

def convert_doc_to_pdf(filename):

end_length = len(filename.split('.')[-1]) + 1

name = filename[0:-end_length]

cmd = 'libreoffice --convert-to pdf %s' % filename

os.system(cmd)

name = name.split('/')[-1] + '.pdf'

return name

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值