Python-docx对EXCEL、Word的操作

Python-docx

安装库:pip install python-docx

例子:

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')
View Code

1.简单的打开保存

from docx import Document

document = Document('existing-document-file.docx')  #可以docx=None

document.save('new-file-name.docx')

打开

f = open('foobar.docx', 'rb')

document = Document(f)

f.close()

2.API Documentation文档对象

python-docx的API旨在使简单的事情变得简单,同时允许通过适当的、增量的理解来实现更复杂的结果。只使用一个对象docx.api就可以创建一个基本文档。打开文件时返回的文档对象。docx.api上的方法。文档允许将块级对象添加到文档的末尾。块级对象包括段落、内联图片和表。标题、项目符号和编号列表只是应用了特定样式的段落。这样一个文件。

3.Working with Sections

Word支持section的概念,即文档的一个部分,具有相同的页面布局设置,如页边距和页面方向。例如,这就是文档在纵向布局中包含某些页面而在横向布局中包含其他页面的方式。大多数Word文档只有一个默认的部分,而且大多数文档没有理由改变默认的页边距或其他页面布局。但是,当您确实需要更改页面布局时,您需要了解各个部分才能完成。

1.获取sections对象

     sections = document.sections        #(from docx import section)

  2.添加新的部分

     add_section()方法允许在文档末尾启动一个新节。调用此方法后添加的段落和表将出现在新的部分:
current_section = document.sections[-1]  # last section in document
new_section = document.add_section(WD_SECTION.ODD_PAGE)
new_section.start_type
 
 

3.Section properties属性

Section对象有11个属性,允许发现和指定页面布局设置。
  • Section start type部分启动类型

    • section.start_type起始类型new_page(2),然后  section.start_type = WD_SECTION.ODD_PAGE
    • ODD_PAGE (4)>>>>>>>start_type的值是WD_SECTION_START枚举的成员。
  • Page dimensions and orientation页面尺寸和方向

    • Section中的三个属性描述了页面的大小和方向。这些可以一起使用,例如,改变一个截面的方向从纵向到横向:
    • 用到了:section.orientation,  section.page_width,  section.page_height
    • >>> section.orientation, section.page_width, section.page_height
      (PORTRAIT (0), 7772400, 10058400)  # (Inches(8.5), Inches(11))
      >>> new_width, new_height = section.page_height, section.page_width
      >>> section.orientation = WD_ORIENT.LANDSCAPE
      >>> section.page_width = new_width
      >>> section.page_height = new_height
      >>> section.orientation, section.page_width, section.page_height
      (LANDSCAPE (1), 10058400, 7772400)
      View Code
  • Page margins页边距

   Section上的七个属性一起指定了决定文本在页面上出现位置的各种边缘间距:

#section.left_margin, section.right_margin
#section.top_margin, section.bottom_margin
#section.gutter
#section.header_distance, section.footer_distance

1 from docx.shared import Inches
2 #section.left_margin, section.right_margin
3 #section.top_margin, section.bottom_margin
4 #section.gutter
5 #section.header_distance, section.footer_distance
6 
7 section.left_margin = Inches(1.5)
8 section.right_margin = Inches(1)
9 section.left_margin, section.right_margin
  • Working with Headers and Footers  处理页眉和页脚

Word支持页眉和页脚。页眉是出现在每页顶部空白区域的文本,与正文分离,通常传递上下文信息,如文档标题、作者、创建日期或页码。文档中的页眉在页与页之间是相同的,只有内容上的细微差异,比如更改了节标题或页码。页眉也称为运行头。页脚在任何方面都类似于页眉,但它出现在页面的底部。它不应该被混淆

    • Accessing the header for a section访问节的标头

      • 每个section对象都有一个.header属性,为该section提供对_Header对象的访问:
    • Adding a header (simple case)添加标题(简单的情况)

      •  paragraph = header.paragraphs[0]
      •  paragraph.text = "Title of my document"
    • Adding “zoned” header content添加“分区”标题内容

带有多个“区域”的页眉通常是使用精心放置的制表符来完成的。中心对齐和右对齐的“区域”所需的制表符停止是Word中页眉和页脚样式的一部分。如果您使用的是自定义模板而不是python-docx默认模板,那么在模板中定义该样式可能是有意义的。插入的制表符(“\t”)用于分隔左、中、右对齐的标题内容:

      • >>> paragraph = header.paragraphs[0]
      • >>> paragraph.text = "Left Text\tCenter Text\tRight Text"
      • >>> paragraph.style = document.styles["Header"]
    • Removing a header

      • header.is_linked_to_previous = True

    •  

猜你喜欢

转载自www.cnblogs.com/xied/p/12619571.html