a

Thursday 9 March 2017

Converts all .html files in the folder to .txt files

 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
88
89
90
91
92
93
94
from os import chdir, getcwd, listdir, path
from time import strftime
import os
import codecs
import re
from bs4 import BeautifulSoup



def count_files(filetype):
    
    count_files = 0
    for files in listdir(folder):
        if files.endswith(filetype):
            count_files += 1
    return count_files


def check_path(prompt):
    ''' (str) -> str
    Verifies if the provided absolute path does exist.
    '''
    abs_path = raw_input(prompt)
    while path.exists(abs_path) != True:
        print "\nThe specified path does not exist.\n"
        abs_path = raw_input(prompt)
    return abs_path    
    
print "\n"

folder = check_path("Provide absolute path for the folder: ")



chdir(folder)



num_html = count_files(".html")

if num_html  == 0:
    print "\nThe specified folder does not contain Html files.\n"
    print strftime("%H:%M:%S"), "There are no files to convert!."
    exit()
else:
    print "\nNumber of Html files: ", num_html, "\n"
    print strftime("%H:%M:%S"), "Starting to convert files ...\n"


list=[]
directory=folder
for root,dirs,files in os.walk(directory):
    for filename in files:
        if filename.endswith('.html'):
            found=os.path.join(directory,filename)
            list.append(found)

m=len(list)
for item in list:
    path=item
    head,tail=os.path.split(path)
    var="\\"
    
    tail=tail.replace(".html",".txt")
    name=head+var+tail
    print item
    html=codecs.open(item,'r')
    soup = BeautifulSoup(html)

    
    for script in soup(["script", "style"]):
        script.extract()    
    
    text = soup.get_text()
    lines = (line.strip() for line in text.splitlines())
    chunks = (phrase.strip() for line in lines for phrase in line.split("  "))
    extractedText = '\n'.join(chunk for chunk in chunks if len(chunk)>6)
    
    content=extractedText
    print strftime("%H:%M:%S"), " Html  -> txt "
    f=open(name,'w')
    f.write(content.encode("UTF-8"))
    f.close

print "\n", strftime("%H:%M:%S"), "Finished converting .html files."




# Count the number of txt files.

num_txt = count_files(".txt")   

print "\nNumber of txt files: ", num_txt

Sunday 19 February 2017

Code for simple Animation loop in python tkinter

 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
import time

from Tkinter import*

screen=Tk()
drawingboard=Canvas(screen,width =400,height=400,bg='white' )
drawingboard.pack()
ball1=drawingboard.create_oval(10,20,30,40,fill='red')
ball2=drawingboard.create_oval(10,20,30,40,fill='blue')
ball3=drawingboard.create_oval(10,20,30,40,fill='green')

x=5
y=3
a=2
b=-5
p=2
q=-7

while True:
    
    drawingboard.move(ball1,x,y)
    drawingboard.move(ball2,a,b)
    drawingboard.move(ball3,p,q)
    destiny=drawingboard.coords(ball3)
    if destiny[1]>=400:
        q=-q
    if destiny[1]<=0:
        q=-q
    if destiny[2]>=400:
        p=-p
    if destiny[2]<=0:
        p=-p
    
    place=drawingboard.coords(ball2)
    if place[1]>=400:
        b=-b
    if place[1]<=0:
        b=-b
    if place[2]>=400:
        a=-a
    if place[2]<=0:
        a=-a
   
    
    position=drawingboard.coords(ball1)
    if position[2]>=400:
        x=-x
        
        
    if position[2]<=0:
        x=-x
    if position[1]>=400:
        y=-y
    if position[1]<=0:
        y=-y

   
    
    screen.update()
    time.sleep(0.02)



    
    
screen.mainloop()


Friday 22 July 2016

Python code to convert .docx , .doc and .pdf files to .txt files

  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
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
from os import chdir, getcwd, listdir, path

from time import strftime

from win32com import client

import os

import pyPdf



def count_files(filetype):

   

    count_files = 0

    for files in listdir(folder):

        if files.endswith(filetype):

            count_files += 1

    return count_files



