Python-docx對EXCEL、Word的操做

Python-docx

安裝庫:pip install python-docxhtml

幫助文檔:https://python-docx.readthedocs.io/en/latest/index.htmlpython

例子:

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

 

東湖日報腳本源碼

# -*- coding:utf-8 -*-  
# ====#====#====#====#====#====#====#====#====
# @Time    : 2020/4/2 10:58
# @Author  : Alex_Dong
# @Email   : 1220274707@qq.com
# @HomePage:https://www.cnblogs.com/xied/  
# @File    : 東湖日報腳本.py
# @Software: PyCharm
# ====#====#====#====#====#====#====#====#====

from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH,WD_LINE_SPACING
from docx.shared import Pt,Inches
from docx import section
from docx.enum.section import WD_SECTION,WD_ORIENT
from docx.shared import RGBColor
import time
import docx

from docx.oxml.ns import qn

# 實例化一個document對象
donghu_document = Document()

# 調取sections對象
sections = donghu_document.sections
for section in sections:
    pass
# 方法二  對下一也新增
# section = donghu_document.add_section()


# 對屬性的修改
#方法容許在文檔末尾啓動一個新節。調用此方法後添加的段落和表將出如今新的部分:(增長一頁)
# current_section = donghu_document.sections[-1]  # last section in document
# new_section = donghu_document.add_section(WD_SECTION.ODD_PAGE)

# 長寬高
section.orientation = WD_ORIENT.LANDSCAPE
# 尺寸像素點
# section.page_width = 15544800
# section.page_hwidth = 15544800

section.page_width = Inches(16)
section.page_height = Inches(12)

# 設置分欄爲2

section._sectPr.xpath('./w:cols')[0].set(qn('w:num'),'2')


#設置頁邊距
section.left_margin = Inches(0.5)
section.right_margin = Inches(0.5)
section.top_margin= Inches(0.5)
section.bottom_margin= Inches(0.5)


# 時間模塊
Data_now1 = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
Data_now = time.strftime('%Y-%m-%d', time.localtime(time.time()))
print('如今時間:',Data_now1)



# 大標題一
head = donghu_document.add_heading(level=0)#添加一級標題
head_run = head.add_run('東湖水環境提高工程設計、施工總承包(EPC)\n')
head_run = head.add_run('工  程  日  報\n')
head_run = head.add_run(Data_now1)
head_run.font.size = Pt(24)
head.alignment = WD_ALIGN_PARAGRAPH.CENTER#居中

# 段落一
p = donghu_document.add_paragraph('1、本日工做進展')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True

#插入圖片
# 添加圖片,設置圖片大小
donghu_document.add_picture(r".\pic\mabu1.jpg", height=Inches(2.25))
donghu_document.add_picture(r'.\pic\mabu2.jpg',height=Inches(2.25))   #  width=Inches(2.25)--2.25英寸

# 換頁和換行--若是是換行的話就不須要使用docx.enum.text.WD_BREAK.PAGE參數
# donghu_document.paragraphs[0].runs[0].add_break(docx.enum.text.WD_BREAK.PAGE)


# 段落二
p = donghu_document.add_paragraph('\n\n\n\n\n\n\n\n')
p = donghu_document.add_paragraph('2、本日完成狀況對比:')
p = donghu_document.add_paragraph('\n\n\n\n\n\n\n\n')

p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True


# 段落三
p = donghu_document.add_paragraph('3、明日施工計劃')
p.add_run('bold').bold = True
p.add_run(' and some ')
p.add_run('italic.').italic = True


# A添加表格,填入表格內容
table = donghu_document.add_table(rows=2, cols=2)
table.cell(0, 0).text = "cell_00"
table.cell(0, 1).text = "cell_01"
table.cell(1, 0).text = "cell_10"
table.cell(1, 1).text = "cell_11"

# B##############################建立表格##########
table = donghu_document.add_table(rows=1, cols=9, style='Table Grid')  # 建立帶邊框的表格
hdr_cells = table.rows[0].cells  # 獲取第0行全部全部單元格
hdr_cells[0].text = '序號'
hdr_cells[1].text = '部位名稱'
hdr_cells[2].text = '單位'
hdr_cells[3].text = '設計總量'
hdr_cells[4].text = '本日計劃'
hdr_cells[5].text = '本日完成'
hdr_cells[6].text = '累計完成比例'
hdr_cells[7].text = '累計完成'
hdr_cells[8].text = '累計總佔比'


