from docx import Document #pip3 install python-docx

from docx.shared import Inches #英寸

import os

#from docx import Document

from docx.shared import Pt

from docx.enum.text import WD_PARAGRAPH_ALIGNMENT

from docx.shared import Cm #厘米

from docx.shared import RGBColor

from docx.enum.table import WD_TABLE_ALIGNMENT #表格排列方式

from docx.enum.table import WD_ALIGN_VERTICAL

from docx.enum.text import WD_ALIGN_PARAGRAPH

from natsort import natsorted #自然排序

from docx.oxml import OxmlElement

from docx.oxml.ns import qn

def set_cell_border(cell, **kwargs):

"""

Set cell`s border

Usage:

set_cell_border(

cell,

top={"sz": 12, "val": "single", "color": "#FF0000", "space": "0"},

bottom={"sz": 12, "color": "#00FF00", "val": "single"},

left={"sz": 24, "val": "dashed", "shadow": "true"},

right={"sz": 12, "val": "dashed"},

)

"""

tc = cell._tc

tcPr = tc.get_or_add_tcPr()

# check for tag existnace, if none found, then create one

tcBorders = tcPr.first_child_found_in("w:tcBorders")

if tcBorders is None:

tcBorders = OxmlElement('w:tcBorders')

tcPr.append(tcBorders)

# list over all available tags

for edge in ('left', 'top', 'right', 'bottom', 'insideH', 'insideV'):

edge_data = kwargs.get(edge)

if edge_data:

tag = 'w:{}'.format(edge)

# check for tag existnace, if none found, then create one

element = tcBorders.find(qn(tag))

if element is None:

element = OxmlElement(tag)

tcBorders.append(element)

# looks like order of attributes is important

for key in ["sz", "val", "color", "space", "shadow"]:

if key in edge_data:

element.set(qn('w:{}'.format(key)), str(edge_data[key]))

path=input('输入整理路径: ')

jpglists=[]

if path=="":

path=os.getcwd()

#print(path)

dirlists=[]

for file in os.listdir(path):

#print(file)

filename=os.path.join(path,file)

#print(filename)

if os.path.isdir(filename):

dirlists.append(filename)

print(dirlists)

# exit()

for dirlist in dirlists:

if not os.path.exists(dirlist+".doc"):

print("目录存在,无对应文件{} 存在,忽略....".format(dirlist+".doc"))

pass

else:

path1=os.path.join(path,dirlist)

print('path1-----',path1)

# exit()

jpglists=[]

for file in os.listdir(path1):

#print(file)

if os.path.splitext(file)[1].lower() in '.emf|.wmf|.jpg|.jpeg|.jfif|.jpe|.png|.bmp|.dib|.rle|.gif|.emz|.wmz|.tiff|.tiff|.svg|.ico':

jpglists.append(file)

jpglists=natsorted(jpglists)

#print(jpglists)

document = Document(dirlist+".doc")

rownums=int(len(jpglists)-1)

# if len(jpglists)%2==0:

# else:

# rownums=int(len(jpglists)/2+1)

#print('设表格行',rownums)

if len(jpglists)!=0:

#插入表格 表格是从1,0开始,第一行就是1,列是从0开始,NND

table = document.add_table(rows=rownums,cols=2,style ='Table Grid')

for rownum in range(rownums):

table.rows[rownum].height=Cm(7)

cell=table.cell(rownum,0)

# if rownum==0:

# cell.add_paragraph('附***照片\n')

p=cell.paragraphs[-1]

cell.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER #水平 居中

cell.vertical_alignment = WD_ALIGN_VERTICAL.CENTER #竖直居中

if rownum==0:

jpghid=19.3

elif rownum==2:

jpghid=9.2

else:

jpghid=12.2

run = p.add_run()

run.add_picture(os.path.join(path1,jpglists[int(rownum)]), height=Cm(jpghid)) # ,width=Cm(jpgwid))

#print('写入',str(rownum),"0",os.path.join(path1,jpglists[int((rownum)*2)]))

set_cell_border(cell,

top={"sz": 12 ,"val": "single", "color": "FFFFFF", "space": "0"},

bottom={"sz":12, "color": "FFFFFF", "val": "single"},

left={"sz":12 , "val": "dashed", "color": "FFFFFF","shadow": "true"},

right={"sz": 10, "color": "FFFFFF","val": "dashed"},

insideH={"color": "FFFFFF"},)

if rownum==0:

cell.add_paragraph('钻孔柱状图')

cell.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

if rownum==1:

cell.add_paragraph('经纬度截图')

cell.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

if rownum==2:

cell.add_paragraph('照片1 现场照片')

cell.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

# jpghid=9.2

if rownum==rownums-1:

cell=table.cell(rownum,1)

p=cell.paragraphs[0]

cell.paragraphs[0].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER #水平 居中

cell.vertical_alignment = WD_ALIGN_VERTICAL.CENTER #竖直居中

run = p.add_run()

run.add_picture(os.path.join(path1,jpglists[int((rownum)+1)]), height=Cm(jpghid)) #width=Cm(7.02)) # ,height=Cm(3.95))

#print('写入',str(rownum),"1",os.path.join(path1,jpglists[int((rownum)*2)+1]))

cell.add_paragraph('照片2 现场照片')

cell.paragraphs[-1].alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

set_cell_border(cell,

top={"sz": 12 ,"val": "single", "color": "FFFFFF", "space": "0"},

bottom={"sz":12, "color": "FFFFFF", "val": "single"},

left={"sz":12 , "val": "dashed", "color": "FFFFFF","shadow": "true"},

right={"sz": 10, "color": "FFFFFF","val": "dashed"},

insideH={"color": "FFFFFF"},)

