import fitz
import wx
class PDFExtractor(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(self, parent, id=wx.ID_ANY, title=u"PDF Extractor", pos=wx.DefaultPosition,
size=wx.Size(500, 254), style=wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL,
name=u"PDF Extractor")
self.SetSizeHintsSz(wx.DefaultSize, wx.DefaultSize)
self.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_WINDOW))
self.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_ACTIVECAPTION))
bSizer2 = wx.BoxSizer(wx.VERTICAL)
self.m_filePicker2 = wx.FilePickerCtrl(self, wx.ID_ANY, wx.EmptyString, u"Select a file", u"*.*",
wx.DefaultPosition, wx.DefaultSize, wx.FLP_DEFAULT_STYLE)
self.m_filePicker2.SetFont(wx.Font(9, 74, 90, 92, False, "微软雅黑"))
self.m_filePicker2.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT))
self.m_filePicker2.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_HIGHLIGHT))
bSizer2.Add(self.m_filePicker2, 0, wx.ALL | wx.EXPAND, 5)
self.m_staticText5 = wx.StaticText(self, wx.ID_ANY, u"Start Page:", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText5.Wrap(-1)
self.m_staticText5.SetFont(wx.Font(9, 74, 90, 92, True, "微软雅黑"))
self.m_staticText5.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT))
bSizer2.Add(self.m_staticText5, 0, wx.ALL, 5)
self.m_textCtrl1 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
bSizer2.Add(self.m_textCtrl1, 0, wx.EXPAND, 5)
self.m_staticText6 = wx.StaticText(self, wx.ID_ANY, u"End Page:", wx.DefaultPosition, wx.DefaultSize, 0)
self.m_staticText6.Wrap(-1)
self.m_staticText6.SetFont(wx.Font(9, 74, 90, 92, True, "微软雅黑"))
self.m_staticText6.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT))
bSizer2.Add(self.m_staticText6, 0, wx.ALL, 5)
self.m_textCtrl2 = wx.TextCtrl(self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0)
bSizer2.Add(self.m_textCtrl2, 0, wx.EXPAND, 5)
self.m_button18 = wx.Button(self, wx.ID_ANY, u"Extract", wx.DefaultPosition, wx.DefaultSize, wx.NO_BORDER)
self.m_button18.SetFont(wx.Font(12, 74, 90, 92, False, "微软雅黑"))
self.m_button18.SetForegroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNTEXT))
self.m_button18.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNHIGHLIGHT))
self.m_button18.Bind(wx.EVT_BUTTON, self.extract_pages)
bSizer2.Add(self.m_button18, 0, wx.ALIGN_CENTER_HORIZONTAL | wx.SHAPED, 5)
self.SetSizer(bSizer2)
self.Layout()
self.Centre(wx.BOTH)
def __del__(self):
pass
def extract_pages(self, event):
file_path = self.m_filePicker2.GetPath()
start_page = int(self.m_textCtrl1.GetValue())
end_page = int(self.m_textCtrl2.GetValue())
doc = fitz.open(file_path)
output_doc = fitz.open()
for page_num in range(start_page - 1, end_page):
output_doc.insert_pdf(doc, from_page=page_num, to_page=page_num)
output_path = file_path.replace(".pdf", "_extracted.pdf")
output_doc.save(output_path)
output_doc.close()
doc.close()
wx.MessageBox("Extraction complete!", "Success", wx.OK | wx.ICON_INFORMATION)
# app = wx.App()
# PDFExtractor(None, title="PDF Extractor")
# app.MainLoop()
if __name__ == '__main__':
app = wx.App() # 运行wx.App()方法。认为窗体是一个独立运行的app,所以要定义一个app的程序类来让窗体执行,调用wx类库对应的App方法来生成应用程序的类对象:wx.App()
frame = PDFExtractor(None) # 调用Frame类,并且不指定父类,当前就成为父类
frame.Show() # 运行展示界面的方法Show()
app.MainLoop() # 进入程序wx.App()循环
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.