提交 cb42e65a 编写于 作者: S SodaCris

init

上级
.ipynb_checkpoints
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "7fdf4d6b-cfa3-4d19-b716-6f7942d88d8b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from docx import Document"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "51b82a2c-3865-43ef-8b85-8806d6e95a0f",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"document=Document(\"file.docx\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "cb771fad-4593-43c2-a223-c20731e44b2b",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<docx.table.Table at 0x7f599bdb0730>]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"document.tables"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "fd55a82b-96a9-4671-9b8d-30a59a790340",
"metadata": {},
"outputs": [],
"source": [
"t=document.tables[0]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "1bc5acab-4bb5-44b1-bd65-9fe814c0c2ad",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'建筑物名称'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(0,0).text"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0d8465c6-7e59-4bdb-88cf-3ed75f0e10a2",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'烟草大厦'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(0,2).text"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c36a18b5-3512-431f-8181-2bb405328548",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'建筑物地址'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(0,4).text"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "c88e97b0-afe6-4a93-a115-43260ba5fab9",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'东湖区子安路76号'"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(0,6).text"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "845fb6d5-7664-436f-9989-279dd46be1fd",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'实际检验面积'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(2,7).text"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "18373029-1060-4268-bd17-69d37db6b968",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'302'"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(3,7).text"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "559137bd-fdf4-425a-b17d-95d6f2a0603a",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'建设单位'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(4,1).text"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "7bc94725-9a99-463c-85e9-77a6b943b283",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'某单位'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(4,2).text"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "fe24d48b-6758-4458-ad6e-526c080ec80e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'联系人'"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(4,4).text"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "9ff8a0b6-74eb-4d2a-8a35-9bbfe5fff2fc",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'李小七'"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(4,5).text"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "f28a3479-762e-4fe8-84af-fa5f7c8c3465",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'联系电话'"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(4,6).text"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "61a2fa50-aa0a-4fe2-a6b4-464aaa1c539d",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'13783541151'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(4,7).text"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "5f968f91-a4d4-4af5-ad08-9102fe32035f",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'检验日期'"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(6,5).text"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "57f111c3-c5a0-4fbd-b4a0-5d8c71373530",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'2021-7-10'"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(6,6).text"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "1db77c2f-9afc-402a-b1ef-2dd1eaf99473",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'受烟草公司委托….结论如下:'"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"t.cell(12,1).text"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "a8c9ad60-da01-4aa8-b937-c0ea7dbacf17",
"metadata": {},
"outputs": [],
"source": [
"import re"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "654d01a1-d128-4b89-ba05-84fb9bec2161",
"metadata": {},
"outputs": [],
"source": [
"comp=re.findall(\".*受(.*)委托.*\",t.cell(12,1).text)[0]"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "dd0e4897-14c2-477d-a57c-93538fb8da16",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'烟草公司'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"comp"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "a85719bf-6e61-499a-bb79-23bef69335f4",
"metadata": {},
"outputs": [],
"source": [
"from openpyxl import Workbook\n",
"wb = Workbook()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "bd7e1c1e-1e5c-4feb-abfa-9b6a6a827028",
"metadata": {},
"outputs": [],
"source": [
"ws=wb.active"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "cc0ca11f-1f88-4413-91be-09628c5cc2f8",
"metadata": {},
"outputs": [],
"source": [
"col_arr=['序号','检验日期','建筑物名称','建筑物地址','委托单位',\n",
" '检测面积','检验类别','建设单位','联系人','联系电话']"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "e161b975-203f-42d2-b02d-353447e504da",
"metadata": {},
"outputs": [],
"source": [
"i=1"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "c739f202-9da5-4b48-96e4-74cb4e889e06",
"metadata": {},
"outputs": [],
"source": [
"ws.append(col_arr)"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "3b906446-3e0c-426f-a90d-0230415122f7",
"metadata": {},
"outputs": [],
"source": [
"idx_arr=[[6,6],[0,2],[0,6],[3,7],[4,2],[4,5],[4,7]]"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "bf95a42e-01f9-445a-b2e0-5f6c2aebb50e",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['2021-7-10', '烟草大厦', '东湖区子安路76号', '302', '某单位', '李小七', '13783541151']"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"[t.cell(i[0],i[1]).text for i in idx_arr]"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "095592a2-91cc-41ce-930e-ba7be740afea",
"metadata": {},
"outputs": [],
"source": [
"x=[t.cell(i[0],i[1]).text for i in idx_arr]"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "d8b22579-dce8-4100-ace2-450465461a4e",
"metadata": {},
"outputs": [],
"source": [
"x.insert(4,'新检')"
]
},
{
"cell_type": "code",
"execution_count": 32,
"id": "84b4a7a2-42ec-4d0d-ba35-ae748f097874",
"metadata": {},
"outputs": [],
"source": [
"x.insert(3, comp)"
]
},
{
"cell_type": "code",
"execution_count": 33,
"id": "5347f85e-2de2-43f7-8714-1d0159d92f8c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['2021-7-10',\n",
" '烟草大厦',\n",
" '东湖区子安路76号',\n",
" '烟草公司',\n",
" '302',\n",
" '新检',\n",
" '某单位',\n",
" '李小七',\n",
" '13783541151']"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x"
]
},
{
"cell_type": "code",
"execution_count": 34,
"id": "1802edce-94cb-404e-80e6-eca0194f0053",
"metadata": {},
"outputs": [],
"source": [
"ws.append([i,*x])"
]
},
{
"cell_type": "code",
"execution_count": 35,
"id": "68d16288-f354-4f38-a5e6-5c2fbb8fa677",
"metadata": {},
"outputs": [],
"source": [
"wb.save(\"out.xlsx\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
# docx read sample
Proof-of-Concept: 使用Python读取Micorosoft Word (.docx)文件,并将结果写入到Excel 2010 (.xlsx)文件中
文件已添加
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册