天天看點

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