边走边学

Learning while walking

1.Python操作PDF

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
# Python操作PDF书签
pip install PyMuPDF

import fitz

pdf_path = ''
pdf_handle = fitz.open(pdf_path)

# 获取PDF文件目录
bookmark = pdf_handle.getToC()

# 为PDF文件设置目录
# [目录层级, '目录文字''目录页码']
# toc = [
# [1, '一级目录', 1],
# [2, '二级目录', 2],
# [3, '三级目录', 3],
# ...
# ]
# doc.setToC(toc)

# 保存为新文件
# doc.save('new.pdf')

# 删除PDF文件目录
# doc._delToC()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Python合并多个PDF文件
pip install PyPDF2

import os
from PyPDF2 import PdfFileMerger

pdf_dir = ''
pdf_list = os.listdir(pdf_dir)

merger = PdfFileMerger(strict=False)
for pdf in file_list:
merger.append(pdf)

new_path = ''
merger.write(new_path)
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
# Python解密AES ECB加密的PDF文件

# Windows上openssl下载地址
32bit https://pan.baidu.com/s/1WfGOimIED7UAERgWsqvsfw
64bit https://pan.baidu.com/s/12a1K1-fP0jdqyv0zlwFkDw
同时配置名为 OPENSSL_CONF 的环境变量,值为上述安装路径下 openssl.cfg 的路径以及配置 openssl 环境变量

若Windows已安装PHP环境则可不必再次安装上述文件,但需要配置名为 OPENSSL_CONF 的环境变量,值为PHP安装目录下的 openssl.cnf 文件路径


def get_key(key):
"""
字符串key转ascii再转16进制
"""
hex_ascii_key = ''
for i in key:
hex_ascii_key += f'{ord(i):x}'
return hex_ascii_key

key = ''
pdf_encrypted_url = ''
pdf_name = ''
hex_ascii_key = get_key(key)
res = requests.get(url=pdf_encrypted_url, stream=True)
with open(pdf_name + '.aes', 'wb') as f:
f.write(res.content)
os.system(f'openssl enc -d -aes-128-ecb -K {hex_ascii_key} -in {pdf_name}.aes -out {pdf_name}.pdf')

2.Python操作调用JS文件

1
2
3
4
5
6
7
8
9
10
11
pip install PyExecJS

import execjs

js_path = 'playback.js'
def call_js(js_path, parameter):
with open(js_path, 'r', encoding='UTF-8') as f:
js_handle = f.read()
loader = execjs.compile(js_handle)
ret = loader.call('decodeUrl', parameter)
return ret