首页 > 编程语言 >[转] python-docx

[转] python-docx

时间:2023-01-27 20:55:26浏览次数:62  
标签:docx python text cells add document

https://python-docx.readthedocs.io/en/latest/

 

https://gitcode.net/mirrors/python-openxml/python-docx/-/tree/master/docx

 

python-docx

Release v0.8.11 (Installation)

python-docx is a Python library for creating and updating Microsoft Word (.docx) files.

What it can do

Here’s an example of what python-docx can do:

img
from docx import Document
from docx.shared import Inches

document = Document()

document.add_heading('Document Title', 0)

p = document.add_paragraph('A plain paragraph having some ')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

document.add_heading('Heading, level 1', level=1)
document.add_paragraph('Intense quote', style='Intense Quote')

document.add_paragraph(
    'first item in unordered list', style='List Bullet'
)
document.add_paragraph(
    'first item in ordered list', style='List Number'
)

document.add_picture('monty-truth.png', width=Inches(1.25))

records = (
    (3, '101', 'Spam'),
    (7, '422', 'Eggs'),
    (4, '631', 'Spam, spam, eggs, and spam')
)

table = document.add_table(rows=1, cols=3)
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'Qty'
hdr_cells[1].text = 'Id'
hdr_cells[2].text = 'Desc'
for qty, id, desc in records:
    row_cells = table.add_row().cells
    row_cells[0].text = str(qty)
    row_cells[1].text = id
    row_cells[2].text = desc

document.add_page_break()

document.save('demo.docx')

User Guide

 

标签:docx,python,text,cells,add,document
From: https://www.cnblogs.com/freeliver54/p/17069312.html

相关文章

  • [转]Python3 xlrd库基本教程
    本文转自:https://www.w3cschool.cn/python3/python3-xlrd.htmlxlrd库是一个python用于操作excel的第三方库。它的主要功能是用来读取excel。通常会与xlwt 、 xlutils组......
  • Python——02.变量
    变量概念:--变量:将数据临时存储在内存中,该内存名字就是变量,此内存为内存储,相对应的外存储为永久存储举例:手机存储量12(内存储:临时)+286G(外存储)查看内......
  • [python3.10] 引用第三方库 PyMouse、PyKeyboard
    importPyMouse前提条件,__init__.py文件中将fromwindowsimportPyMouse,PyMouseEvent 改为  frompymouse.windows importPyMouse,PyMouseEventpyHook第......
  • Python入门笔记
    Python入门笔记Nowisbetterthannever.Althoughneverisoftenbetterthanrightnow.—————TheZenofPython,byTimPeters目录Python入门笔记1.前言py......
  • 005 python 打开windows下的软件,python打开记事本 subprocess
    如果要用python代码打开windos上的记事本,代码如下:importsubprocessnotePro=subprocess.Popen('notepad.exe')print(notePro)如果要打开别的软件,则把notepad.exe......
  • Python 介绍和环境准备
    目录一、概述二、Python应用领域1)Web应用开发2)自动化运维3)网路爬虫4)人工智能领域5)科学计算三、Python环境准备1)Linux平台安装Python2)Window平台安装Python3)安装pyt......
  • Python 自定义数据库连接类
    创建一个配置文件:E:/mypy/mycnf.txt[mssql]host=HZCuser=kkpwd=kkdbname=DemoDB数据库连接类:E:/mypy/MSSQL.py#-*-coding:utf-8-*-#python3.5importsysimp......
  • Python_argparse_读取命令行参数
    argparse模块是Python内置的用于命令项选项与参数解析的模块,可以方便地读取命令行参数。参考代码如下#1.导入模块importargparsedefformat_parser():#......
  • Python入门之continue语法
    """continue语法"""#累加1-100之间的数字,能被5整除的数字#sum_value=0#foriteminrange(1,101):#满足条件则累加#ifitem%5==0:#su......
  • Python入门之for循环练习
    """for:适合执行预定次数。while:适合根据条件循环执行。"""#for变量in可迭代对象#循环体str01="我叫黎二狗!"#item存储的是字符串中每个字符串......