使用Phpword实现word转html

//Word转HTML
$phpWord = \PhpOffice\PhpWord\IOFactory::load('./word/hello.docx');
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, "HTML");
$xmlWriter->save('./html/hello.html'); 

Mathpix收费了?快使用API吧,个月免费识别1000次!

最近,数学公式OCR神器Mathpix 开始收费,一个月只能免费用50次,再用就得5刀一月,小伙伴们顿时叫苦不迭。想想这种套路国内互联网公司不知道玩了多少次了,从滴滴打车到美团外卖再到共享单车,都是先培养消费习惯再收割一波韭菜。但是当初大家都只顾享受免费识别公式带来的快感,哪知由俭入奢易,由奢入俭难,现在再手敲公式真是痛苦不堪。

目前来看,由于需要联网识别,基本没有空子可钻。当然也不排除个别神人用大量邮箱来白嫖:如何看待 Mathpix 收费?​www.zhihu.com

但是,Mathpix官网对于一些有能力的开发者还是开了一扇后门的:Mathpix OCR​mathpix.com

从它提供的OCR API来看,一个月可以免费使用1000次!在之后一直到10万次都是0.004美元(约合0.028元)一次。对个人使用者而言,1000次已经基本够了。

首先需要登录平台:MathpixOCR login​dashboard.mathpix.com

登录之后需要输入信用卡信息,完成账号激活。

激活后,将会显示API的id和key:

API官方文档:Mathpix API v3 Reference​docs.mathpix.com

Github上给出了具体示例:Mathpix/api-examples​github.com

以Python为例,只需要在mathpix.py中填写自己的id和key,再调用simple.py就能运行。从示例来看,它识别的是本地图片。我在它的基础上加入了识别剪贴板的代码,从而达成与Mathpix相似度为99%的使用体验。

import os
import base64
import requests
import json
from PIL import ImageGrab
#
# Common module for calling Mathpix OCR service from Python.
#
# N.B.: Set your credentials in environment variables APP_ID and APP_KEY,
# either once via setenv or on the command line as in
# APP_ID=my-id APP_KEY=my-key python3 simple.py 
#

env = os.environ

default_headers = {
    'app_id': env.get('APP_ID', '你的id'),
    'app_key': env.get('APP_KEY', '你的key'),
    'Content-type': 'application/json'
}

service = 'https://api.mathpix.com/v3/latex'

#
# Return the base64 encoding of an image with the given filename.
#
def image_uri(filename):
    image_data = open(filename, "rb").read()
    return "data:image/jpg;base64," + base64.b64encode(image_data).decode()

#
# Call the Mathpix service with the given arguments, headers, and timeout.
#
def latex(args, headers=default_headers, timeout=30):
    r = requests.post(service,
        data=json.dumps(args), headers=headers, timeout=timeout)
    return json.loads(r.text)


def mathpix_clipboard(): # 识别剪贴板公式
    im = ImageGrab.grabclipboard()
    im.save('equa.png','PNG')
    r = latex({
        'src': image_uri("equa.png"),
        'formats': ['latex_simplified']
    })
    print(r['latex_simplified'])

if __name__ == '__main__':
    mathpix_clipboard()