沧州海外网站建设,炫酷网页html代码,河北建设厅查询官方网站,wordpress pdf 加密前言 因为一些项目原因#xff0c;我需要提供解析docx内容功能。本来以为这是一件比较简单的工作#xff0c;没想到在解析复选框选项上吃了亏#xff0c;并且较长一段时间内通过各种渠道都没有真正解决这一问题#xff0c;反而绕了远路。 终于#xff0c;我在github pytho…前言 因为一些项目原因我需要提供解析docx内容功能。本来以为这是一件比较简单的工作没想到在解析复选框选项上吃了亏并且较长一段时间内通过各种渠道都没有真正解决这一问题反而绕了远路。 终于我在github python-docx模块的Issues中找到了重要的思路及线索并最终通过后续努力实现了【解析docx/word文件文字、图片、复选框】这一功能。
Feature: Read checkboxes in Word forms · Issue #224 · python-openxml/python-docx · GitHub python-docx基础操作
# 安装python-docx模块
pip install python-docx
import os
import docx
import time# 图片附件的存储地址
image_save_path appendix_dir# 读取docx表格里的数据图片及文字
def read_table_from_docx(file_path)::param file_path::return: table_data, images# 读取docx/word文件doc docx.Document(file_path)# 获取docx中的table对象tables doc.tablestable_data []images []# 拿取文件中的图片对象并存储在images列表里for rel in doc.part.rels.values():if image in rel.reltype:image rel.target_partimage_data image.blobimages.append(image_data)# 读取文件表格中的文字内容# 这里不能解析特殊字符和复选框# 并且合并单元格的文字内容将出现多行多列重复出现需要注意for table in tables:for row in table.rows:row_data []for cell in row.cells:# print(cell, cell.text)row_data.append(cell.text)table_data.append(row_data)return table_data, imagestable_data, images read_table_from_docx(template.docx)
print(table_data)# 另存docx图片到本地
for i, image_data in enumerate(images):# 拼接 存储图片 绝对路径image_name fexpert_{int(time.time() * 1000)}.jpgwith open(os.path.join(image_save_path, image_name), wb) as f:f.write(image_data) 拿取复选框选项 关于docx复选框在这次项目中遇到了一种独特的复选框样式这种样式并不是通过wps里的【复选框内容控件】创建的让我一时没办法找到方向。 这是正常用wps添加的复选框方式 很明显和我的目标不太一样 二者都没办法通过【python-docx基础操作】拿到因此我只能继续刨坑终于如【前言】所述我不得已去模块github的评论区里找到了线索——直接以xml的形式剖析docx文件并获取复选框选项。 这里为了节约文本资源太懒了直接上代码吧
from docx import Documentdocument Document(template1.docx)
tables document.tables
content []
for table in tables:for row in table.rows:for cell in row.cells:for paragraph in cell.paragraphs:p paragraph._element# 打印docx的xml内容形式# print(p.xml)# 拿取所有w14:checkbox标签的匹配xml数据checkBoxes p.xpath(.//w14:checkbox)if checkBoxes:# 解析w14:checkbox内部的内容for checkBox in checkBoxes:# 尝试匹配xml中的w14:val对象也就是上面wps自建的复选框checked_state checkBox.xpath(.//w14:checked/w14:val, namespaces{w14:http://schemas.microsoft.com/office/word/2010/wordml})if checked_state:checked_value checked_state[0] # 获取第一个匹配的属性值print(paragraph.text, Checked value:, checked_value)break# 这是原模板的复选框选项拿取方案# checkBoxes p.xpath(.//w:r)# if checkBoxes:# for checkBox in checkBoxes:# checked_state checkBox.xpath(.//w:sym/w:char)# if checked_state:# checked_value checked_state[0] # 获取第一个匹配的属性值# print(paragraph.text, Checked value:, checked_value)# break 这是我的结果【1是选择0是未选择】 这是docx解析后的xml内容请自行体会代码与它的联系吧
w:p xmlns:whttp://schemas.openxmlformats.org/wordprocessingml/2006/main xmlns:wpchttp://schemas.microsoft.com/office/word/2010/wordprocessingCanvas xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006 xmlns:ourn:schemas-microsoft-com:office:office xmlns:rhttp://schemas.openxmlformats.org/officeDocument/2006/relationships xmlns:mhttp://schemas.openxmlformats.org/officeDocument/2006/math xmlns:vurn:schemas-microsoft-com:vml xmlns:wp14http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing xmlns:wphttp://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing xmlns:w14http://schemas.microsoft.com/office/word/2010/wordml xmlns:w10urn:schemas-microsoft-com:office:word xmlns:w15http://schemas.microsoft.com/office/word/2012/wordml xmlns:wpghttp://schemas.microsoft.com/office/word/2010/wordprocessingGroup xmlns:wpihttp://schemas.microsoft.com/office/word/2010/wordprocessingInk xmlns:wnehttp://schemas.microsoft.com/office/word/2006/wordml xmlns:wpshttp://schemas.microsoft.com/office/word/2010/wordprocessingShape xmlns:wpsCustomDatahttp://www.wps.cn/officeDocument/2013/wpsCustomDataw:pPrw:jc w:valboth/w:rPrw:rFonts w:hinteastAsia w:ascii仿宋_GB2312 w:hAnsi仿宋_GB2312 w:eastAsia仿宋_GB2312 w:cs仿宋_GB2312/w:u w:valsingle/w:lang w:valen-US w:eastAsiazh-CN//w:rPr/w:pPrw:rw:rPrw:rFonts w:hintdefault w:ascii仿宋_GB2312 w:hAnsi仿宋_GB2312 w:eastAsia仿宋_GB2312 w:cs仿宋_GB2312/w:sz w:val21/w:szCs w:val21//w:rPrw:sym w:fontWingdings w:char00A8//w:rw:rw:rPrw:rFonts w:hinteastAsia w:ascii仿宋_GB2312 w:hAnsi仿宋_GB2312 w:eastAsia仿宋_GB2312 w:cs仿宋_GB2312/w:sz w:val21/w:szCs w:val21/w:lang w:valen-US w:eastAsiazh-CN//w:rPrw:t这是选项一/w:t/w:rw:bookmarkStart w:id0 w:name_GoBack/w:bookmarkEnd w:id0/w:sdtw:sdtPrw:rPrw:rFonts w:hinteastAsia w:ascii仿宋_GB2312 w:hAnsi仿宋_GB2312 w:eastAsia仿宋_GB2312 w:cs仿宋_GB2312/w:color w:valauto/w:kern w:val2/w:sz w:val21/w:szCs w:val24/w:lang w:valen-US w:eastAsiazh-CN w:bidiar-SA//w:rPrw:id w:val147457823/w14:checkboxw14:checked w14:val1/w14:checkedState w14:val2612 w14:fontMS Gothic/w14:uncheckedState w14:val2610 w14:fontMS Gothic//w14:checkbox/w:sdtPrw:sdtEndPrw:rPrw:rFonts w:hinteastAsia w:ascii仿宋_GB2312 w:hAnsi仿宋_GB2312 w:eastAsia仿宋_GB2312 w:cs仿宋_GB2312/w:color w:valauto/w:kern w:val2/w:sz w:val21/w:szCs w:val24/w:lang w:valen-US w:eastAsiazh-CN w:bidiar-SA//w:rPr/w:sdtEndPrw:sdtContentw:rw:rPrw:rFonts w:asciiMS Gothic w:hAnsiMS Gothic w:eastAsia宋体 w:csTimes New Roman/w:color w:valauto/w:kern w:val2/w:sz w:val21/w:szCs w:val24/w:lang w:valen-US w:eastAsiazh-CN w:bidiar-SA//w:rPrw:t☒/w:t/w:r/w:sdtContent/w:sdtw:rw:rPrw:rFonts w:hinteastAsia w:ascii仿宋_GB2312 w:hAnsi仿宋_GB2312 w:eastAsia仿宋_GB2312 w:cs仿宋_GB2312/w:i w:val0/w:iCs w:val0/w:color w:val000000/w:kern w:val0/w:sz w:val21/w:szCs w:val21/w:u w:valnone/w:bdr w:valsingle w:color000000 w:sz4 w:space0/w:lang w:valen-US w:eastAsiazh-CN w:bidiar//w:rPrw:drawingwp:anchor distT0 distB0 distL114300 distR114300 simplePos0 relativeHeight251659264 behindDoc0 locked0 layoutInCell1 allowOverlap1wp:simplePos x0 y0/wp:positionH relativeFromcolumnwp:posOffset0/wp:posOffset/wp:positionHwp:positionV relativeFromparagraphwp:posOffset0/wp:posOffset/wp:positionVwp:extent cx18415 cy19685/wp:effectExtent l0 t0 r0 b0/wp:wrapNone/wp:docPr id1 name图片_2/wp:cNvGraphicFramePr/a:graphic xmlns:ahttp://schemas.openxmlformats.org/drawingml/2006/maina:graphicData urihttp://schemas.openxmlformats.org/drawingml/2006/picturepic:pic xmlns:pichttp://schemas.openxmlformats.org/drawingml/2006/picturepic:nvPicPrpic:cNvPr id1 name图片_2/pic:cNvPicPr//pic:nvPicPrpic:blipFilla:blip r:embedrId4/a:stretcha:fillRect//a:stretch/pic:blipFillpic:spPra:xfrma:off x0 y0/a:ext cx18415 cy19685//a:xfrma:prstGeom prstrecta:avLst//a:prstGeoma:noFill/a:lna:noFill//a:ln/pic:spPr/pic:pic/a:graphicData/a:graphic/wp:anchor/w:drawing/w:r
/w:p