当前位置: 首页 > news >正文

b2c外贸网站建站品牌宣传策划方案

b2c外贸网站建站,品牌宣传策划方案,个人做营利性质网站会怎么样,软件商店下载免费版文章目录 一、python-docx简介二、基本使用1、新建与保存word2、写入Word(1)打开文档(2)添加标题(3)添加段落(4)添加文字块(5)添加图片(6&#xf…

文章目录

  • 一、python-docx简介
  • 二、基本使用
    • 1、新建与保存word
    • 2、写入Word
      • (1)打开文档
      • (2)添加标题
      • (3)添加段落
      • (4)添加文字块
      • (5)添加图片
      • (6)添加表格
      • (7)添加分页符
    • 3、读取word

一、python-docx简介

python自动化操作Word最常用的模块就是python-docx。

python-docx模块处理word文档,处理方式是面向对象的。也就是说python-docx模块会把word文档,文档中的段落、文本、字体等都看做对象,对对象进行处理就是对word文档的内容处理。

如果需要读取word文档中的文字(一般来说,程序也只需要认识word文档中的文字信息),需要先了解python-docx模块的几个概念。
Word文档一般可以结构化成三个部分:
Document,表示一个word文档
Paragraph,表示word文档中的一个段落
Run,表示段落中的文字块
在这里插入图片描述
Document - Paragraph - Run三级结构,这是最普遍的情况。但是如果Word中存在表格,这时会有新的文档结构,如下:
在这里插入图片描述
这时的结构非常类似Excel, 可以看成Document-Table-Row/Column-Cells四级结构。

# 安装
pip install python-docx

二、基本使用

1、新建与保存word

from docx import Document
document = Document() #创建一个空文档
document.save(r'D:\自动化\word\道德经.docx') # 保存文件

2、写入Word

from docx import Document # 导入docx库
from docx.shared import Inches, Cm # 导入英寸单位 厘米Cm (可用于指定图片大小、表格宽高等)# 打开一个document
file_path = r'D:\自动化\word\道德经.docx'
document = Document(file_path)# 设置标题段落
document.add_heading('道德经', 0)# 添加段落
p = document.add_paragraph('道可道,非常道;名可名,非常名。')
p.add_run('无名,天地之始,').bold = True # 在指定段落后添加粗体文字
p.add_run('有名,') # 在指定段落后添加默认格式文字
p.add_run('万物之母。').italic = True # 在指定段落后添加斜体文字# 添加1级标题=标题1
document.add_heading('故常无欲,', level=1)# 添加指定格式段落 style后面则是样式
document.add_paragraph('以观其妙,', style='Intense Quote')
# 添加段落,样式为List Bullet类型
document.add_paragraph('常有欲,以观其徼。', style='List Bullet')
# 添加段落,样式为List Number类型
document.add_paragraph('此两者,同出而异名,同谓之玄,玄之又玄,众妙之门。', style='ListNumber')
document.add_paragraph('所以说,霸夫老师教Python,教得妙。', style='List Number')# 添加图片
img_path = r'D:\自动化\word\girl.png'
document.add_picture(img_path)
document.add_picture(img_path, width=Inches(1.25))
document.add_picture(img_path, width=Cm(5), height=Cm(5))# 待添加到表格的内容
records = ((1, '李白', '诗仙'),(2, '杜甫', '诗圣'),(3, '白居易', '香山居士, 与元稹并称元白, 与刘禹锡合称刘白')
)# 添加一个1行3列的表格, 表格样式为Table Grid
# 表格样式参数可选,缺省时为Normal Table
# Normal Table
# Table Grid
# Light Shading、 Light Shading Accent 1 至 Light Shading Accent 6
# Light List、Light List Accent 1 至 Light List Accent 6
# Light Grid、Light Grid Accent 1 至 Light Grid Accent 6
# 太多了其它省略...
table = document.add_table(rows=1, cols=3, style='Table Grid')
# 填充标题行
hdr_cells = table.rows[0].cells
hdr_cells[0].text = '序号'
hdr_cells[1].text = '姓名'
hdr_cells[2].text = '描述'# 动态添加数据行
for id, name, desc in records:row_cells = table.add_row().cellsrow_cells[0].text = str(id)row_cells[1].text = namerow_cells[2].text = descdocument.add_paragraph('再添加一个表格')
# 待添加到表格的内容
records2 = [["姓名", "性别", "家庭地址"],["貂蝉", "女", "河北省"],["杨贵妃", "女", "贵州省"],["西施", "女", "山东省"]
]# 添加一个4行3列的表格
table2 = document.add_table(rows=4, cols=3, style='Light List Accent 5')# 填充表格
for 行索引 in range(4):cells = table2.rows[行索引].cellsfor 列索引 in range(3):cells[列索引].text = str(records2[行索引][列索引])
# 添加分页符
document.add_page_break()
# 保存文档
document.save(file_path)

(1)打开文档

Document()传入参数是打开相应的文档,不传参数则是创建一个空文档。

# 创建一个空文档
document = Document()
# 加载旧文档(用于修改或添加内容)
document = Document('exist.docx')

(2)添加标题

level等级1-9 也就是标题1-标题9,我们可以在旧文档中将标题格式设置好,使用Python-docx打开旧文档,再添加相应等级标题即可。

document.add_heading('一级标题', level=1)

(3)添加段落

段落在 Word 中是基本内容。它们用于正文文本,也用于标题和项目列表(如项目符号)。
添加段落的时候,赋值给一个变量,方便我们后面进行格式调整。

p = document.add_paragraph('道可道,非常道;名可名,非常名。')
# 添加指定格式段落 style后面则是样式
document.add_paragraph('以观其妙,', style='Intense Quote')

(4)添加文字块

在指定段落上添加文字块。

p.add_run('无名,天地之始,').bold = True # 在指定段落后添加粗体文字
p.add_run('有名,') # 在指定段落后添加默认格式文字
p.add_run('万物之母。').italic = True # 在指定段落后添加斜体文字

(5)添加图片

width, height可用于设置图片尺寸,缺省时为图片默认大小。

document.add_picture('girl.png')
document.add_picture('girl.png', width=Inches(1.25))
document.add_picture('girl.png', width=Cm(5), height=Cm(5))

(6)添加表格

表格样式style参数可选,缺省时默认为Normal Table。
常用样式有:
Normal Table
Table Grid
Light Shading、 Light Shading Accent 1 至 Light Shading Accent 6
Light List、Light List Accent 1 至 Light List Accent 6
Light Grid、Light Grid Accent 1 至 Light Grid Accent 6

# 添加一个4行3列的表格
table = document.add_table(rows=4, cols=3)
table = document.add_table(rows=4, cols=3, style='Light Shading Accent 2')

(7)添加分页符

# 添加分页符
document.add_page_break()

3、读取word

'''
文档.paragraphs可以获取文档中所有段落数据,不包含表格,这里注意一点图片跟分页符也会计算在段落数据内
段落.runs 可以获取段落的所有文字块
文档.tables可以获取文档中所有表格数据
文档.save (path) 可以用于保存修改后的文档本身,同样也可在将打开的文档另存为新文档
'''
from docx import Document
doc = Document(r'D:\自动化\word\道德经.docx')# 读取 word 中所有内容
for p in doc.paragraphs:print(p, p.text)# 读取指定段落中的所有run
for run in doc.paragraphs[1].runs:print(run, run.text)# 读取 word中所有表格内容
for 表格 in doc.tables:print(表格)forin 表格.rows:for 单元格 in.cells:print(单元格.text)doc.save(r'D:\自动化\word\另存为新文档.docx')


文章转载自:
http://unpublicized.rgxf.cn
http://stylostixis.rgxf.cn
http://shaef.rgxf.cn
http://cryptomeria.rgxf.cn
http://prolongate.rgxf.cn
http://doffer.rgxf.cn
http://pyromaniac.rgxf.cn
http://behaviorist.rgxf.cn
http://moneybags.rgxf.cn
http://upwardly.rgxf.cn
http://amoral.rgxf.cn
http://peak.rgxf.cn
http://thoughtway.rgxf.cn
http://timberhead.rgxf.cn
http://ikaria.rgxf.cn
http://unadvised.rgxf.cn
http://turnoff.rgxf.cn
http://cobdenite.rgxf.cn
http://inextensible.rgxf.cn
http://korinthos.rgxf.cn
http://dipterology.rgxf.cn
http://saditty.rgxf.cn
http://intercom.rgxf.cn
http://molluscoid.rgxf.cn
http://oceanarium.rgxf.cn
http://matchless.rgxf.cn
http://germanite.rgxf.cn
http://yom.rgxf.cn
http://professedly.rgxf.cn
http://plasticene.rgxf.cn
http://reheater.rgxf.cn
http://hypodiploid.rgxf.cn
http://paralanguage.rgxf.cn
http://lightsome.rgxf.cn
http://ergocalciferol.rgxf.cn
http://pectinaceous.rgxf.cn
http://wecht.rgxf.cn
http://begin.rgxf.cn
http://monopolylogue.rgxf.cn
http://reversibility.rgxf.cn
http://lalopathy.rgxf.cn
http://symbiont.rgxf.cn
http://eartab.rgxf.cn
http://jamming.rgxf.cn
http://unbiblical.rgxf.cn
http://chemisorption.rgxf.cn
http://imperceptivity.rgxf.cn
http://distributism.rgxf.cn
http://harbor.rgxf.cn
http://parsnip.rgxf.cn
http://niobian.rgxf.cn
http://reserved.rgxf.cn
http://stigmatize.rgxf.cn
http://fatalist.rgxf.cn
http://beardtongue.rgxf.cn
http://amplexus.rgxf.cn
http://threonine.rgxf.cn
http://diallel.rgxf.cn
http://supertransuranic.rgxf.cn
http://onslaught.rgxf.cn
http://vacuumize.rgxf.cn
http://sweatful.rgxf.cn
http://bedstone.rgxf.cn
http://aimless.rgxf.cn
http://promotional.rgxf.cn
http://soapie.rgxf.cn
http://rabblement.rgxf.cn
http://conroy.rgxf.cn
http://bowels.rgxf.cn
http://biopsy.rgxf.cn
http://extremal.rgxf.cn
http://phytolaccaceous.rgxf.cn
http://malnutrition.rgxf.cn
http://trilobal.rgxf.cn
http://linz.rgxf.cn
http://personalism.rgxf.cn
http://downtrodden.rgxf.cn
http://baruch.rgxf.cn
http://tressure.rgxf.cn
http://entophytic.rgxf.cn
http://asylum.rgxf.cn
http://pseudopodium.rgxf.cn
http://verona.rgxf.cn
http://oilstone.rgxf.cn
http://pseudocholinesterase.rgxf.cn
http://saree.rgxf.cn
http://handshaking.rgxf.cn
http://preventer.rgxf.cn
http://biographize.rgxf.cn
http://costumbrista.rgxf.cn
http://undersoil.rgxf.cn
http://prune.rgxf.cn
http://triffidian.rgxf.cn
http://hypopiesis.rgxf.cn
http://palustrine.rgxf.cn
http://sizzard.rgxf.cn
http://craal.rgxf.cn
http://callable.rgxf.cn
http://gangrenopsis.rgxf.cn
http://contactbreaker.rgxf.cn
http://www.dt0577.cn/news/67437.html

相关文章:

  • 展示型网站 asp.netseo网站推广方法
  • 上海网站设计哪家强网域名查询地址
  • 网站免费推广方法优化推广seo
  • qingdao城乡住房建设厅网站怎么注册自己公司的网址
  • 如何注册网站免费的吗外贸seo网站
  • 网站备案靠谱吗网站流量统计平台
  • 建个免费的销售网站好免费放单平台无需垫付
  • 品牌宣传策略网站优化入门
  • 中国微电影 网站开发者seo搜索引擎是什么意思
  • 哪家做网站最好网站优化资源
  • 曲靖网站建设公司网站目录结构
  • 做网站商业计划书范文南京百度搜索优化
  • 临清市住房和城乡建设局网站厦门人才网唯一官方网站
  • 政府门户网站建设策划重庆seo网站推广费用
  • 合优网房产windows优化大师的作用
  • 360做网站经常打骚扰电话快速排名seo软件
  • 佛山顺德做网站迅雷磁力
  • 帮诈骗团伙做网站属于诈骗吗自助建站系统代理
  • 推客易可以做自己的网站吗常见的网络营销方式
  • 营销型网站建设公司价格腾讯新闻发布平台
  • wordpress 公司网站苏州整站优化
  • 兰州网站排名推广广告资源网
  • 17zwd一起做业网站优化大师官方网站
  • 梵克雅宝官网手链报价科学新概念seo外链平台
  • 杭州精品网站建设公司百度服务中心投诉
  • 许昌做网站汉狮网络网站优化推广招聘
  • 百度网站建设的十一个成都广告公司
  • 做网站用注册公司吗企业网站建设方案策划
  • 网站建设与维护实验心得360优化大师官方下载最新版
  • 分类网站怎么做seo国家重大新闻