1. ホーム
  2. スクリプト・コラム
  3. パイソン

PythonはWordの読み書きの変更操作を実装している

2022-01-02 20:41:53

docxモジュールでWordを読む

docxのインストール

cmdで、次のように入力します。 org.apache.axis2.AxisFault: com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs.  at [row,col {unknown-source}]: [1,150411]         at org.apache.axis2.AxisFault.makeFault(AxisFault.java:430)         at cn.golaxy.yqpt.client.stub.BoardServiceStub.fromOM(BoardServiceStub.java:22704)         at cn.golaxy.yqpt.client.stub.BoardServiceStub.getDocList (BoardServiceStub.java:222)         at cn.golaxy.yqpt.client.thread.GatherThread.run (GatherThread.java:75) Caused by: org.apache.axiom.om.impl.exception.OMStreamingException: com.ctc.wstx.exc.WstxParsingException: Illegal processing instruction target ("xml"); xml (case insensitive) is reserved by the specs. をインストールします。 [ERROR] Invalid white space character (0xc) in text to output AxisFault: Invalid white space character (0xc) in text to output Caused by: com.ctc.wstx.exc.WstxIOException: Invalid white space character (0xc) in text to output モジュール

docx共通機能

白紙文書の作成

from docx import Document

document = Document()
document.save("word.docx") # Generate blank word
print(document)


ドキュメントを読む

from docx import Document
document = Document("word.docx") # read existing word to create a document object


文書の段落を取得する

from docx import Document

document = Document("word.docx") # read the existing word to create a document object
all_paragraphs = document.paragraphs
print(type(all_paragraphs))
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format) # print out the style name of each paragraph in the word
    # print the text of each paragraph
    print(paragraph.text)
    # Loop through the run contents of each paragraph
# A run object is a continuation of the same style of text
for paragraph in all_paragraphs:
    for run in paragraph.runs:
        print(run.text) # Print the contents of run



ワードアジャストスタイル

from docx import Document
from docx.shared import Pt, RGBColor

document = Document() # read the existing word to create a document object

# two, write the content
# paragraph
p1 = document.add_paragraph("Early to bed and early to rise!!! ")
format_p1 = p1.paragraph_format
# indent left and right
format_p1.left_indent = Pt(20)
format_p1.right_indent = Pt(20)
# First line indent
format_p1.first_line_indent = Pt(20)
# Line spacing
format_p1.line_spacing = 1
# Append
# A run object is a continuation of the same style of text
run = p1.add_run("I want to do dog licking too \n")
# font, font size, text color
run.font.size = Pt(12)
run.font.name = "Microsoft elegant black"
run.font.color.rgb = RGBColor(235, 123, 10)
run1 = p1.add_run("Jia someone does not study")
# bold, underline, italic
run1.bold = True
run1.font.underline = True
run1.font.italic = True
# Three, save the file
document.save("word.docx")

all_paragraphs = document.paragraphs
# print(type(all_paragraphs))
# <class 'list'>, print and find it is a list
# If it's a list, start a loop to read d
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format) # print out the name of the style of each paragraph in the word
    # print the text of each paragraph
    print(paragraph.text)
    # Loop through the run content of each paragraph
    # for run in paragraph.runs:
    # print(run.text) # Print the contents of run


ワード書き込み操作

from docx import Document
from docx.shared import Pt, RGBColor

document = Document() # read the existing word to create a document object

# two, write the content
document.add_heading("python operation Word")
# paragraph
p1 = document.add_paragraph("Early to bed, early to rise!!! ")
p1.insert_paragraph_before("Power!!! ")
format_p1 = p1.paragraph_format
# Indent left and right
format_p1.left_indent = Pt(20)
format_p1.right_indent = Pt(20)
# First line indent
format_p1.first_line_indent = Pt(20)
# Line spacing
format_p1.line_spacing = 1
# Append
# A run object is a continuation of the same style of text

run = p1.add_run("I want to do dog licking too \n")
# font, font size, text color
run.font.size = Pt(12)
run.font.name = "Microsoft elegant black"
run.font.color.rgb = RGBColor(235, 123, 10)
run1 = p1.add_run("Jia someone does not study")
# bold, underline, italic
run1.bold = True
run1.font.underline = True
run1.font.italic = True
# Three, save the file
document.save("word.docx")

all_paragraphs = document.paragraphs
# print(type(all_paragraphs))
# <class 'list'>, print and find it is a list
# If it's a list, start a loop to read d
for paragraph in all_paragraphs:
    # print(paragraph.paragraph_format) # print out the name of the style of each paragraph in the word
    # print the text of each paragraph
    print(paragraph.text)
    # Loop through the run content of each paragraph
    # for run in paragraph.runs:
    # print(run.text) # Print the contents of run



Wordの読み書きの変更操作をPythonで実装してみたという記事は以上です。Python関連のコンテンツは、Script Houseの過去記事を検索するか、以下の関連記事を引き続きご覧ください。