Boolean Indexing
Pages
- Index of Lessons in Technology
- Index of Book Summaries
- Index of Book Lists And Downloads
- Index For Job Interviews Preparation
- Index of "Algorithms: Design and Analysis"
- Python Course (Index)
- Data Analytics Course (Index)
- Index of Machine Learning
- Postings Index
- Index of BITS WILP Exam Papers and Content
- Lessons in Investing
- Index of Math Lessons
- Downloads
- Index of Management Lessons
- Book Requests
- Index of English Lessons
- Index of Medicines
- Index of Quizzes (Educational)
Tuesday, September 7, 2021
Factorial, NumPy and DocTest
Script: s1.py
import numpy as np
import doctest
def factorial(n):
"""
Test for the factorial of 3 that should pass.
>>> factorial(3)
6
Test for the factorial of 0 that should fail.
>>> factorial(0)
1
"""
return np.arange(1, n+1).cumprod()[-1]
doctest.testmod()
OUTPUT:
(base) CMD>python s1.py
**********************************************************************
File "s1.py", line 11, in __main__.factorial
Failed example:
factorial(0)
Exception raised:
Traceback (most recent call last):
File "E:\programfiles\Anaconda3\lib\doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest __main__.factorial[1]>", line 1, in <module>
factorial(0)
File "s1.py", line 14, in factorial
return np.arange(1, n+1).cumprod()[-1]
IndexError: index -1 is out of bounds for axis 0 with size 0
**********************************************************************
1 items had failures:
1 of 2 in __main__.factorial
***Test Failed*** 1 failures.
(base) CMD>python s1.py -v
Trying:
factorial(3)
Expecting:
6
ok
Trying:
factorial(0)
Expecting:
1
**********************************************************************
File "s1.py", line 11, in __main__.factorial
Failed example:
factorial(0)
Exception raised:
Traceback (most recent call last):
File "E:\programfiles\Anaconda3\lib\doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest __main__.factorial[1]>", line 1, in <module>
factorial(0)
File "s1.py", line 14, in factorial
return np.arange(1, n+1).cumprod()[-1]
IndexError: index -1 is out of bounds for axis 0 with size 0
1 items had no tests:
__main__
**********************************************************************
1 items had failures:
1 of 2 in __main__.factorial
2 tests in 2 items.
1 passed and 1 failed.
***Test Failed*** 1 failures.
Code with two functions
import numpy as np
import doctest
def factorial(n):
"""
Test for the factorial of 3 that should pass.
>>> factorial(3)
6
Test for the factorial of 0 that should fail.
>>> factorial(0)
1
"""
return np.arange(1, n+1).cumprod()[-1]
def isEven(n):
"""
Test that would pass
>>> isEven(10)
True
Test that would fail
>>> isEven(9)
True
Test that would pass
>>> isEven(9)
False
"""
rtn = n % 2
return rtn == 0
doctest.testmod()
Output
(base) CMD>python script.py
**********************************************************************
File "script.py", line 11, in __main__.factorial
Failed example:
factorial(0)
Exception raised:
Traceback (most recent call last):
File "E:\programfiles\Anaconda3\lib\doctest.py", line 1329, in __run
compileflags, 1), test.globs)
File "<doctest __main__.factorial[1]>", line 1, in <module>
factorial(0)
File "script.py", line 14, in factorial
return np.arange(1, n+1).cumprod()[-1]
IndexError: index -1 is out of bounds for axis 0 with size 0
**********************************************************************
File "script.py", line 24, in __main__.isEven
Failed example:
isEven(9)
Expected:
True
Got:
False
**********************************************************************
2 items had failures:
1 of 2 in __main__.factorial
1 of 3 in __main__.isEven
***Test Failed*** 2 failures.
Tags: Technology,Python,Machine Learning,NumPy
Factorial, NumPy and UnitTest
Writing unit tests
Test-driven development (TDD) is the best thing that has happened to software development this century. One of the most important aspects of TDD is the almost manic focus on unit testing.
The TDD methodology uses the so-called test-first approach, where we first write a test that fails and then write the corresponding code to pass the test. The tests should document the developer's intent, but on a lower level than functional design. A suite of tests increases confidence by decreasing the probability of regression and facilitates refactoring.
Unit tests are automated tests that test a small piece of code, usually a function or method. Python has the PyUnit API for unit testing. As NumPy users, we can make use of the convenience functions in the numpy.testing module as well. This module, as its name suggests, is dedicated to testing.
Script.py:
import numpy as np
import unittest
def factorial(n):
if n == 0:
return 1
if n < 0:
raise ValueError("Don't be so negative")
return np.arange(1, n+1).cumprod()
class FactorialTest(unittest.TestCase):
def test_factorial(self):
#Test for the factorial of 3 that should pass.
self.assertEqual(6, factorial(3)[-1])
np.testing.assert_equal(np.array([1, 2, 6]), factorial(3))
def test_zero(self):
#Test for the factorial of 0 that should pass.
self.assertEqual(1, factorial(0))
def test_negative(self):
# Test for the factorial of negative numbers that should fail.
# It should throw a ValueError, but we expect IndexError
self.assertRaises(IndexError, factorial(-10))
if __name__ == '__main__':
unittest.main()
OUTPUT:
(base) CMD>python script.py
.E.
======================================================================
ERROR: test_negative (__main__.FactorialTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "script.py", line 24, in test_negative
self.assertRaises(IndexError, factorial(-10))
File "script.py", line 8, in factorial
raise ValueError("Don't be so negative")
ValueError: Don't be so negative
----------------------------------------------------------------------
Ran 3 tests in 0.078s
FAILED (errors=1)
(base) CMD>
(base) CMD>
Testing functions available to us:
Tags: Technology,Python,Machine Learning,
2011-Feb-11 (Death of Ms Bhati)
Index of Journals
'Ignorance isn't bliss forever' When one gets along an irritating person, thing, or habit, it may sound intelligent to ignore them all. But, it doesn't work that way for our psych, our mental being. Attention seeking irritating people can keep one's brain highly occupied in inhibiting from producing thoughts. So it is supposed to be fought, not escaped. Look onto them, recognize them, people like them, their behavior, actions. Once you know what they do and what they are, it should not be difficult to device a reaction for their actions. Plus, there will be nothing you need to know, or don’t need to know, because you already know it all. February 11, 2011 That old woman, Ms. Bhati died last night. She used to look well in her health; she used to walk around in the society, had friends with most extravagant house-wives, and used to look like in the early years of old age. News came home via chachi; she had seen her last night around ten when she was walking in the society. And around 0030 last night, she just slipped after the attack of some ailment. I was thinking about her death, and thinking of amma growing old each day. Huh! It was the second day of the sports meet. I sat in the library to study Communication Systems. I have no place on earth where I can be alone. I mean, being alone is not what I want but I want no disturbance. That’s impossible at home, chachi and Prashant both are d**kheads, I can’t even expect them to act like normal, let alone intelligent. I was home around seven. It feels good to travel at night, crowded bus passing by traffic of Delhi roads. God Bless ‘Me’ Ashish
2011-Feb-10
Index of Journals
February 10, 2011 I didn’t wake up to study early after having wasted time in sleeping, watching TV, and watching movie on laptop last night. Chachi and kids had gone to a wedding, then. I reached college at around eleven. And I sat there till four-thirty in the evening. I studied ‘Communication Systems’ it required third semester math which I had missed, so I had to do and still have to do that missing part of math course from third semester. Result is still a month away but the way chachi ask for it is disturbing, as if she owns me. God Bless ‘Me’ Ashish
2011-Feb-9 (Money lent to Hemanshu Verma)
Index of Journals
February 9, 2011 I wasn’t about to miss today’s ADA (Algorithms Analysis and Design) class after being made the class coordinator yesterday. I reached college on time well around eight. It wasn’t difficult to study Prashant’s sir subject after having spent hours in library to do the same. Well, getting to class was worth the money I spent on reaching college on time. I spent R29 to get there. Otherwise, I travel free for weeks! It was a fine day at college, I came back home early at four. Oops, I had not thought that I would find chachaji on the door. I thought he left but when I reached home, he was about to leave. Huh! I also have to count the R50 I gave up to Hemanshu Verma of S2. I didn’t mind lending him that money; otherwise, I am not even a spender. I was doing second semester Math, now I should better catch up with this, the 4th semester subjects. First terminal tests are nearing. First is on 27th of February. God Bless ‘Me’ Ashish
2011-Feb-7 (Neeru Ma'am)
Index of Journals
February 7, 2011 I woke up in a rush at 0802, something. I went to amma’s room to get ready and leave with babaji. As we waited for Manju buaji on the bus stop, driver told babaji to clean the wound which was bleeding on his forehead. Actually, babaji bruised his forehead near the brow by the edge of the door. It felt extremely awful to see yet another loss in by some means. How can I be so thoughtful about such things at this age? I couldn’t attend even the nine-am class for coming late. Then we were just made to sit in the DCS (Digital Circuits and Systems) lab. Ms. Neeru ma’am was not in the mood to teach, you see! I was left alone. The class is so poor! I was reading Abhilash’s notebook and when I tried to ask him and Mukul what they were talking about I was looked down by the Mukul Chandra, wow, wow, wow, is that reality I am living in? I was in the badminton court eating my lunch and I didn’t raise my head up to see but I knew I was being watched by people hanging on the bridges on the top floors on my left. Well, the game was going on the right, so that kept me at ease. After sitting alone whole day, I was met by Neha in the last class. She came by to ask about that post. And after a hearty talk, she just said if next time to any such thing, I should be excluding her name too. It was a favor, she said, she was asking for. And, I learnt what this bitch was up about, well, that doesn’t hurt anymore. I have a clear pass to Apurva Sood, but my pocket is not filled enough for nobody. Neeru ma’am is after me. She was acting totally slutty whenever I saw her. The way she eyes me, and I was literally scared for a second when I was bent down to search for a page in my bag and she came down the row to stand next to me. She had run her finger on my table like trying to seduce. This is going beyond limits. I am totally infamous at college, just one case and I am dead. You know, it felt proud when in the break Apurv referred to me as ‘Tank of Our Class’. God Bless ‘Me’ Ashish
2011-Feb-6
Index of Journals
February 6, 2011 I went to buy books today. And chachaji came back today, for mere two days to my gladness. We saw each other before I left for Daryaganj. I collected books from a single shopkeeper. I mean, I didn’t get to go to another. He suited me. Though, he wasn’t very profitable for me. After that, I went to badi buaji’s house. I had sort of breakfast around twelve, I told badi buaji to keep it light but it went on to becoming heavy, though I had never expressed myself in words. Huh! Then, it was normal life at her place. I sat on computer with Ankur in the office, and we were upstairs for lunch. After lunch, Shruti and I had kind of a serious talk about her preparation for board exams and life after that. I mean, she is troubled by her almost nil preparations for three subjects. And, in those subjects, she failed to clear the pre-board exam. It is Chemistry, Physics, and Math. It is funny how a science student can fail in PCM. I was just sitting downstairs whole time long, taking x-rated movies from Ankur. I was home with Ankur in his car because badi buaji sent him to buy some bakery items from some far away shop and in the process, he just came by to Manu Apartments with me. Chachaji saw the books, and not much inspection of the prices and other details, just a casual view of everything. That was easy to handle. God Bless ‘Me’ Ashish
2011-Feb-5 (God is not omniscient)
Index of Journals
February 5, 2011 Last night I was crying in bed for having such a f***ing sick life. Today while sitting in college I almost structured a new religion, Modern-Day-Jainism, or more clearly Jainism-post-2011. It states that God has no known face on name. And god is neither omniscient, nor a retard, unlike what all other religions try to prove. Before writing this, I literally went to dropping two heavy drops of tears on the page on the book I was reading. It was more noticeable how I got back to normal in no time. Crying was because of the last night again. Life never felt so sick before. I have been missing sleeping hours and have been napping in evenings and afternoons so I generally feel my head shaking which remind me of earthquakes. I mean I have faced quaking tremors one night I always get the same feeling every time my spins out of natural weakness. In the library after having shed tears, it went quite around me. These days, I can hear my heart beating; I can feel my blood pumped into my skull, that’s it is so heavy because of all kinds of troubles from life. I was home in the evening, and I was asleep. God Bless ‘Me’ Ashish
2011-Feb-4 (Disappointed in Religion)
Index of Journals
4 February 2011 Babaji said Manju buaji was angry with him yesterday morning. I get to hear this kind of bullshit when I am there in amma’s room for bathing, or changing purposes. I was there in the morning. I didn’t pay attention to that, but still it comes to my mind and I have to make special efforts to avoid it. Later when amma called Manju buaji on phone, buaji almost wept as I heard amma saying. I went to the college and it was fine. During the return trip, as I climbed the bus a girl was doing her hair and I just found it funny somehow. I had to first laugh and as she saw me, my pursed lips opened up in smile. She was cute: I have to say that. And I didn’t mean to scare her. But she and her friend checked me almost a dozen times after that, it was awful in its own. Her friend changed the bus on Red Fort and the girl changed her seat from second to mine in the next row to the most distant one. The first one in the same row, the one that also falls in the seats reserved for women. Huh! I was asleep and there was this drama from Prashant, of leaving the lights and door opened. I couldn’t sleep after ten, that’s when he begins his activities. I was reckoning my options of living life here. One is going to Trinagar, second is managing time-table to adjust the disturbances which this idiot creates, and continuing a life here. It sucks when I have to tell myself that Ashish cut down your hopes for reaching high because your run isn’t on the same track as that of a professional. I can’t even trust babaji when it comes to the most important decisions of my life, because on almost every such point of my life he has always disappointed me, not once or twice. I am not lying nor do I have words to describe how it feels. Just tell me, when does a grown man cry? I am no more going to follow the practice of reading Bhaktamar and counting beads on fingers. I just did it twice today because I missed doing it yesterday due to busy schedule. I don’t believe in blindly following any religion whatever it might be. I don’t believe in ‘God’ as the people describe them. I do believe in teachers but not gods as the people describe Him. And I don’t even understand Sanskrit; I don’t understand pure Hindi properly, let alone Sanskrit. English is my mother tongue now! -Ashish
2011-Feb-3
Index of Journals
February 3, 2011 I posted in C.S1.E on Facebook at 2 a.m. ” Except Karishma, Astha, Shreya, Sonam, and Arushi Jain, all other females are total rude hags. Listen bitches please take no offence. I was just putting light on your reality.” It was deleted when I checked my account in the morning (1330). I was in library as I switched on my phone I received Vibha’s messages sent an hour before. I studied ADA till four-thirty and then I came back home. I watched TV from 1830 to 1930, Hip-Hop Hustle, and Ticket to V.I.P. I was asleep after that, and I got two missed calls from Vibha. Vibha had called me ‘THE BLOODY F***ER.’ And was she threatening me of breaking up with me earlier, as if I ever felt for her! I had never replied to her since my message card got over, funny. Teachers in college were watching my moves, they had planned to even check it when I saw that naughty-in-forty (maidservant) walking upstairs to the classrooms. I quickly turned and headed to the library to avoid any confusion. These college keepers were everywhere, ignoring or noticing me, god knows. I met principal today. I showed her the notice and she carefully went through it taking her full time. Then she comments in the language used in the notice itself. Like I am some fool. She never did anything about it the way she had said last time when we met. God Bless ‘Me’ Ashish
2011-Feb-2
Index of Journals
February 2, 2011 Without tea, I can’t stay awake whole night. It feels so tiring to go to college in half-sleep. Today again, I was thrown out in the first class. It wasn’t my fault completely. It was 10 a.m. class, Operating Systems. That stupid looking teacher (who really is stupid if trust anyone from the class) came and asked some question from the first bencher. We missed the question. And, then Aditya had opened the notebook. She probably saw that and stood him. He said he didn’t know the question, so she passed the same one on to me. I told her the same thing that she wasn’t audible here in the end. So she sent all three of us out, Aditya, Nitish, and me. Huh, because lately I have been feeling alone so I just sat with these two and talked for an hour. Next class of Comm. Graphics was fine, and the practical class of S.E. (Software Engineering). Sometimes you know it feels awesome to be single, the attention one gets from these single-girls most of whom are single because of there attitude. You know it just raises your price. Kanika (fatso), Shreya, Karishma, Tanvi all look at me like I am an eye-candy. God Bless ‘Me’ Ashish
2011-Feb-1
Index of Journals
February 1, 2011 “You should not dive into murky waters” Had I not slept till seven-forty I would have got to attend ADA class. ‘Analyzing and Designing Algorithms’ taught by Mr. Prashant. His class started at nine and I reached college at nine-thirty. I sat alone in the next class, and studied the ADA book which I had. Faizan, Apurv, and Rizwan came there; Faizan had come late while Apurv and Rizwan said they were thrown out of the class for entering late. They had entered the class a few seconds late and sir was rubbing the board when he didn’t let them in. ‘A’ Batch had Computer Graphics lab but the teacher wasn’t coming so we were free, while B-batch went to attend their lab session. I was roaming like lost soul in the break; I am supposed to be like that. I haven’t made any friends yet. Life sucks in those fifteen-twenty minutes, which seem to be like hours. Communication Systems teacher asked for introduction today. Ah, I was literally huffing while speaking and teacher asked why I was looking tired while speaking. I didn’t want to give introduction, it sucks, and my results were oh-so-f***ing-poor. That is which every teacher asks. Swarnlata ma’am came to the class but just let us go after taking the attendance. The class almost seems to hate me, the groups have formed, and I can’t find a place nowhere. Plus, I learned from examination cell that 90% of the first year back-log would have to be cleared. So, I was seen in Principal’s (Yamini) room again. She said she would check it and tell me tomorrow. Now, I’m wondering at what time I should get back to her. God Bless ‘Me’ Ashish
Friday, September 3, 2021
Two Types of Matrix Multiplication Using NumPy
How are two matrices multiplied according to the math rules: Khan Academy (base) C:\Users\ashish>python Python 3.7.1 (default, Dec 10 2018, 22:54:23) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32 Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>> a = [[1, -1], [1, 2]] >>> b = [[3], [4]] >>> a * b Traceback (most recent call last): File "[stdin]", line 1, in [module] TypeError: can't multiply sequence by non-int of type 'list' >>> a = np.array(a) >>> b = np.array(b) >>> a array([[ 1, -1], [ 1, 2]]) >>> b array([[3], [4]]) >>> np.matmul(a,b) array([[-1], [11]]) >>> np.matmul(b, a) Traceback (most recent call last): File "[stdin]", line 1, in [module] ValueError: shapes (2,1) and (2,2) not aligned: 1 (dim 1) != 2 (dim 0) >>> a.dot(b) array([[-1], [11]]) >>> np.dot(a, b) array([[-1], [11]]) >>> a.b Traceback (most recent call last): File "[stdin]", line 1, in [module] AttributeError: 'numpy.ndarray' object has no attribute 'b' >>> np.dot(b, a) Traceback (most recent call last): File "[stdin]", line 1, in [module] ValueError: shapes (2,1) and (2,2) not aligned: 1 (dim 1) != 2 (dim 0) # How about an element to element multiplication? # Note: this is not how math books do it >>> np.multiply(a, b) array([[ 3, -3], [ 4, 8]]) >>> np.multiply(b, a) array([[ 3, -3], [ 4, 8]]) >>> a*b array([[ 3, -3], [ 4, 8]]) >>> a = [[1, 1], [2, 2]] >>> b = [[0, 0], [1, 1]] >>> a = np.array(a) >>> b = np.array(b) >>> a*b array([[0, 0], [2, 2]]) >>> a = [[0, 1], [2, 3]] >>> a = np.array(a) >>> b = [[5, 10], [15, 20]] >>> b = np.array(b) >>> a*b array([[ 0, 10], [30, 60]]) >>> Ref: docs.scipy.org Tags: Technology,Python,NumPy
Using NumPy's 'random' package (randint and shuffle)
randint
>>> np.random.randint(4) 0 >>> np.random.randint(4) 3 >>> np.random.randint(4) 2 >>> np.random.randint(4) 0shuffle
>>>arr = np.arange(10) >>>np.random.shuffle(arr) >>>arr [1 7 5 2 9 4 3 6 0 8] # random - - - >>> import numpy as np >>> arr = [0, 1, 2, 3] >>> np.random.shuffle(arr) >>> arr [1, 0, 2, 3] >>> np.random.shuffle(arr) >>> arr [0, 1, 3, 2] >>> Tags: Technology,Python,NumPy
Dot Product using Python, NumPy, Matplotlib
Dot product / Inner product / Scalar product
Algebraic Definition Geometric Definition Python Code from matplotlib.pyplot import plot point1 = [0, 0] point2 = [3, 0] x_values = [point1[0], point2[0]] y_values = [point1[1], point2[1]] plot(x_values, y_values, 'b-') # format: Blue dashes point1 = [0, 0] point2 = [3, 3] x_values = [point1[0], point2[0]] y_values = [point1[1], point2[1]] plot(x_values, y_values, color='red', marker='o') import numpy as np a = np.array([3, 0]) b = np.array([3, 3]) c = np.array([1, 1]) print("a, b, c:", a, b, c, end = "\n\n") print("a.dot(b):", a.dot(b)) print("b.dot(a):", b.dot(a), end = "\n\n") print("np.dot(a, b):", np.dot(a, b)) print("sum(a*b):", sum(a*b), end = "\n\n") print("np.dot(b, c):", np.dot(b, c)) print("sum(b*c):", sum(b*c), end = "\n\n") theta = np.pi / 4 print("theta = np.pi / 4") print("np.linalg.norm(a) * np.linalg.norm(b) * np.cos(theta):", np.linalg.norm(a) * np.linalg.norm(b) * np.cos(theta)) a, b, c: [3 0] [3 3] [1 1] a.dot(b): 9 b.dot(a): 9 np.dot(a, b): 9 sum(a*b): 9 np.dot(b, c): 6 sum(b*c): 6 theta = np.pi / 4 np.linalg.norm(a) * np.linalg.norm(b) * np.cos(theta): 9.0 Tags: Technology,Python,Data Visualization,Machine Learning,
Google Sites, Tor Exit Nodes and Captcha
Top 10 Websites
Rank - Website - Monthly Visitors - Country of Origin - Category 1 Google.com 92.5B U.S. Search Engines 2 Youtube.com 34.6B U.S. TV Movies and Streaming 3 Facebook.com 25.5B U.S. Social Networks and Online Communities 4 Twitter.com 6.6B U.S. Social Networks and Online Communities 5 Wikipedia.org 6.1B U.S. Dictionaries and Encyclopedias 6 Instagram.com 6.1B U.S. Social Networks and Online Communities 7 Baidu.com 5.6B China Search Engines 8 Yahoo.com 3.8B U.S. News and Media 9 xvideos.com 3.4B Czech Republic Adult 10 pornhub.com 3.3B Canada Adult ... 41 Walmart.com 718.6M U.S. Marketplace 42 Bilibili.com 686.0M China Animation and Comics 43 Tiktok.com 663.2M China Social Networks and Online Communities 44 Paypal.com 657.2M U.S. Financial Planning and Management 45 Google.de 624.5M Germany Search Engines 46 Amazon.co.jp 619.2M Japan Marketplace 47 Aliexpress.com 611.0M China Marketplace 48 Amazon.de 608.8M Germany Marketplace 49 Rakuten.co.jp 593.4M Japan Marketplace 50 Amazon.co.uk 579.7M United Kingdom MarketplaceTop 50 Websites
Google.com
YouTube
2021-Sep-02 2021-Aug-24survival8.blogspot.com
Ref: visualcapitalist Tags: Technology,Cyber Security,Web Scraping,
Wednesday, September 1, 2021
Extracting Text from Docx, Doc and Pdf files Using Python
import os import docx # pip install python-docx # Does not support .pdf and .doc import PyPDF2 from docx import Document SUPPORTED_FORMATS = ['pdf', 'doc', 'docx'] WORD_FORMATS = ['doc', 'docx']For DOCX
f_list = [] for dirpath, subdirs, files in os.walk("."): for f in files: if f.split(".")[1] == "docx" and f[0] != "~": f_list.append(os.path.join(dirpath, f)) d_list = [] for f in f_list: d_list.append(Document(f)) t_list = [] for d in d_list: para_text = "" for para in d.paragraphs: para_text = para_text + " " + para.text t_list.append(para_text)FOR PDF
f_list = [] for dirpath, subdirs, files in os.walk("."): for f in files: if f.split(".")[1] == "pdf" and f[0] != "~": #print(f, dirpath) f_list.append(os.path.join(dirpath, f)) d_list = [] t_list = [] for f in f_list: pdfFileObj = open(f, 'rb') pdfReader = PyPDF2.PdfFileReader(pdfFileObj) d_list.append(pdfReader) text = "" for pageObj in pdfReader.pages: text = text + " " + pageObj.extractText() t_list.append(text) pdfFileObj.close()For DOC
import win32com.client word = win32com.client.Dispatch("Word.Application") word.visible = False wb = word.Documents.Open(r'D:\xyz.doc') doc = word.ActiveDocument print(doc.Range().Text) Tags: Technology,Python,Natural Language Processing,
Managing task on Windows using CMD
Managing task on Windows using CMD using "tasklist", "taskkill" and "findstr".
(base) CMD>tasklist | findstr "WINWORD*"
WINWORD.EXE -- 3164 Console -- 7 251,092 K
WINWORD.EXE -- 9292 Console -- 7 245,508 K
WINWORD.EXE -- 13640 Console -- 7 246,748 K
WINWORD.EXE -- 10288 Console -- 7 242,704 K
WINWORD.EXE -- 19244 Console -- 7 246,996 K
WINWORD.EXE -- 18416 Console -- 7 246,552 K
WINWORD.EXE -- 12380 Console -- 7 163,932 K
(base) CMD>taskkill /F /PID 3164
SUCCESS: The process with PID 3164 has been terminated.
(base) CMD>taskkill /IM "WINWORD.exe" /F
SUCCESS: The process "WINWORD.EXE" with PID 13640 has been terminated.
SUCCESS: The process "WINWORD.EXE" with PID 10288 has been terminated.
SUCCESS: The process "WINWORD.EXE" with PID 19244 has been terminated.
SUCCESS: The process "WINWORD.EXE" with PID 18416 has been terminated.
SUCCESS: The process "WINWORD.EXE" with PID 12380 has been terminated.
Tags: Technology,Windows CMD,
Convert MS Word files into PDF format using Python on Windows
import os
import PyPDF2
import sys
import comtypes.client
SUPPORTED_FORMATS = ['pdf', 'doc', 'docx']
WORD_FORMATS = ['doc', 'docx']
f_list = []
for dirpath, subdirs, files in os.walk("."):
for f in files:
if f.split(".")[1] in WORD_FORMATS and f[0] != "~":
f_list.append(os.path.join(dirpath, f))
"""
The following code converts "doc" and "docx" files to "pdf". But once it opens the files, due to some issue in our code
(usually an unattended Word prompt) it does not closes the files properly and the Operating System file lock
remains open on the file. So the code runs for one time but not the second time unless we end the "Word" program
instances from the Task Manager.
"""
os.system('taskkill /IM "WINWORD.exe" /F')
wdFormatPDF = 17
for f in f_list:
in_file = os.path.abspath(f)
out_file = in_file.split(".")[0] + ".pdf".strip()
word = comtypes.client.CreateObject('Word.Application')
word.Visible = True
doc = word.Documents.Open(in_file)
doc.SaveAs(out_file, FileFormat=wdFormatPDF)
#doc.Close()
#word.Quit()
os.system('taskkill /IM "WINWORD.exe" /F')
Other Notes
import docx
# pip install python-docx
# Does not support .pdf and .doc
Tags: Technology,Python,Natural Language Processing,
Subscribe to:
Comments (Atom)


















