使用转到pdf上的特定页面wx.lib.pdfwin\u旧

2024-10-02 04:37:37 发布

您现在位置:Python中文网/ 问答频道 /正文

看来wx.lib.pdfwin\u旧具有只转到pdf的上一页和下一页的函数。如何将其修改为转到pdf上的特定页面或转到pdf上的特定页面的代码是什么?你知道吗

import  wx
if wx.Platform == '__WXMSW__':
    from wx.lib.pdfwin_old import PDFWindow

class MyPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=-1)
        self.pdf = None

        sizer = wx.BoxSizer(wx.VERTICAL)


        self.pdf = PDFWindow(self, size=(parent.GetSize().width,parent.GetSize().height*3/5),style=wx.SUNKEN_BORDER)
        sizer.Add(self.pdf, proportion=3, flag=wx.EXPAND|wx.ALL)

        btn = wx.Button(self, wx.NewId(), "Open PDF File")
        self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
        sizer.Add(btn, proportion=1, flag=wx.EXPAND|wx.ALL)

        self.SetSizer(sizer)

    def OnOpenButton(self, event):
        global current_pdf_path
        dlg = wx.FileDialog(self, wildcard="*.pdf")
        if dlg.ShowModal() == wx.ID_OK:
            wx.BeginBusyCursor()
            print(dlg.GetPath())
            self.pdf.LoadFile(dlg.GetPath())
            wx.EndBusyCursor()

        dlg.Destroy()

app = wx.App()
frame = wx.Frame(None, -1, "PDFWindow", size = (640, 480))
MyPanel(frame)
frame.Show(True)
app.MainLoop()

Tags: importselfifpdflib页面frameparent
1条回答
网友
1楼 · 发布于 2024-10-02 04:37:37

你试过了吗

setCurrentPage(n)

来自pdfwin_旧的.py地址:

def setCurrentPage(self, npage):
    """
    Goes to the specified page in the document.  Maintains the
    current location within the page and zoom level.  npage is
    the page number of the destination page.  The first page
    in a document is page 0.

    ## Oh no it isn't! The first page is 1 (dfh)
    """
    if self.ie.Document:
        return self.ie.Document.setCurrentPage(npage)
    else:
        raise PDFWindowError()

编辑: 对不起,博伊肯,我帮不了你!我没有意识到pdfwin仅仅是windows,尽管名字里有线索。我在Linux上运行。
作为安慰,我在下面提供了这段代码,一个使用浏览器的方法,它可能会也可能不会给你一些里程。(Python3)

import  wx
import webbrowser

class MyPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, id=-1)
        sizer = wx.BoxSizer(wx.VERTICAL)
        btn = wx.Button(self, wx.NewId(), "Open PDF File")
        self.Bind(wx.EVT_BUTTON, self.OnOpenButton, btn)
        sizer.Add(btn, 0, flag=wx.EXPAND|wx.ALL)
        self.SetSizer(sizer)

    def OnOpenButton(self, event):
        dlg = wx.FileDialog(self, wildcard="*.pdf")
        if dlg.ShowModal() == wx.ID_OK:
            url = dlg.GetPath()
        dlg.Destroy()
        try:
            if not url:
                return
        except:
            return
        url = "file://"+url+'#page=12'
        url = url.encode('UTF-8')
        print(url)
        browser_list = ("chromium-browser","firefox")
        for x in browser_list:
            try:
                browser = webbrowser.get(x)
            except:
                pass
            else:
                browser.open(url)
                break
        try:
            if browser:
                pass
        except:
            print("Neither Supported browsers Firefox or Chromium found")

app = wx.App()
frame = wx.Frame(None, -1, "PDF Via Browser", size = (640, 480))
MyPanel(frame)
frame.Show(True)
app.MainLoop()

chromium浏览器似乎是最准确的。
就我个人而言,为了在我的项目中显示help,我在主文档中使用sections为每个主题创建了一个mainpdf和一个特定于内容的pdf。随着文档的发展,这种方法避免了页码变化时出现的问题。
我建议你不要接受这个答案,因为它显然不能回答你的具体问题。你知道吗

相关问题 更多 >

    热门问题