python's experience of writing wechat to automatically grab files

Keywords: Python pip Linux Windows

python's experience of writing wechat to automatically grab files


Recently, the epidemic is rampant. All the teachers are teaching online. There are a lot of messy documents in wechat group. I'm tired of my baby. I have to read them every time. If the doc has to be converted into pdf and put it into the PDF Expert of iPad Pro, I have to import pages from wechat during this period, which makes my iCloud Drive messy. There is a problem in making pdf typesetting of quick instructions, so I decided to make it hard Core once, use python to do it!

Early stage

Of course, I went to collect information,
Wechat operation: itchat
doc to pdf: pypiwin32+wps (rich people can use office, linux can search online by themselves)
Note that this requires a Windows environment.

pip install itchat
pip install pypiwin32

Write code & Pit

There are many articles about itchat. I won't go over them here, but one thing that we don't know very well (or I didn't see it) is how itchat downloads attachments from wechat.
First of all, the received message will be packaged into a very friendly msg object, which is a dictionary with a bunch of key s. If you are interested in it, you can dir check it or search it online.
We need to use the Text and FileName key s.


First, write a function that runs when receiving a message. The function has only one parameter, which is the received message. It is generally named msg. Add the @ statement before def (this is called the modifier). When the message you accept is an attachment (file), the object corresponding to msg's Text key is a function! Yes, it's a function! So the length of the download file is as follows:


But where will it be downloaded? Nowhere, in memory, it will return back as a bytes object.
So the whole download code is as follows:

with open(msg['FileName'],'wb') as file:#Remember wb's b!!!

Just fine.
It's suggested that you print it, or you may think you haven't received it
As for PDF, it's not a small pit.
First of all, the online tutorial will tell you this:


The precondition for such a child call is that Microsoft Word is installed.
If there is only wps, the first line should be changed to:


... Do you?
Some tutorials say so, but at least the latest version I download now looks like this:


It's said that it's because wps snatches the fresh version. Anyway, it has to be written like this.
Then you run my code like this. There must be something wrong. I can't open the file. The reason is that the FileName in the msg is a file name rather than an absolute path. In fact, it's ok if you think about it. The problem is that when you call wps, it won't inherit your working directory, and it doesn't even have a working directory. You have to specify an absolute path.
When you can't figure it out, you must remember to print something!!!!!!
Then there is After changing the code of opening doc, remember to add the absolute path to the code of saving pdf I won't tell you that tm

from win32com.client import gencache,constants
import itchat,pdb,os,time,win32com,sys
def workPath():
    for i in directory:
    return workPath
def convertToPDF(docPath,pdfPath):
    print("....Converting to PDF...")


def gotAttachment(msg):
    if not ('pdf' in msg['FileName'] or 'doc' in msg['FileName']): return None
    print("Gotten Attachment: %s"%msg['FileName'])
    with open(msg['FileName'],'wb') as file:
    if 'doc' in end:
        os.system('del "'+msg['FileName']+'"')
    print("...Saved. ")
    return None

Because there is Quit() in the code, it seems that it will cause the code analyzer to exit directly.
Scatter flowers! Don't worry about the teacher sending out documents anymore~~~~~~

Published 2 original articles, praised 0 and visited 336
Private letter follow

Posted by ricroma on Sun, 09 Feb 2020 04:58:00 -0800