[{"data":1,"prerenderedAt":59},["ShallowReactive",2],{"article-104":3},{"code":4,"msg":5,"data":6,"count":14},200,"查询成功",{"id":7,"title":8,"keywords":9,"description":10,"category_id":11,"content":12,"body_html":13,"thumb_up":14,"clicks":15,"sort":14,"remark":13,"status":16,"is_open":16,"is_deleted":14,"is_top":16,"is_recommend":16,"create_time":17,"update_time":18,"image_id":19,"url":13,"member_id":14,"cate_name":20,"prev":21,"next":24,"tags":27,"words":30,"read_time":31,"comments":14,"cover":32,"relevant":33},104,"利用Python将PDF转html并提取文字和图片","python,pdf,提取","由于工作项目中所需要做一个提取pdf中内容和图片的功能，在使用过的几种方法中，还原度高的只有pdf2htmlEX，但pdf2htmlEX在项目中的用处不到，我们用更简单的方法实现",5,"由于工作项目中所需要做一个提取pdf中内容和图片的功能，在使用过的几种方法中，还原度高的只有pdf2htmlEX，但pdf2htmlEX在项目中的用处不到，我们用更简单的方法实现\n\n##### 1、安装相关python库\n```python\npip install PyMuPDF\npip install tqdm\npip install Beautifulsoup4\n```\n\n##### 2、代码实现\n```python\nimport fitz\nimport os,re,io\nfrom tqdm import tqdm\nimport base64\nfrom bs4 import BeautifulSoup\nfrom PIL import Image\n\ndef decode_image(src, dir, index):\n\n    # 1、信息提取\n    result = re.search(\"data:image\u002F(?P\u003Cext>.*?);base64,(?P\u003Cdata>.*)\", src, re.DOTALL)\n    if result:\n        ext = result.groupdict().get(\"ext\")\n        data = result.groupdict().get(\"data\")\n    else:\n        raise Exception(\"Do not parse!\")\n\n    imgdata = base64.b64decode(data)\n    im = Image.open(io.BytesIO(imgdata))\n    width, height = im.size\n\n    print(\"width:{},height:{}\".format(width, height))\n\n    # 2、base64解码\n    img = base64.urlsafe_b64decode(data)\n\n    path = dir.replace('.pdf', '')\n    if not os.path.exists(path):\n        os.makedirs(path, 777)\n    # 3、二进制文件保存\n    filename = \"{}{}.{}\".format(path+'\u002F', index, ext)\n    print(filename)\n    with open(filename, \"wb\") as f:\n        f.write(img)\n        f.close()\n        \n    # 人脸识别使用\n    if width % 4 != 0:\n        print('resize_4_pic:{}'.format(filename))\n        nw = (width % 4 == 0) and width or (width + (4 - (width % 4)))\n        nh = (height % 4 == 0) and height or (height + (4 - (height % 4)))\n\n        img = Image.open(filename)\n        img = img.resize( (nw,nh), Image.ANTIALIAS)\n        img.save(filename)\n\n    imgs.append(filename)\n    return filename\n\ndef read_pdf(srcfile):\n    doc = fitz.open(srcfile)\n    html_content = ''\n    for page in tqdm(doc):\n        html_content += page.get_text('html')\n\n    html_content = html_content.replace('\u003Cbr\u002F>', ' \\n').replace('\u003Cbr>', ' \\n').replace('\u003Cbr >', ' \\n').replace('\u003Cbr \u002F>', ' \\n')\n    html_content = html_content.replace(' ', ' ').replace('&#160;', ' ')\n\n    soup =  BeautifulSoup(html_content,'lxml')\n\n    index = 0\n    for imgtag in soup.find_all('img'):\n        decode_image(imgtag['src'], srcfile, index)\n        index += 1\n\n    return soup.text\n\nif __name__ == '__main__':\n    path = \"D:\u002Ftest.pdf\"\n    imgs = []\n    content = read_pdf(path)\n    print(content)\n    print(imgs)\n```",null,0,500,1,"2023-06-07 14:29:52","2023-06-08 00:09:34",62,"Python",{"id":22,"title":23},103,"面向对象三大特性五大原则 + 低耦合高内聚[转载]",{"id":25,"title":26},105,"php项目中调用python脚本使用pdf和image之间相互转换",[28],{"id":29,"name":20},27,1286,3,"https:\u002F\u002Ftp.myong.top\u002Fstorage\u002Fwechat\u002F505bf7fd163ad29e8ce69c9281fa05056fe3328c.jpeg",[34,39,44,49,54],{"id":35,"title":36,"create_time":37,"description":38},95,"Django启用memcache缓存","2020-10-23 11:17:27","在动态网站中,用户所有的请求,服务器都会去数据库中进行相应的增,删,查,改,渲染模板,执行业务逻辑,最后生成用户看到的页面.当一个网站的用户访问量很大的时候,每一次的的后台操作,都会消耗很多的服务端资源,所以必须使用缓存来减轻后端服务器的压力.",{"id":40,"title":41,"create_time":42,"description":43},91,"用Python实现编程语言 20 年的动态排行榜","2020-06-24 16:06:54","爬取一下，自2001年5月至今，TIOBE 编程语言排行榜上编程语言的变化情况，看一下在接近20年的时间里，编程语言的热度是如何变化的。\r\n",{"id":45,"title":46,"create_time":47,"description":48},100,"re正则表达式","2021-07-10 18:09:52","正则表达式是一个特殊的字符序列，它能帮助你方便的检查一个字符串是否与某种模式匹配，本文罗列re正则表达式中，常用的匹配符号。",{"id":50,"title":51,"create_time":52,"description":53},34,"Python基础整理之列表常见操作","2019-02-10 16:53:55","列表是最常用的Python数据类型，它可以作为一个方括号内的逗号分隔值出现。列表的数据项不需要具有相同的类型",{"id":55,"title":56,"create_time":57,"description":58},63,"Python与mysqldump的数据库备份","2019-10-14 15:35:33","mysqldump是备份MySQL数据库的一种好工具。它相对于用phpmyadmin等备份工具更加快速，又避免受php.ini等的限制，在windows系统下还可以结合计划任务实现定时远程备份数据库",1783431655025]