【Python】将微信收藏的文章批量导出为pdf

【Python】将微信收藏的文章批量导出为pdf

写在前面
微信收藏了大量的文章,一直没时间看。乘飞机或火车时有闲暇时间但网络不行。本文提供了一种基于python的将微信收藏的公众号推文批量导出为pdf的方法,方便在平板等移动设备离线阅读。

第一步:导出微信收藏的链接

这一步我并没有找到一键导出的办法,选择的方法是:

  1. 先将所有要导出的推文批量多选,并分享给好友或者文件传输助手;
    在这里插入图片描述

  2. 然后在聊天界面多选并使用邮箱发送;

  3. 可以发送给自己的其他邮箱。邮件的内容便包含了每篇文章的标题的网址;
    在这里插入图片描述

  4. 将邮件内容粘贴保存为.txt格式,编码类型为ANSI。

第二步:将所有网址导出为pdf

pdf文件名即微信推文标题,部分特殊字符进行了替换。此方法仅适用于微信推文,筛除了不是微信推文的链接。
代码:

import pdfkit
import requests
import os
import re
from bs4 import BeautifulSoup

# set the font style
font = '''
    <style type="text/css">
         @font-face{font-family: "微软雅黑";src:url("‪C:\\Windows\\Fonts\\msyh.ttc")
    </style>
     <style type = "text/css">
        p { font-family: '微软雅黑', cursive;}
    </style>
    '''
# 选项
options = {
    'page-size': 'A6',
#     'margin-top': '0.75in',
#     'margin-right': '0.75in',
#     'margin-bottom': '0.75in',
#     'margin-left': '0.75in',
    'encoding': "UTF-8",
#      'custom-header': headers,
#     'debug-javascript': [''],
#     'javascript-delay': 10000,
#     'no-stop-slow-scripts': "",
#     'load-media-error-handling': 'abort',
 } 

path_wkthmltopdf = r'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe'
config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)

# 替换特殊字符
def validate_title(title):
    rstr = r"[\/\\\:\*\?\"\<\>\|]"  # '/ \ : * ? " < > |'
    new_title = re.sub(rstr, "_", title)  # 替换为下划线
    new_title = new_title.replace(':',' _')
    new_title = new_title.replace('?','_')
    return new_title

f = open("E:\\user\\Desktop\\wechat\\url.txt")          # 返回一个文件对象   
line = f.readline() 
while line:
    if line[0]=='[':
        # 读取网址和标题
        line2 = line[1:-2]
        urls = line2.split(' : ')
        url = urls[-1]
        
        title = ''.join(urls[0:-1])
        title = validate_title(title)
        
        if url.find('mp.weixin.qq.com') > 0:
            res = requests.get(url)
            # data-src替换为src 有时候返回的正文被隐藏了,将hidden去掉
            html = res.text.replace("data-src", "src").replace('style="visibility: hidden;"',"")
             
            soup = BeautifulSoup(html)
            # 选择正文(去除javascrapt等)
            html = soup.select('div#img-content')[0]
            
            # html_str = str(html)
            # i = 0
            # title = html_str.splitlines()[i]
            # while title[0] == '<' or title.isspace():
            #     i = i+1
            #     title = html_str.splitlines()[i]
            #     title = title.replace('|','-')	# 将字符串里的k全部替换为8
            
            # 修改字体
            html = font + str(html)
            
            output = title+'.pdf'
            
            pdfkit.from_string(str(html), output, configuration=config, options=options)
        else:
            print(title)
        
    line = f.readline() 
f.close

代码主要参考了【实战】Python将微信文章保存为PDF,使用的是 “wkhtmltopdf” 命令行工具,下载地址:https://wkhtmltopdf.org/downloads.html
在11寸左右的平板上阅读的话,实测将页面保存为A6是比较合适的,可以在options里设置,具体参数参考Paper Sizes

在读取txt文档时,参考了一些操作关于python的方法文章,具体如下:

  1. python逐行读取文本
  2. python字符串截取、查找、分割
  3. Python3 读取字符串的第一行
  4. Python拼接字符串的7种方法总结
  5. python字符串去掉特殊符号
  6. Python-替换或去除不能用于文件名的字符
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

撼沧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值