def check_path(prompt):

    ''' (str) -> str

    Verifies if the provided absolute path does exist.

    '''

    abs_path = raw_input(prompt)

    while path.exists(abs_path) != True:

        print "\nThe specified path does not exist.\n"

        abs_path = raw_input(prompt)

    return abs_path   

   

print "\n"


folder = check_path("Provide absolute path for the folder: ")




chdir(folder)




num_docx = count_files(".docx")

num_doc = count_files(".doc")

num_pdf=count_files(".pdf")




if num_docx + num_doc +num_pdf  == 0:

    print "\nThe specified folder does not contain docx,doc or pdf files.\n"

    print strftime("%H:%M:%S"), "There are no files to convert. BYE, BYE!."

    exit()

else:

    print "\nNumber of doc,docx and pdf files: ", num_docx + num_doc +num_pdf, "\n"

    print strftime("%H:%M:%S"), "Starting to convert files ...\n"



try:

    word = client.DispatchEx("Word.Application")

    for files in listdir(getcwd()):

        if files.endswith(".docx"):

            new_name = files.replace(".docx", r".txt")

            in_file = path.abspath(folder + "\\" + files)

            new_file = path.abspath(folder + "\\" + new_name)

            doc = word.Documents.Open(in_file)

            print strftime("%H:%M:%S"), " docx -> txt ", path.relpath(new_file)

           

            doc.SaveAs(new_file, FileFormat = 2)

            doc.Close()

        if files.endswith(".doc"):

            new_name = files.replace(".doc", r".txt")

            in_file = path.abspath(folder + "\\" + files)

            new_file = path.abspath(folder + "\\" + new_name)

            doc = word.Documents.Open(in_file)

            print strftime("%H:%M:%S"), " doc  -> txt ", path.relpath(new_file)

            doc.SaveAs(new_file, FileFormat = 2)

            doc.Close()





       



           

except Exception, e:

    print e

finally:

    word.Quit()


list=[]

directory=folder

for root,dirs,files in os.walk(directory):

    for filename in files:

        if filename.endswith('.pdf'):

            t=os.path.join(directory,filename)

            list.append(t)


m=len(list)

i=0

while i<=len(list):

    path=list[i]

    head,tail=os.path.split(path)

    var="\\"

   

    tail=tail.replace(".pdf",".txt")

    name=head+var+tail

   

   

    

    content = ""

    # Load PDF into pyPDF

    pdf = pyPdf.PdfFileReader(file(path, "rb"))

    # Iterate pages

    for i in range(0, pdf.getNumPages()):

        # Extract text from page and add to content

        content += pdf.getPage(i).extractText() + "\n"

    print strftime("%H:%M:%S"), " pdf  -> txt "

    f=open(name,'w')

    f.write(content.encode("UTF-8"))

    f.close


print "\n", strftime("%H:%M:%S"), "Finished converting .doc .docx and .pdf files."





# Count the number of txt files.


num_txt = count_files(".txt")  


print "\nNumber of txt files: ", num_txt

Thursday 7 July 2016

Python code to convert all pdf files to .txt files in a folder

 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
88
89
90
91
92
93
94
95
 import os

from os import chdir, getcwd, listdir, path

import pyPdf

from time import strftime


def check_path(prompt):

    ''' (str) -> str

    Verifies if the provided absolute path does exist.

    '''

    abs_path = raw_input(prompt)

    while path.exists(abs_path) != True:

        print "\nThe specified path does not exist.\n"

        abs_path = raw_input(prompt)

    return abs_path   

   

print "\n"


folder = check_path("Provide absolute path for the folder: ")


list=[]

directory=folder

for root,dirs,files in os.walk(directory):

    for filename in files:

        if filename.endswith('.pdf'):

            t=os.path.join(directory,filename)

            list.append(t)


m=len(list)

i=0

