PDF To Excel是一款免費(fèi)在線PDF轉(zhuǎn)換成Excel轉(zhuǎn)換器,根據(jù)pdf轉(zhuǎn)excel的輪子,優(yōu)化的小工具添加了幾個選項。直接上代碼,我直接pyinstaller打包了,做了個UI!
代碼說明
#-*-coding:utf-8-*-
import tkinter as tk
import tkinter.messagebox
import pdfplumber
import xlwt
from tkinter import filedialog
import os
def file_browse():
file_path=filedialog.askopenfilename(parent=main_win,initialdir='./',initialfile='tmp',filetypes=[("PDF","*.pdf"),("All files","*")])
#獲取文件路徑
#a=os.path.split(file_path)
my_file_name.set(file_path)
def page_tran():
workbook = xlwt.Workbook() #定義workbook
sheet = workbook.add_sheet('Sheet1') #添加sheet
i = 0 # Excel起始位置
path = my_file_name.get()
print(path) # 導(dǎo)入PDF路徑
with pdfplumber.open(path) as pdf:
w_label4.insert('end','開始讀取數(shù)據(jù)...')
p=my_page.get()-1 #頁數(shù)從0開始
# 獲取當(dāng)前頁面的全部文本信息,包括表格中的文字
page=pdf.pages[p]
# 獲取當(dāng)前頁面的全部文本信息,包括表格中的文字
for table in page.extract_tables():
for row in table:
for j in range(len(row)):
sheet.write(i, j, row[j])
i += 1
print('---------- 分割線 ----------')
path = my_file_name.get()
a=os.path.split(path)
b=a[1].split('.')
ex_name=a[0]+'/'+b[0]+'-p'+str(p+1)+'.xls'
workbook.save(ex_name)
w_label4.insert('end','excel轉(zhuǎn)換成功\n')
def index_tran():
workbook = xlwt.Workbook() #定義workbook
sheet = workbook.add_sheet('Sheet1') #添加sheet
i = 0 # Excel起始位置
path = my_file_name.get()
#print(path) # 導(dǎo)入PDF路徑
with pdfplumber.open(path) as pdf:
w_label4.insert('end', '開始讀取數(shù)據(jù)...')
first=my_index1.get()-1
last=my_index2.get()
ind_list=list(range(first,last))
for k in ind_list:
# 獲取當(dāng)前頁面的全部文本信息,包括表格中的文字
page=pdf.pages[k]
# 獲取當(dāng)前頁面的全部文本信息,包括表格中的文字
for table in page.extract_tables():
for row in table:
for j in range(len(row)):
sheet.write(i, j, row[j])
i += 1
print('---------- 分割線 ----------')
path = my_file_name.get()
a=os.path.split(path)
b=a[1].split('.')
ex_name=a[0]+'/'+b[0]+'-p'+str(first+1)+'-'+str(last)+'.xls'
workbook.save(ex_name)
w_label4.insert('end','excel轉(zhuǎn)換成功\n')
def all_tran():
workbook = xlwt.Workbook() #定義workbook
sheet = workbook.add_sheet('Sheet1') #添加sheet
i = 0 # Excel起始位置
path = my_file_name.get()
print(path) # 導(dǎo)入PDF路徑
with pdfplumber.open(path) as pdf:
w_label4.insert('end', '開始讀取數(shù)據(jù)...')
for page in pdf.pages:
# 獲取當(dāng)前頁面的全部文本信息,包括表格中的文字
for table in page.extract_tables():
for row in table:
for j in range(len(row)):
sheet.write(i, j, row[j])
i += 1
print('---------- 分割線 ----------')
path = my_file_name.get()
a=os.path.split(path)
b=a[1].split('.')
ex_name=a[0]+'/'+b[0]+'.xls'
workbook.save(ex_name)
w_label4.insert('end','excel轉(zhuǎn)換成功\n')
def tran2ex():
print(chkVar1.get())
if chkVar1.get()==1:
print(chkVar1.get())
page_tran()
if chkVar1.get()==2:
print(chkVar2.get())
index_tran()
if chkVar1.get()==3:
print(chkVar3.get())
all_tran()
main_win=tk.Tk()
main_win.title('PDF to EXCEL Created by Wolf')
main_win.geometry('450x240')
main_win.resizable(width=True, height=True)
w_label1=tk.Label(main_win,text='請選擇PDF文件:')
w_label3=tk.Label(main_win,text='到')
w_label4=tk.Text(main_win,width=40,height=1)
#標(biāo)簽
my_file_name=tk.StringVar()
my_entry=tk.Entry(main_win,width=30,textvariable=my_file_name)
#輸入kuang
my_page=tk.IntVar()
my_entry_numb3=tk.Entry(main_win,width=5,textvariable=my_page)
my_index1=tk.IntVar()
my_entry_numb1=tk.Entry(main_win,width=5,textvariable=my_index1)
my_index2=tk.IntVar()
my_entry_numb2=tk.Entry(main_win,width=5,textvariable=my_index2)
chkVar1=tk.IntVar()
my_chk3=tk.Radiobutton(main_win,text='全部',variable=chkVar1,value=3)
my_chk1=tk.Radiobutton(main_win,text='單頁 ',variable=chkVar1,value=1)
my_chk2=tk.Radiobutton(main_win,text='頁數(shù)從',variable=chkVar1,value=2)
#單選按鈕
my_button3=tk.Button(main_win,text='瀏覽',command=file_browse)
#瀏覽文件按鈕
my_button1=tk.Button(main_win,width=10,text='轉(zhuǎn)換',command=tran2ex)
my_button2=tk.Button(main_win,width=10,text='退出',command=main_win.quit)
w_label1.place(x=50,y=40)
my_entry.place(x=140,y=40)
my_button3.place(x=360,y=40)
my_chk2.place(x=50,y=100)
my_entry_numb1.place(x=120,y=100)
w_label3.place(x=160,y=100)
my_entry_numb2.place(x=180,y=100)
my_chk1.place(x=50,y=75)
my_entry_numb3.place(x=120,y=75)
my_chk3.place(x=50,y=130)
w_label4.place(x=60,y=160)
my_button1.place(x=120,y=200)
my_button2.place(x=280,y=200)
main_win.mainloop()
使用方法
1、在本極速下載下載解壓文件,解壓后找到“PDF2excel.exe”雙擊打開
2、稍等片刻就會出現(xiàn)下圖的界面,歡迎使用