# 添加三行數據
data_lines = 5
for i in range(data_lines):
    cells = table.add_row().cells
    cells[0].text = '這是第%s行' % i           #序號
    cells[1].text = '部位數據%s' % i           #部位
    cells[2].text = '單位%s' % i           #單位
    cells[4].text = '單位%s' % i           #'本日計劃'
    cells[5].text = '單位%s' % i           #'本日完成'
    cells[6].text = '單位%s' % i           #'累計完成比例'
    cells[7].text = '單位%s' % i           #'累計完成'
    cells[8].text = '單位%s' % i           #'累計總佔比'


#插入無框表格
p = donghu_document.add_paragraph('\n\n<這是無框表格>')

rows = 2
cols = 4
table = donghu_document.add_table(rows=rows, cols=cols)
val = 1
for i in range(rows):
    cells = table.rows[i].cells
    for j in range(cols):
        cells[j].text = str(val * 10)
        val += 1


# 最後簽名
p = donghu_document.add_paragraph('\n\n\n\n\n\n\n\n編制:馬梅                                                  審覈:舒飛超')



donghu_fileName = '【%s】東湖水環境提高日報'%Data_now+'.docx'
donghu_fileName = str(donghu_fileName)
print(donghu_fileName,'已經成功生成!!!')

#存儲文檔
donghu_document.save('.\word\%s' %donghu_fileName)
View Code

 

 

一.簡單的打開保存

from docx import Documentapi

document = Document('existing-document-file.docx')  #能夠docx=Noneapp

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

打開佈局

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

document = Document(f)this

f.close()url

 

二.API Documentation文檔對象

python-docx的API旨在使簡單的事情變得簡單,同時容許經過適當的、增量的理解來實現更復雜的結果。只使用一個對象docx.api就能夠建立一個基本文檔。打開文件時返回的文檔對象。docx.api上的方法。文檔容許將塊級對象添加到文檔的末尾。塊級對象包括段落、內聯圖片和表。標題、項目符號和編號列表只是應用了特定樣式的段落。這樣一個文件。spa

三.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

    • python給word添加換行換頁符

      • #換頁只須要把docx.enum.text.WD_BREAK.PAGE做爲惟一的參數傳遞給add_break
      • #若是是換行的話就不須要使用docx.enum.text.WD_BREAK.PAGE參數
      • doc2.paragraphs[0].runs[0].add_break(docx.enum.text.WD_BREAK.PAGE)

 

四.Styles

Questions:

    • What is a style in Word?

    • Why doesn’t the style I applied show up?

  • Access a style

 The Styles object is also iterable. By using the identification properties on BaseStyle, various subsets of the defined styles can be generated. For example, this code will produce a list of the defined paragraph styles:Styles對象也是可迭代的。經過使用BaseStyle上的標識屬性,能夠生成定義的樣式的各類子集。例如,這段代碼將生成定義的段落樣式列表:

>>> from docx.enum.style import WD_STYLE_TYPE
>>> styles = document.styles
>>> paragraph_styles = [
...     s for s in styles if s.type == WD_STYLE_TYPE.PARAGRAPH
... ]
>>> for style in paragraph_styles:
...     print(style.name)
...
Normal
Body Text
List Bullet
View Code
  • Apply a style應用一個樣式

>>> document = Document()
>>> paragraph = document.add_paragraph()
>>> paragraph.style
<docx.styles.style._ParagraphStyle object at <0x11a7c4c50>
>>> paragraph.style.name
'Normal'
>>> paragraph.style = document.styles['Heading 1']
>>> paragraph.style.name
'Heading 1'
View Code
  • Add or delete a style

#經過指定一個惟一的名稱和樣式類型,能夠將一個新樣式添加到文檔中:
>>> from docx.enum.style import WD_STYLE_TYPE
>>> styles = document.styles
>>> style = styles.add_style('Citation', WD_STYLE_TYPE.PARAGRAPH)
>>> style.name
'Citation'
>>> style.type
PARAGRAPH (1)

#使用base_style屬性指定新樣式應該繼承的格式設置:
>>> style.base_style
None
>>> style.base_style = styles['Normal']
>>> style.base_style
<docx.styles.style._ParagraphStyle object at 0x10a7a9550>
>>> style.base_style.name
'Normal'

#一個樣式能夠從文檔中刪除,只需調用它的delete()方法:
>>> styles = document.styles
>>> len(styles)
10
>>> styles['Citation'].delete()
>>> len(styles)
9
相關文章
相關標籤/搜索