else:

cell.merge(table.cell(rownum,1)) # 合并单元格

# table.cell(r,c).vertical_alignment = WD_ALIGN_VERTICAL.CENTER

# WD_ALIGN_VERTICAL.TOP|WD_ALIGN_VERTICAL.BOTTOM

#table.cells0[0].add_paragraph('院(系)\n')

#p = document.add_paragraph('This is paragraph')

#合并表格

#table.cell(0,0).merge(table.cell(2,2))

#table.cols[0].weight=Cm(7.51)

# for jpglist in jpglists:

# document.add_picture(os.path.join(path1,jpglist), width=Inches(6)) #插入图片

document.save(dirlist+".docx") #保存文档

print('保存文件 {} ........'.format(dirlist+".docx"))

print()

input('按任意键退出。。。。。。。')

python-docx表格设置实线_python docx加入表格 在表格中加图,设置框线相关推荐

  1. aspose Word for java 表格中插入斜下框线、斜上框线

    aspose Word for java 表格中插入斜下框线.斜上框线 //添加斜线 //拿到表格 Table table = (Table) allTables.get(3); //获取具体的表格中 ...

  2. python docx 设置字体_python docx 中文字体设置的操作方法

    这篇文章主要介绍了关于python docx 中文字体设置的操作方法,有着一定的参考价值,现在分享给大家,有需要的朋友可以参考一下 最近用到了docx生成word文档,docx本身用起来很方便,自带的 ...

  3. python选定区域设置边框_Python教程:巧用openpyxl为指定区域设置边框为粗匣框线...

    有些小伙伴做表格追求版面美观,有的是喜欢表格层次分明,那就一定不能错过今天的巧用openpyxl设置边框为粗匣框线教学,集美观与层次分明一体的实用教学. 举个简单的例子,就是这样: 思路:openpy ...

  4. python合并单元格 索引_python笔记:纵向合并表格

    工作中我们常常遇到多个格式相同的表格面要合并成一个表格,比如将2019年的数据与2020年的数据合并在一起来进行分析.平时EXCEL时我们通常直接复制粘贴,但有时因表格内容过大,直接粘贴效率低,且当字 ...

  5. batchsize设置为2的n次方_试卷中的页码设置

    高中的教学中,教师难免是需要出卷子的.在学校的试卷印刷中,常见的是8K模板,一般情况下都会被分成2栏,如下图所示: 在试卷下方有时候需要添加页码,如果对页码要求不是很高,只要在页面居中插入一个页码的话 ...

  6. Winform中设置DevExpress的RadioGroup的items从配置文件中加载

    场景 DevExpress的RadioGroup的items选项如果是不确定的话,需要其从配置文件中加载. 实现 在项目目录下新建Config文件夹,文件夹下新建xml配置文件. <?xml v ...

  7. python合并word表格单元格_python docx模块读取word表格遇到合并单元格时的处理

    python docx模块读取word表格遇到合并单元格时的处理 python docx模块读取word表格遇到合并单元格时的处理 python docx模块读取word表 遇到合并单元格时的处理 通 ...

  8. python获取二进制bit位_Python读字节某一位的值,设置某一位的值,二进制位操作...

    Python读字节某一位的值,设置某一位的值,二进制位操作 在物联网实际应用项目开发中,为了提升性能,与设备端配合,往往最终使用的是二进制字节串方式进行的通信协议封装,更会把0和1.True和Fals ...

  9. python中init外面的属性_Python mock属性在\uyu init中定义和设置__

    我正在尝试为一个应用程序编写一些单元测试,我使用python mock.我对其他嘲弄库很熟悉,直到现在也没遇到过什么麻烦.我试图模拟对父类的init块中的属性集的链式调用.以下是我需要的示例:clas ...

最新文章

  1. Android之AsyncTask源码解析
  2. 转图片_图片怎么转PDF?图片转PDF就用这些方法吧!
  3. Microsoft SQL Server 2005数据库安装
  4. 各个页面样子的实现与演示
  5. python编写系统遇到的问题_python编写时遇到的小问题
  6. MySql 存储大量长字节 Text报错处理办法
  7. 计算机分屏解决方案,整套解决方案:使窗口布局更高效,为Windows平台编译分屏工具...
  8. [带权并查集] Jzoj P1503 体育场
  9. 【数据结构】线段树(interval tree)
  10. c语言编程 通讯录排序,C语言实现一个通讯录
  11. plc原理及应用_一年只一次,百篇电工+PLC技术资料大合集,不看真的亏!
  12. 测试人员如何分析需求文档
  13. 酒店BI成就创业者管理升级
  14. Android中URI
  15. 盛大进军语音识别领域:将开源哼唱搜索技术
  16. Operator 基础原理和概念
  17. 大数据人工智能时代的核心思维——数学建模
  18. It seems that scikit-learn has not been built correctly
  19. Pod 污点和容忍度
  20. 【C++】STL简介(了解)

热门文章

  1. Jeff Dean回顾谷歌2021
  2. TensorFlow基础剖析
  3. Android中Service生命周期、启动、绑定、混合使用
  4. 腾讯云https认证
  5. 【网站汇总】论文相关
  6. [JAVA EE] 拦截器
  7. 怎样往csdn上传自己的demo
  8. ES6 Proxy 性能之我见
  9. HBuilder简单入门
  10. centos7+ansible自动化工具使用