{"type":"doc","content":[{"type":"paragraph","attrs":{"id":"ddeaf693-81dc-406d-9868-eb7405c8e642","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"很多报表、对账单、回单都是 PDF 格式,没法直接用 pandas 读。本文用 pdfplumber 把 PDF 里的表格提取成 Python 列表 / 字典,再转 DataFrame 做后续分析。比复制粘贴稳得多,尤其适合批量处理几十份 PDF。"}]},{"type":"paragraph","attrs":{"id":"da539d99-82ca-4dec-92a4-4aac4a060e8e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"一、环境准备"}]},{"type":"codeBlock","attrs":{"id":"6141fb5c-2471-4157-ac92-e43217b9c594","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"javascript"},"content":[{"type":"text","text":"pip install pdfplumber pandas\n"}]},{"type":"paragraph","attrs":{"id":"d2f6449d-44ba-41bb-9b4b-34e264d20a3b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"二、核心思路"}]},{"type":"orderedList","attrs":{"id":"28ff5382-9709-44c5-a37d-7c925e2a8126","start":1,"isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"7ea3070c-9ae3-4860-8931-5e6cc814dca8"},"content":[{"type":"paragraph","attrs":{"id":"77076a0d-8877-4cc1-8745-daeda7f8f25b","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"pdfplumber.open 打开 PDF,逐页读取;"}]}]},{"type":"listItem","attrs":{"id":"5849bf36-8cc5-4251-8ab0-36b625ede53f"},"content":[{"type":"paragraph","attrs":{"id":"65cb866d-ce24-4fa7-84d3-4395cbc587b9","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"page.extract_tables() 返回每页所有表格(嵌套列表);"}]}]},{"type":"listItem","attrs":{"id":"2402c053-d889-4545-a16b-950013e4e9ad"},"content":[{"type":"paragraph","attrs":{"id":"eb7203db-1a19-49b7-a359-2ebf320be459","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"第一行为表头,其余行 zip 成字典,方便入库;"}]}]},{"type":"listItem","attrs":{"id":"3085edf9-bdf7-4162-acb0-c8f0f1570b20"},"content":[{"type":"paragraph","attrs":{"id":"3575ee63-a009-4d5a-8929-fc4728770e0a","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"多个 PDF 循环提取,拼成一个总 DataFrame。"}]}]}]},{"type":"paragraph","attrs":{"id":"d69c80b3-45e9-41a6-a26b-df3c13e174c1","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"三、完整代码(自包含,可直接跑)"}]},{"type":"codeBlock","attrs":{"id":"aab41bb6-2bba-4949-83d2-d3184e161c6a","language":"javascript","theme":"atom-one-dark","runtimes":0,"isHoverDragHandle":false,"key":"","languageByAi":"javascript"},"content":[{"type":"text","text":"import pdfplumber\nimport pandas as pd\n\npdf_path = \"report.pdf\"\nrows = []\n\nwith pdfplumber.open(pdf_path) as pdf:\n for page in pdf.pages:\n tables = page.extract_tables()\n for table in tables:\n if not table:\n continue\n header = table[0]\n for r in table[1:]:\n rows.append(dict(zip(header, r)))\n\ndf = pd.DataFrame(rows)\nprint(df.head())\nprint(\"total rows:\", len(df))\n"}]},{"type":"paragraph","attrs":{"id":"21fb325a-5ebc-44f1-b769-c136b58c8dda","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"四、几个实战坑"}]},{"type":"bulletList","attrs":{"id":"be8ebb4a-77e8-4105-8b98-70732216179b","isHoverDragHandle":false},"content":[{"type":"listItem","attrs":{"id":"56edf2fc-fdd1-4ea5-8017-0d13c072eb98"},"content":[{"type":"paragraph","attrs":{"id":"d778b0c1-efd8-4335-8b2e-5f615319d869","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"扫描件 PDF(图片型)extract_tables 会返回空,必须先 OCR 转成文本型 PDF;"}]}]},{"type":"listItem","attrs":{"id":"2cae52f7-8463-4b53-b8e7-a4281c2ae16d"},"content":[{"type":"paragraph","attrs":{"id":"2c482398-8d33-40cd-8de3-493aefba1d7e","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"表格线缺失时提取会错位,可用 page.crop 先框定区域再 extract_table;"}]}]},{"type":"listItem","attrs":{"id":"27d26a6c-40a0-475a-9a5d-2553f4e81737"},"content":[{"type":"paragraph","attrs":{"id":"1b51607c-cc8b-4e75-bd33-434f29ab37ca","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"数字被提成字符串带千分位逗号,要 df[col].str.replace(\",\", \"\").astype(float) 清洗;"}]}]},{"type":"listItem","attrs":{"id":"493d9b19-652e-4316-ac3c-e8ade62f70f5"},"content":[{"type":"paragraph","attrs":{"id":"d44320ae-fb6c-456d-8b54-62e7f69ab65c","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"合并单元格会被拆成多行,需补前向值 fillna(method=\"ffill\");"}]}]},{"type":"listItem","attrs":{"id":"a96622f2-554f-4c6d-816a-b2d43060a7c8"},"content":[{"type":"paragraph","attrs":{"id":"39321407-8f33-46a0-a7f8-f2a4b68e0f40","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"大 PDF 逐页处理内存友好,不必一次性 load 全部。"}]}]}]},{"type":"paragraph","attrs":{"id":"cda4712f-631f-43e4-890d-d825d0c47f08","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"五、边界"}]},{"type":"paragraph","attrs":{"id":"dcc577f5-dc31-473d-a025-a9bb68b35127","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"脚本只负责把 PDF 文本结构化,数据真实性、勾稽关系由你复核。客户资料本地跑,别贴通用云端大模型。"}]},{"type":"paragraph","attrs":{"id":"35a68743-cf04-4776-80ba-7d030bb309b8","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"我是会计师事务所的一名注册会计师,做过财报审计、经济责任审计、专项审计、尽职调查、内控评价各类项目,从手写底稿到 AI 辅助都经历过。如果你也在财务 / 审计岗位想提效,欢迎关注、交流实战。"}]},{"type":"paragraph","attrs":{"id":"3a77328a-2b19-4a91-b644-3171398661bd","textAlign":"inherit","indent":0,"color":null,"background":null,"isHoverDragHandle":false},"content":[{"type":"text","text":"感谢你看到这里。如果这段代码对你有用,欢迎点赞 + 收藏 + 关注——我是会计师事务所的一名注册会计师,会继续分享审计提效与 Python 实战,咱们下篇见。"}]}]}