This commit is contained in:
512song committed 2024-04-10 19:58:54 +08:00
1 parent daf054d886
commit 55437a8820
3 files changed
+1808 -123

No files matched your search

+84 -18
View File
@@ -615,6 +615,87 @@
"print('ok') "
]
},
{
"cell_type": "markdown",
"id": "75aa9852-64aa-4ede-b9a0-00cd4f672527",
"metadata": {},
"source": [
"## word文档读取表格"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2d01d0ae-ab9a-4798-9723-51e4fd2bb112",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import docx\n",
"import json\n",
"doc = docx.Document(r\"data/中国石化北海炼化有限责任公司2023年健康体检团体报告.docx\")\n",
"for i, table in enumerate(doc.tables):\n",
" print(f\"Table {i}:\")\n",
" for row in table.rows:\n",
" print(type(row))\n",
" for cell in row.cells:\n",
" if cell.text =='序号':\n",
" print(cell.text, end=\" | \")\n",
" print() # 每一行结束后换行\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "c3ccfa25-f31c-46d4-bfd0-903832cc620d",
"metadata": {
"execution": {
"iopub.execute_input": "2024-04-10T11:08:13.063231Z",
"iopub.status.busy": "2024-04-10T11:08:13.063012Z",
"iopub.status.idle": "2024-04-10T11:08:25.173599Z",
"shell.execute_reply": "2024-04-10T11:08:25.173048Z",
"shell.execute_reply.started": "2024-04-10T11:08:13.063213Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ok!\n"
]
}
],
"source": [
"import docx\n",
"import json\n",
"import openpyxl\n",
"doc = docx.Document(r\"data/中国石化北海炼化有限责任公司2023年职业体检团体报告.docx\")\n",
"list2 = []\n",
"for i, table in enumerate(doc.tables):\n",
" \n",
" if table.cell(0, 0).text == '序号':\n",
" #print(f\"Table {i}:\")\n",
" \n",
" for row in table.rows:\n",
" list1= []\n",
" for cell in row.cells:\n",
" list1.append(cell.text)\n",
" if list1[0] !='序号':\n",
" list2.append(list1)\n",
"filename = '北海体测报告情况表2024.xlsx'\n",
"wb = openpyxl.Workbook()\n",
"sheet = wb.active\n",
"\n",
"for row in list2:\n",
" sheet.append(row)\n",
" \n",
"wb.save(filename)\n",
"print('ok!')"
]
},
{
"cell_type": "code",
"execution_count": null,
@@ -735,27 +816,12 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": null,
"id": "d05d85d2-5229-446e-bfff-1f0feb8b7f09",
"metadata": {
"execution": {
"iopub.execute_input": "2023-04-10T10:04:32.991821Z",
"iopub.status.busy": "2023-04-10T10:04:32.991387Z",
"iopub.status.idle": "2023-04-10T10:04:34.154437Z",
"shell.execute_reply": "2023-04-10T10:04:34.153707Z",
"shell.execute_reply.started": "2023-04-10T10:04:32.991783Z"
},
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13\n"
]
}
],
"outputs": [],
"source": [
"import openpyxl\n",
"from docxtpl import DocxTemplate,InlineImage\n",
@@ -1175,7 +1241,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
"version": "3.10.12"
}
},
"nbformat": 4,