欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文教會(huì)你用Python實(shí)現(xiàn)pdf轉(zhuǎn)word

 更新時(shí)間:2023年01月04日 10:49:04   作者:Josepyth  
python實(shí)現(xiàn)pdf轉(zhuǎn)word,支持中英文轉(zhuǎn)換,轉(zhuǎn)換精度高,可以達(dá)到使用效果,下面這篇文章主要給大家介紹了關(guān)于用Python實(shí)現(xiàn)pdf轉(zhuǎn)word的相關(guān)資料,需要的朋友可以參考下

一、實(shí)驗(yàn)?zāi)繕?biāo)

通過利用python中的pyinstaller庫和PySimpleGUI庫,實(shí)現(xiàn)將pdf轉(zhuǎn)為word的實(shí)際功能。

二、實(shí)驗(yàn)準(zhǔn)備

1、安裝pdf2docx庫

pip install pdf2docx

2、安裝PySimpleGUI庫

pip install PySimpleGUI

三、實(shí)驗(yàn)代碼

from pdf2docx import Converter
import PySimpleGUI as sg
 
 
def pdf2word(file_path):
    file_name = file_path.split('.')[0]
    doc_file = f'{file_name}.docx'
    p2w = Converter(file_path)
    p2w.convert(doc_file, start=0, end=None)
    p2w.close()
    return doc_file
 
 
def main():
    # 選擇主題
    sg.theme('DarkAmber')
 
    layout = [
        [sg.Text('pdfToword', font=('微軟雅黑', 12)),
         sg.Text('', key='filename', size=(50, 1), font=('微軟雅黑', 10))],
        [sg.Output(size=(80, 10), font=('微軟雅黑', 10))],
        [sg.FilesBrowse('選擇文件', key='file', target='filename'), sg.Button('開始轉(zhuǎn)換'), sg.Button('退出')]]
    # 創(chuàng)建窗口
    window = sg.Window("張臥虎", layout, font=("微軟雅黑", 15), default_element_size=(50, 1))
    # 事件循環(huán)
    while True:
        # 窗口的讀取,有兩個(gè)返回值(1.事件;2.值)
        event, values = window.read()
        print(event, values)
 
        if event == "開始轉(zhuǎn)換":
 
            if values['file'] and values['file'].split('.')[1] == 'pdf':
                filename = pdf2word(values['file'])
                print('文件個(gè)數(shù) :1')
                print('\n' + '轉(zhuǎn)換成功!' + '\n')
                print('文件保存位置:', filename)
            elif values['file'] and values['file'].split(';')[0].split('.')[1] == 'pdf':
                print('文件個(gè)數(shù) :{}'.format(len(values['file'].split(';'))))
                for f in values['file'].split(';'):
                    filename = pdf2word(f)
                    print('\n' + '轉(zhuǎn)換成功!' + '\n')
                    print('文件保存位置:', filename)
            else:
                print('請(qǐng)選擇pdf格式的文件哦!')
        if event in (None, '退出'):
            break
 
    window.close()
main()

四、部分代碼解析

1、pdf轉(zhuǎn)word代碼主體部分

def pdf2word(file_path):
    file_name = file_path.split('.')[0]
    doc_file = f'{file_name}.docx'
    p2w = Converter(file_path)
    p2w.convert(doc_file, start=0, end=None)
    p2w.close()
    return doc_file

convert(doc_file,start,end)函數(shù)中

doc_file:轉(zhuǎn)化完成后文件名

start:轉(zhuǎn)化開始頁面

end:轉(zhuǎn)化結(jié)束頁面

注意點(diǎn):

①若不給start,end參數(shù)則默認(rèn)轉(zhuǎn)化全篇

②對(duì)于不連續(xù)的頁面,也可寫作convert(doc_file , pages = [2,4,6])

2、圖形化界面部分

sg.theme('default1')
layout = [[sg.Text('pdfToword', font=('微軟雅黑', 12)),
        sg.Text('', key='filename', size=(50, 1), font=('微軟雅黑', 10))],
        [sg.Output(size=(80, 10), font=('微軟雅黑', 10))],
        [sg.FilesBrowse('選擇文件', key='file', target='filename'), sg.Button('開始轉(zhuǎn)換'),sg.Button('退出')]]
window = sg.Window("id", layout, font=("微軟雅黑", 15), default_element_size=(50, 1))

theme表示主題,即圖形化界面的主題顏色(sg.theme_list()可以查看所有可用主題)

3、實(shí)現(xiàn)多個(gè)文件轉(zhuǎn)化

if event == "開始轉(zhuǎn)換":
 
    if values['file'] and values['file'].split('.')[1] == 'pdf':
        filename = pdf2word(values['file'])
        print('文件個(gè)數(shù) :1')
        print('\n' + '轉(zhuǎn)換成功!' + '\n')
        print('文件保存位置:', filename)
    elif values['file'] and values['file'].split(';')[0].split('.')[1] == 'pdf':
        print('文件個(gè)數(shù) :{}'.format(len(values['file'].split(';'))))
        for f in values['file'].split(';'):
            filename = pdf2word(f)
            print('\n' + '轉(zhuǎn)換成功!' + '\n')
            print('文件保存位置:', filename)
    else:
        print('請(qǐng)選擇pdf格式的文件')

①第一個(gè)if處,通過“.”進(jìn)行劃分,判斷是否為單個(gè)且pdf格式文件

②else if處,通過“;”劃分,將所有文件的存儲(chǔ)路徑儲(chǔ)存在一個(gè)數(shù)組中,利用循環(huán)來遍歷數(shù)組,從而達(dá)到依次轉(zhuǎn)化的目的

③else處,若傳入非pdf文件則輸出提示文字

五、結(jié)果

220220a570b445aea523fc3ea03f0c6f.png

完結(jié)撒花!

總結(jié)

到此這篇關(guān)于用Python實(shí)現(xiàn)pdf轉(zhuǎn)word的文章就介紹到這了,更多相關(guān)Python實(shí)現(xiàn)pdf轉(zhuǎn)word內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論