2014-10-30 26 views
4

我目前正在尋找一種可能性,以在Tkinter應用程序內顯示PDF文件(例如在框架小部件或類似內容中顯示它們)。用於Python的PDF查看器Tkinter

這個問題已經有解決方案嗎?

我已經搜索過了,使用了ddg,但沒有找到任何相關信息。我發現的唯一的事情是如何將tk.Canvas的內容打印到PDF中 - 有沒有辦法將PDF加載到Canvas中?

回答

5

最後我找到了一個我可以使用的解決方案。

使用pypdfocr及其pypdfocr_gs庫我叫

pypdfocr.pypdfocr_gs.PyGs({}).make_img_from_pdf(pdf_file)

檢索JPG圖像,然後我用PIL從中獲得ImageTk.PhotoImage情況下,在我的代碼中使用它們。

ImageTk.PhotoImage(_img_file_handle) 

會盡快添加一個適當的例子。

編輯:

如這裏承諾而來的代碼


    import pypdfocr.pypdfocr_gs as pdfImg 
    from PIL import Image, ImageTk 
    import Tkinter as tk 
    import ttk 

    import glob, os 

    root=tk.Tk() 

    __f_tmp=glob.glob(pdfImg.PyGs({}).make_img_from_pdf("\tmp\test.pdf")[1])[0] 
    #       ^this is needed for a "default"-Config 
    __img=Image.open(__f_tmp) 

    __tk_img=ImageTk.PhotoImage(__img) 

    ttk.Label(root, image=__tk_img).grid() 

    __img.close() 
    os.remove(__f_tmp) 

    root.mainloop() 

編輯:

使用viranthas pypdfocr版本似乎是的Windows 10的處理中的錯誤和蟒蛇子過程:

# extract from pypdfocr_gs: 
def _run_gs(self, options, output_filename, pdf_filename): 
     try: 
      cmd = '%s -q -dNOPAUSE %s -sOutputFile="%s" "%s" -c quit' % (self.binary, options, output_filename, pdf_filename) 

      logging.info(cmd)   

      # Change this line for Windows 10: 
      # out = subprocess.check_output(cmd, shell=True) 
      out = subprocess.check_output(cmd) 
# end of extract 
0

您的搜索關鍵字是「python pdf parsing」。谷歌變成這個SO questionpdfminer。還有這個決定pdfminer的review是最好的選擇,但它比最新的pdfminer版本要舊兩年。還有Py32&3的pdfminer版本。

+0

感謝您的建議特里,但我不想解析它們,我想將它們帶入類似於包括縮放的文檔視圖的tkinter gui。 – R4PH43L 2014-10-30 21:01:09

+0

* Something *必須解析.pdf文件中的字節並將解析的對象轉換爲可顯示的位圖圖像。 – 2014-10-30 21:22:38

+0

你知道是否有可能「恢復」tk.canvas PostScript功能的過程? – R4PH43L 2014-10-31 17:09:17