Posts: 5
Threads: 1
Joined: Jun 2022
Jun-21-2022, 10:35 AM
(This post was last modified: Jun-21-2022, 10:36 AM by ebincharles869.)
Hello,
I am sending whatsapp message through python. Below is the code.
1 2 3 |
import pywhatkit
pywhatkit.sendwhats_image( "E9wnucCUpBpD1aizIUyLv6" , "E:\\PythonFiles\\Music.jpg" , "footer.txt" , 10 )
pywhatkit.sendwhatmsg_to_group( "E9wnucCUpBpD1aizIUyLv6" , "Test" , 13 , 3 , 15 , True , 5 )
|
But i want to send a paragraph of message which is typed and saved in the same directory in text file.
How can i code to take message from the text file.
Thanks for the big help.
regards,
ebin
Posts: 7,324
Threads: 123
Joined: Sep 2016
If pywhatkit don't take a file object you can do it like this.
1 2 3 4 5 |
import pywhatkit
with open ( "footer.txt" ) as f:
pywhatkit.sendwhats_image( "E9wnucCUpBpD1aizIUyLv6" , "E:\\PythonFiles\\Music.jpg" , f.read(), 10 )
pywhatkit.sendwhatmsg_to_group( "E9wnucCUpBpD1aizIUyLv6" , "Test" , 13 , 3 , 15 , True , 5 )
|
Posts: 5
Threads: 1
Joined: Jun 2022
But i get error like this
Error: Traceback (most recent call last):
File "C:\Users\sisya\PycharmProjects\Whatsapppython\main.py", line 9, in <module>
with open("footer.txt") as f:
FileNotFoundError: [Errno 2] No such file or directory: 'footer.txt'
(Jun-21-2022, 10:49 AM)snippsat Wrote: If pywhatkit don't take a file object you can do it like this.
1 2 3 4 5 |
import pywhatkit
with open ( "footer.txt" ) as f:
pywhatkit.sendwhats_image( "E9wnucCUpBpD1aizIUyLv6" , "E:\\PythonFiles\\Music.jpg" , f.read(), 10 )
pywhatkit.sendwhatmsg_to_group( "E9wnucCUpBpD1aizIUyLv6" , "Test" , 13 , 3 , 15 , True , 5 )
|
Posts: 7,324
Threads: 123
Joined: Sep 2016
You most have footer.txt in same folder as main.py.
Or give path to where you have footer.txt.
1 |
with open (r "C:\somefolder\footer.txt" ) as f:
|
Posts: 5
Threads: 1
Joined: Jun 2022
(Jun-21-2022, 11:27 AM)snippsat Wrote: You most have footer.txt in same folder as main.py.
Or give path to where you have footer.txt.
1 |
with open (r "C:\somefolder\footer.txt" ) as f:
|
Below is the error i am receiving. I guess there should be string explained inside the bracket in f.read().
1 |
pywhatkit.sendwhatmsg_to_group( "E9wnucCUpBpD1aizIUyLv6" , f.read(), 15 , 2 )
|
Error: C:\Users\sisya\PycharmProjects\Whatsapppython\venv\Scripts\python.exe C:/Users/sisya/PycharmProjects/Whatsapppython/main.py
Traceback (most recent call last):
File "C:\Users\sisya\PycharmProjects\Whatsapppython\main.py", line 18, in <module>
pywhatkit.sendwhatmsg_to_group("E9wnucCUpBpD1aizIUyLv6", f.read(), 15, 2)
File "E:\Python310\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 10: character maps to <undefined>
Posts: 7,324
Threads: 123
Joined: Sep 2016
Windows is messing up choosing wrong encoding.
Do this:
1 |
with open ( "footer.txt" , encoding = 'utf-8' ) as f:
|
Posts: 5
Threads: 1
Joined: Jun 2022
(Jun-21-2022, 12:22 PM)snippsat Wrote: Windows is messing up choosing wrong encoding.
Do this:
1 |
with open ( "footer.txt" , encoding = 'utf-8' ) as f:
|
Error: IndentationError: expected an indented block after 'with' statement on line 17
the above error comes for indented block after with statement.
Posts: 7,324
Threads: 123
Joined: Sep 2016
Jun-21-2022, 12:52 PM
(This post was last modified: Jun-21-2022, 12:52 PM by snippsat.)
(Jun-21-2022, 12:44 PM)ebincharles869 Wrote: the above error comes for indented block after with statement. Yes,Python need indentation π§
1 2 3 4 5 |
import pywhatkit
with open ( "footer.txt" , encoding = 'utf-8' ) as f:
pywhatkit.sendwhats_image( "E9wnucCUpBpD1aizIUyLv6" , "E:\\PythonFiles\\Music.jpg" , f.read(), 10 )
pywhatkit.sendwhatmsg_to_group( "E9wnucCUpBpD1aizIUyLv6" , "Test" , 13 , 3 , 15 , True , 5 )
|
Posts: 5
Threads: 1
Joined: Jun 2022
Its working Perfectly. Thank you.
However it works only when there English texts. I need to insert some symbols like below and other languages texts. I hope the copy paste function works. Is that possible.
ββ¨β¦ ββπΉββ β¦β¨β
βΌβΌββββββΌ
ππ§πβπ
__π
Posts: 7,324
Threads: 123
Joined: Sep 2016
It depend on what pywhatkit accept,it works from Python side.
1 2 |
with open ( "test.txt" , encoding = 'utf-8' ) as f:
print (f.read())
|
Output: Hello
ππ§πβπ
Try different stuff and just one Emoji at time as test.
1 2 |
print ( f '\N{grinning face}' )
print ( f '\N{eye}' )
|
Output: π
π
emoji 1.7.0
1 2 3 4 5 |
import emoji
print (emoji.emojize( 'Python is :thumbs_up:' ))
print (emoji.emojize( 'Python is fun :ghost:' ))
|
Output: Python is π
Python is fun π»
|