小白一个,大部分代码还是在网上学习,自己只是做了一些集合和整理从而实现功能的使用,旨在学习过程中留下学习笔记。仅供参考,有什么更好的建议和意见,欢迎大佬指出!
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 22 16:05:39 2022@author: Bruce
"""
from docx import Document
from docx.shared import Cm
from docx.shared import Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn #中文格式
import os
rows_n=144 #偶数
clos_n=2 #输入需要的表格大小
picture_path='D:/1project/ABAQUS/'
os.chdir('D:/1project/ABAQUS')#工作目录
pic_name='GJL_bogie_pure_M'
file_name='GJL_bogie.docx'
doc=Document() #创建一个新的word
doc.styles['Normal'].font.name=u'Times New Roman' #设置文档基础字体(all)
doc.styles['Normal']. element.rPr.rFonts.set(qn('w:eastAsia'),u'黑体')
sec=doc.sections[0]table1=doc.add_table(rows=rows_n,cols=clos_n)
table1.style='Normal Table'
count=1 ; control=1
sec.left_margin=Cm(1.2)
sec.right_margin=Cm(1.2)
for row in range(rows_n):cells=table1.rows[row].cellsif row % 2 ==0:for col in range(clos_n):ph=cells[col].paragraphs[0] #表格本身有一个paragraph,引用即可;添加pra:add_paragraph()ph.paragraph_format.alignment=WD_ALIGN_PARAGRAPH.LEFTrun=ph.add_run()try:run.add_picture(picture_path+pic_name+f'{count}.png',width=Cm(10))count+=1except FileNotFoundError:breakelse:for col in range(clos_n):ph=cells[col].paragraphs[0]ph.paragraph_format.alignment=WD_ALIGN_PARAGRAPH.CENTERrun=ph.add_run(f'第{control}阶模态及振型')run.font.name='Times New Roman'run.font.size=Pt(10.5)control+=1
# run=table1.cell(2,1).add_paragraph('Add_one',style='List Bullet')
# run=table1.cell(2,1).paragraphs[1].add_run()
# pic=run.add_picture('D:/1project/ABAQUS/S3.png')
# pic.height=Cm(2)
# pic.width=Cm(2)doc.save(file_name)