while i<=len(list):

    path=list[i]

    head,tail=os.path.split(path)

    var="\\"

   

    tail=tail.replace(".pdf",".txt")

    name=head+var+tail

   

   

    

    content = ""

    # Load PDF into pyPDF

    pdf = pyPdf.PdfFileReader(file(path, "rb"))

    # Iterate pages

    for i in range(0, pdf.getNumPages()):

        # Extract text from page and add to content

        content += pdf.getPage(i).extractText() + "\n"

    print strftime("%H:%M:%S"), " pdf  -> txt "

    f=open(name,'w')

    f.write(content.encode("UTF-8"))

    f.close



Simple animation loop in python


Click here to download the python file



Build A Salesforce To Sell For Your Business With Monkey Business


  For Python version 3 users follow the code given below.
  for the code to work you have to install pypdf2
  by using  ---------->        pip install pypdf2
   
 For converting html files to text files  Click here

 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
88
89
90
91
92
import os

from os import chdir, getcwd, listdir, path

import PyPDF2

from time import strftime


def check_path(prompt):

    ''' (str) -> str

    Verifies if the provided absolute path does exist.

    '''

    abs_path = input(prompt)

    while path.exists(abs_path) != True:

        print ("\nThe specified path does not exist.\n")

        abs_path = input(prompt)

    return abs_path   

   

print ("\n")


folder = check_path("Provide absolute path for the folder: ")


list=[]

directory=folder

for root,dirs,files in os.walk(directory):

    for filename in files:

        if filename.endswith('.pdf'):

            t=os.path.join(directory,filename)

            list.append(t)




for item in list:
    path=item

    head,tail=os.path.split(path)

    var="\\"

   

    tail=tail.replace(".pdf",".txt")

    name=head+var+tail

    

   

   

    

    content = ""

    

    pdf = PyPDF2.PdfFileReader(path, "rb")

    

    for i in range(0, pdf.getNumPages()):

        

        content += pdf.getPage(i).extractText() + "\n"
        

    print (strftime("%H:%M:%S"), " pdf  -> txt ")

    with open(name,'a') as out:
        out.write(content 

Friday 17 June 2016





I am here to help you all. This site will provide you with free Python codes. All the codes are written

in Python 2.7.11 version .

1. Program to obtain numbers from user and store it in a list. 




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
a=''

list=[]

while a!='done':

    a=raw_input('Enter a number:  ')

    if a!='done':

        list.append(a)

print'List =',list
output:  
Enter a number:  2
Enter a number:  5
Enter a number:  6
Enter a number:  4
Enter a number:  done
List = ['2', '5', '6', '4']

2. Program to open a Text file and print data as output.
    (Hint: if text file is not present at Python folder input the path along with file name)

1
2
3
4
5
var1=raw_input('Enter the file name:  ')

f=open(var1,'r')

print f.read()




output:
Enter the file name:

3.


 

1.Program to get all the text from a site and store it in a .txt file
  
  (Requires  BeautifulSoup )

  



 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
import urllib

import re

from bs4 import BeautifulSoup

var1=raw_input('Enter url:   ')


url =var1

html = urllib.urlopen(url).read()

soup = BeautifulSoup(html)



for script in soup(["script", "style"]):

    script.extract()   




text = soup.get_text()

lines = (line.strip() for line in text.splitlines())

chunks = (phrase.strip() for line in lines for phrase in line.split("  "))

text = '\n'.join(chunk for chunk in chunks if chunk)

text = text.encode('ascii', 'ignore').decode('ascii')


print('Text copied to Data.txt file')

fhand=open('Data.txt','w')

fhand.write(text)

fhand.close()
Regex code to find all email address :

1
re.findall(r"(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)",string_name)


Program to find a particular file type (eg. .mp3,.doc,.mp4 etc) in the PC along with file path

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import fnmatch
import os
rootdir='/'
var1=raw_input('Enter file type to search  prefix  * :  ')
pattern=var1
for root,subdirname,filelist in os.walk(rootdir):
        for filename in fnmatch.filter(filelist,pattern):
                t=(os.path.join(root,filename))
                print(t)
print('ALL FILES FOUND')



 


 

converting doc,docx files to txt files