"I use VSCode to execute the following code normally, but when I use pyinstaller --onefile --windowed pdf_excel.py to generate an executable file, an error message appears: 'NoneType' object has no attribute 'PdfDocument_Create'. How should I troubleshoot this issue?"
import tkinter as tk
from tkinter import filedialog, messagebox
from spire.pdf.common import *
from spire.pdf import *
def select_pdf_file():
file_path = filedialog.askopenfilename(filetypes=[("PDF files", "*.pdf")])
if file_path:
pdf_path.set(file_path)
def select_save_location():
save_path = filedialog.asksaveasfilename(defaultextension=".xlsx",
filetypes=[("Excel files", "*.xlsx")])
if save_path:
excel_path.set(save_path)
def convert_pdf_to_excel():
try:
pdf_file = pdf_path.get()
excel_file = excel_path.get()
if not pdf_file or not excel_file:
raise ValueError("Please select both PDF file and save location.")
# 創建PDFDocument對象
pdf = PdfDocument()
# 加載PDF文件
pdf.LoadFromFile(pdf_file)
# 創建XlsxLineLayoutOptions對象來指定轉換選項
convertOptions = XlsxLineLayoutOptions(True, True, True, True, False)
# 設置轉換選項
pdf.ConvertOptions.SetPdfToXlsxOptions(convertOptions)
# 將PDF文件保存為EXCEL XLSX格式
pdf.SaveToFile(excel_file, FileFormat.XLSX)
pdf.Close()
messagebox.showinfo("完成!", "PDF檔案已轉為EXCEL!!")
except Exception as e:
messagebox.showerror("Error", str(e))
# 創建主窗口
root = tk.Tk()
root.title("PDF to Excel Converter")
# PDF文件路徑
pdf_path = tk.StringVar()
tk.Label(root, text="選取 PDF 檔案:").grid(row=0, column=0, padx=10, pady=10)
tk.Entry(root, textvariable=pdf_path, width=50).grid(row=0, column=1, padx=10, pady=10)
tk.Button(root, text="瀏覽...", command=select_pdf_file).grid(row=0, column=2, padx=10, pady=10)
# Excel文件保存路徑
excel_path = tk.StringVar()
tk.Label(root, text="Excel 檔案儲存位置:").grid(row=1, column=0, padx=10, pady=10)
tk.Entry(root, textvariable=excel_path, width=50).grid(row=1, column=1, padx=10, pady=10)
tk.Button(root, text="瀏覽...", command=select_save_location).grid(row=1, column=2, padx=10, pady=10)
# 轉換按鈕
tk.Button(root, text="PDF To EXCEL", command=convert_pdf_to_excel).grid(row=2, column=0, columnspan=3, pady=20)
# 運行主循環
root.mainloop()