Skip to content

Commit 861edd7

Browse files
committed
FilesSplitter.
0 parents  commit 861edd7

File tree

1 file changed

+199
-0
lines changed

1 file changed

+199
-0
lines changed

FilesSplitter.py

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
# import project libraries.
2+
import wx
3+
import webbrowser
4+
5+
6+
# Create app with wx.
7+
app= wx.App()
8+
9+
# Create Files splitter window with wx.
10+
class FilesSplitter(wx.Frame):
11+
def __init__(self):
12+
wx.Frame.__init__(self, None, title = 'FilesSplitter', size=(300, 230))
13+
14+
#make window in center.
15+
self.Center()
16+
17+
#make window Minimum size.
18+
self.Maximize(False)
19+
self.EnableMaximizeButton(False)
20+
self.FileContent = ""
21+
self.encoding = ""
22+
23+
24+
# Creating panel
25+
self.Panel = wx.Panel(self)
26+
27+
# Creating Buttons
28+
self.ImportFiles = wx.Button(self.Panel, -1, "Import a file", pos=(30,30), size=(100,30))
29+
wx.StaticText(self.Panel, -1, "Select the number of characters in each file:", pos=(20,70), size=(380, 30))
30+
self.PartsNumber = wx.SpinCtrl(self.Panel, -1, "2500", min=100, max=10000000000, style=wx.SP_ARROW_KEYS, pos=(40, 100), size=(50, 20))
31+
self.SaveFiles = wx.Button(self.Panel, -1, "Save files", pos=(20,140), size=(70,30))
32+
self.more = wx.Button(self.Panel, -1, "more", pos=(10,140), size=(120,30))
33+
self.Close = wx.Button(self.Panel, -1, "Close", pos=(100,140), size=(60,30))
34+
35+
# Show Main window
36+
self.Show()
37+
38+
# events.
39+
self.ImportFiles.Bind(wx.EVT_BUTTON, self.OnOnOpwnFile)
40+
self.Close.Bind(wx.EVT_BUTTON, self.OnCloseProgram)
41+
self.SaveFiles.Bind(wx.EVT_BUTTON, self.OnSaveFiles)
42+
self.more.Bind(wx.EVT_BUTTON, self.OnMore)
43+
44+
45+
def OnMore(self, event):
46+
MoreMenu = wx.Menu()
47+
ContactMenu = wx.Menu()
48+
QaisMenu = wx.Menu()
49+
QaisEm = QaisMenu.Append(-1, "&E-mail")
50+
QaisTe =QaisMenu.Append(-1, "&Telegram")
51+
QaisWh =QaisMenu.Append(-1, "&Whats App")
52+
QaisTw =QaisMenu.Append(-1, "&Twitter")
53+
QaisFa =QaisMenu.Append(-1, "&Facebook")
54+
ContactMenu.AppendSubMenu(QaisMenu, "&Qais Alrefai")
55+
MahmoodMenu = wx.Menu()
56+
MahmoodEm =MahmoodMenu.Append(-1, "&E-mail")
57+
MahmoodTe =MahmoodMenu.Append(-1, "&Telegram")
58+
MahmoodWh =MahmoodMenu.Append(-1, "&Whats App")
59+
MahmoodTw =MahmoodMenu.Append(-1, "&Twitter")
60+
MahmoodFa =MahmoodMenu.Append(-1, "&Facebook")
61+
ContactMenu.AppendSubMenu(MahmoodMenu, "&Mahmood atef")
62+
MesterPerfectMenu = wx.Menu()
63+
MesterPerfectEm =MesterPerfectMenu.Append(-1, "&E-mail")
64+
MesterPerfectTe =MesterPerfectMenu.Append(-1, "&Telegram")
65+
MesterPerfectWh =MesterPerfectMenu.Append(-1, "&Whats App")
66+
MesterPerfectTw =MesterPerfectMenu.Append(-1, "&Twitter")
67+
MesterPerfectFa =MesterPerfectMenu.Append(-1, "&Facebook")
68+
ContactMenu.AppendSubMenu(MesterPerfectMenu, "&Ahmed Bakr")
69+
TecWindow=ContactMenu.Append(-1, "TecWindow on Telegram")
70+
MoreMenu.AppendSubMenu(ContactMenu, "&Contact us")
71+
self.AboutItem = MoreMenu.Append(-1, "About")
72+
self.CloseProgramItem = MoreMenu.Append(-1, "Close program")
73+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open("mailto:ww258148@gmail.com"), QaisEm)
74+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://t.me/QaisAlrefai"), QaisTe)
75+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://twitter.com/qais_Alrefai"), QaisTw)
76+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://wa.me/962792540394"), QaisWh)
77+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://www.facebook.com/Qais1461"), QaisFa)
78+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open("mailto:mahmoud.atef.987123@gmail.com"), MahmoodEm)
79+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://t.me/MahmoodAtef"), MahmoodTe)
80+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://twitter.com/mahmoud_atef999"), MahmoodTw)
81+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://wa.me/201224660664"), MahmoodWh)
82+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://www.facebook.com/mahmoud.atef.000"), MahmoodFa)
83+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open("mailto:AhmedBakr593@gmail.com"), MesterPerfectEm)
84+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://t.me/MesterPerfect"), MesterPerfectTe)
85+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://twitter.com/my_nvda"), MesterPerfectTw)
86+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://wa.me/201554240991"), MesterPerfectWh)
87+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://www.facebook.com/my.nvda.1"), MesterPerfectFa)
88+
self.Bind(wx.EVT_MENU, lambda event: webbrowser.open_new("https://t.me/TecWindowProjects"), TecWindow)
89+
self.Bind(wx.EVT_MENU, self.OnAboutProgram, self.AboutItem)
90+
self.Bind(wx.EVT_MENU, self.OnCloseProgram, self.CloseProgramItem)
91+
self.PopupMenu(MoreMenu)
92+
93+
94+
#creating OnAboutProgram function to show information about this program.
95+
def OnAboutProgram(self, event):
96+
AboutDialog = wx.MessageDialog(self, """ Files splitter.
97+
Version: 1.0.
98+
This simple tool allows you to split large text files into small files.
99+
This tool was developed by:
100+
Ahmed Bakr.
101+
Qais Alrifai.
102+
Mahmoud Atef.""", "About the program", style=wx.ICON_INFORMATION+wx.OK)
103+
AboutDialog.SetOKLabel("&Ok")
104+
AboutDialog.ShowModal()
105+
106+
107+
108+
def OnOnOpwnFile(self, ev):
109+
if not self.FileContent == "":
110+
ConfirmReplace = wx.MessageDialog(self,"""You have already imported a file.
111+
If you import another file, the new file will replace the old one.
112+
Do you want to proceed?""", "Confirm", style=wx.YES_NO+wx.YES_DEFAULT+wx.ICON_WARNING+wx.ICON_QUESTION)
113+
ConfirmReplace.SetYesNoLabels("&Yes", "&No")
114+
if ConfirmReplace.ShowModal() == wx.ID_YES:
115+
OpenFile = wx.FileDialog(self, "Choose a file", "self.FilePath")
116+
OpenFile.Wildcard = "Text files (.txt)|*.txt"
117+
OpenFileResult = OpenFile.ShowModal()
118+
if OpenFileResult == wx.ID_OK:
119+
FilePath = OpenFile.Path
120+
FileName = OpenFile.Filename
121+
file = open(FilePath, "r")
122+
self.FileContent= file.read()
123+
file.close()
124+
else:
125+
return
126+
else:
127+
OpenFile = wx.FileDialog(self, "Choose a file", "self.FilePath")
128+
OpenFile.Wildcard = "Text files (.txt)|*.txt"
129+
OpenFileResult = OpenFile.ShowModal()
130+
if OpenFileResult == wx.ID_OK:
131+
FilePath = OpenFile.Path
132+
FileName = OpenFile.Filename
133+
file = open(FilePath, "r")
134+
self.FileContent= file.read()
135+
self.encoding = file.encoding
136+
file.close()
137+
else:
138+
return
139+
140+
def OnSaveFiles(self, ev):
141+
if self.FileContent == "":
142+
wx.MessageBox("You need to import a file first.", "Error", style=wx.ICON_ERROR)
143+
return
144+
SaveFile = wx.FileDialog(self, "Save ", "self.FilePath", style=wx.FD_SAVE+wx.FD_OVERWRITE_PROMPT)
145+
SaveFile.Wildcard = "Text files (.txt)|*.txt"
146+
SaveFileResult = SaveFile.ShowModal()
147+
if SaveFileResult == wx.ID_OK:
148+
FilePath = SaveFile.Path
149+
FileName = SaveFile.Filename
150+
151+
152+
parts = self.split_text(self.FileContent, self.PartsNumber.Value)
153+
154+
for number, part in enumerate(parts):
155+
NewPath = FilePath[:-4] + str(number+1) + ".txt"
156+
with open(NewPath, "w", encoding=self.encoding) as f:
157+
f.write(part)
158+
159+
def split_text(self, text: str, maxcharsnum:int = 10000):
160+
if len(text) <= maxcharsnum:
161+
return [text]
162+
rettxt = ""
163+
retlist = []
164+
for line in text.split("\n"):
165+
if len(f"{rettxt}\n{line}") <= maxcharsnum:
166+
if rettxt == "":
167+
rettxt = line
168+
else:
169+
rettxt += f"\n{line}"
170+
continue
171+
172+
crtline = ""
173+
for word in line.split():
174+
txt_length = len(f"{rettxt}\n{crtline + ' ' + word}")
175+
if txt_length <= maxcharsnum:
176+
if crtline == "":
177+
crtline = word
178+
else:
179+
crtline += " " + word
180+
elif txt_length > maxcharsnum:
181+
retlist.append(f"{rettxt}\n{crtline}")
182+
rettxt = ""
183+
crtline = word
184+
185+
if rettxt == "":
186+
rettxt = crtline
187+
else:
188+
rettxt += f"\n{crtline}"
189+
crtline = ""
190+
191+
else:
192+
retlist.append(rettxt)
193+
return retlist
194+
195+
def OnCloseProgram(self, event):
196+
wx.Exit()
197+
198+
FilesSplitter()
199+
app.MainLoop()

0 commit comments

Comments
 (0)