Hướng dẫn python work with word documents - Python làm việc với các tài liệu từ

python-docx cho phép bạn tạo các tài liệu mới cũng như thay đổi các tài liệu hiện có. Trên thực tế, nó chỉ cho phép bạn thay đổi các tài liệu hiện có; Nó chỉ là nếu bạn bắt đầu với một tài liệu không có nội dung nào, ban đầu nó có thể cảm thấy giống như bạn tạo ra một tài liệu từ đầu.

Đặc điểm này là một trong những mạnh mẽ. Rất nhiều tài liệu trông được xác định bởi các phần còn lại khi bạn xóa tất cả các nội dung. Những thứ như phong cách và tiêu đề trang và chân trang được chứa tách biệt với nội dung chính, cho phép bạn đặt nhiều tùy chỉnh trong tài liệu bắt đầu của bạn sau đó xuất hiện trong tài liệu bạn sản xuất.

Hãy cùng bước đi qua các bước để tạo một tài liệu một ví dụ tại một thời điểm, bắt đầu với hai trong số những điều chính bạn có thể làm với một tài liệu, mở nó và lưu nó.

Mở một tài liệu

Cách đơn giản nhất để bắt đầu là mở một tài liệu mới mà không cần chỉ định một tệp để mở:

from docx import Document

document = Document()
document.save('test.docx')

Điều này tạo ra một tài liệu mới từ mẫu mặc định tích hợp và lưu nó không thay đổi vào một tệp có tên ‘test.docx. Cái gọi là mẫu mặc định, thực sự chỉ là một tệp Word không có nội dung, được lưu trữ với gói python-docx được cài đặt. Nó rất giống với bạn nhận được bằng cách chọn mẫu tài liệu Word sau khi chọn tệp Word Word> Mới từ mục menu mẫu.File > New from Template… menu item.

Thực sự mở một tài liệu

Nếu bạn muốn kiểm soát nhiều hơn đối với tài liệu cuối cùng hoặc nếu bạn muốn thay đổi một tài liệu hiện có, bạn cần mở một tài liệu bằng tên tệp:

document = Document('existing-document-file.docx')
document.save('new-file-name.docx')

Những điều cần lưu ý:

  • Bạn có thể mở bất kỳ từ 2007 hoặc sau đó tệp theo cách này (các tệp .doc từ Word 2003 và trước đó đã giành được công việc). Mặc dù bạn có thể không thể thao túng tất cả các nội dung, nhưng bất cứ thứ gì đã có trong đó sẽ tải và tiết kiệm tốt. Bộ tính năng vẫn đang được xây dựng, vì vậy bạn không thể thêm hoặc thay đổi những thứ như tiêu đề hoặc chú thích, nhưng nếu tài liệu có chúng python-docx đủ lịch sự để để chúng một mình và đủ thông minh để lưu chúng mà không thực sự hiểu chúng là gì .
  • Nếu bạn sử dụng cùng một tên tệp để mở và lưu tệp, python-docx sẽ ngoan ngoãn ghi đè lên tệp gốc mà không cần nhìn trộm. Bạn muốn đảm bảo rằng, những gì bạn dự định.

Mở một tài liệu giống như tệp

python-docx có thể mở một tài liệu từ cái gọi là đối tượng giống như tệp. Nó cũng có thể lưu vào một đối tượng giống như tệp. Điều này có thể hữu ích khi bạn muốn lấy nguồn hoặc tài liệu đích qua kết nối mạng hoặc từ cơ sở dữ liệu và don sắt muốn (hoặc aren được phép) tương tác với hệ thống tệp. Trong thực tế, điều này có nghĩa là bạn có thể truyền một tệp mở hoặc chuỗi Stringio/byteo để mở hoặc lưu tài liệu như vậy:

f = open('foobar.docx', 'rb')
document = Document(f)
f.close()

# or

with open('foobar.docx', 'rb') as f:
    source_stream = StringIO(f.read())
document = Document(source_stream)
source_stream.close()
...
target_stream = StringIO()
document.save(target_stream)

Tham số chế độ mở tệp 'rb' được yêu cầu trên tất cả các hệ điều hành. Nó mặc định là

document = Document('existing-document-file.docx')
document.save('new-file-name.docx')
0, đôi khi đủ, nhưng ’B, (chọn chế độ nhị phân) được yêu cầu trên Windows và ít nhất một số phiên bản Linux để cho phép ZipFile mở tệp.

Được rồi, vì vậy bạn đã mở một tài liệu và khá chắc chắn rằng bạn có thể lưu nó ở đâu đó sau này. Bước tiếp theo là có được một số nội dung trong đó

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    So, we cannot work with these documents using normal text editors. But, we can manipulate these word documents in python using the python-docx module.

    Bàn luận The first step is to install this third-party module python-docx. You can use pip “pip install python-docx” or download the tarball from here. Here’s the Github repository.

    Tài liệu Word chứa văn bản được định dạng trong ba cấp đối tượng. Các đối tượng cấp cấp thấp nhất, các đối tượng đoạn trung bình và đối tượng tài liệu cấp cao nhất. Vì vậy, chúng ta không thể làm việc với các tài liệu này bằng cách sử dụng các trình chỉnh sửa văn bản thông thường. Nhưng, chúng ta có thể điều khiển các tài liệu từ này trong Python bằng mô-đun Python-DOCX. After installation import “docx” NOT “python-docx”.
    3. Use “docx.Document” class to start working with the word document.

    1. Bước đầu tiên là cài đặt mô-đun bên thứ ba này Python-docx. Bạn có thể sử dụng PIP PIP PIP Cài đặt Python-Docx, hoặc tải xuống tarball từ đây. Ở đây, kho lưu trữ GitHub.

    2. Sau khi cài đặt nhập, DOCX, không phải là Python-docx .3. Sử dụng lớp docx.document của lớp để bắt đầu làm việc với tài liệu từ.

    Mã số 1:

    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    6
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    7
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    8
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    9
    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    1
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    2

    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    3
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    4
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    5

    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    6
    List of paragraph objects:->>>
    [,
    ,
    ,
    ,
    ]
    
    List of runs objects in 1st paragraph:->>>
    []
    
    Text in the 1st paragraph:->>>
    Heading for the document
    
    The whole content of the document:->>>
    
    Heading for the document
    Your paragraph goes here, hey there, bold here, and these words are italic
    
    
    Heading level 2
    
    
    2
    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    1
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    4
    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    3
    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    4
    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    List of paragraph objects:->>>
    [,
    ,
    ,
    ,
    ]
    
    List of runs objects in 1st paragraph:->>>
    []
    
    Text in the 1st paragraph:->>>
    Heading for the document
    
    The whole content of the document:->>>
    
    Heading for the document
    Your paragraph goes here, hey there, bold here, and these words are italic
    
    
    Heading level 2
    
    
    9

    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    6python-docx1
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    8python-docx3
    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    python-docx5python-docx6

    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    python-docx8python-docx9

    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    Output:

    Hướng dẫn python work with word documents - Python làm việc với các tài liệu từ

    Hướng dẫn python work with word documents - Python làm việc với các tài liệu từ

    Lưu ý rằng ngắt trang trong trang thứ hai.
     
    Code #2: Now, to open a word document, create an instance along with passing the path to the document.

    python-docx1 python-docx2

    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    1 python-docx4

    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    3
    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    4 python-docx7python-docx8
    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    python-docx0python-docx1python-docx2

    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    python-docx0python-docx5

    python-docx0python-docx1python-docx8

    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    python-docx0python-docx1

    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    9python-docx3

    python-docx0python-docx1python-docx6

    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    python-docx0python-docx1

    document = Document('existing-document-file.docx')
    document.save('new-file-name.docx')
    
    9python-docx1

    python-docx0python-docx1python-docx4

    f = open('foobar.docx', 'rb')
    document = Document(f)
    f.close()
    
    # or
    
    with open('foobar.docx', 'rb') as f:
        source_stream = StringIO(f.read())
    document = Document(source_stream)
    source_stream.close()
    ...
    target_stream = StringIO()
    document.save(target_stream)
    
    0

    python-docx6 python-docx7python-docx8 python-docx9

    'rb'0python-docx0'rb'2

    Output:

    List of paragraph objects:->>>
    [,
    ,
    ,
    ,
    ]
    
    List of runs objects in 1st paragraph:->>>
    []
    
    Text in the 1st paragraph:->>>
    Heading for the document
    
    The whole content of the document:->>>
    
    Heading for the document
    Your paragraph goes here, hey there, bold here, and these words are italic
    
    
    Heading level 2
    
    

    Tham khảo: https://python-docx.readthedocs.io/en/latest/#user-guide.https://python-docx.readthedocs.io/en/latest/#user-guide.


    Python có thể đọc tài liệu từ không?

    Khoa học dữ liệu thực tế bằng cách sử dụng Python để đọc một tài liệu từ mà chúng tôi nhận trợ giúp của mô -đun có tên Docx. Trước tiên chúng tôi cài đặt DOCX như hình dưới đây. Sau đó viết một chương trình để sử dụng các chức năng khác nhau trong mô -đun DOCX để đọc toàn bộ tệp theo các đoạn văn. Chúng tôi sử dụng lệnh dưới đây để đưa mô -đun DOCX vào môi trường của chúng tôi.To read a word document we take help of the module named docx. We first install docx as shown below. Then write a program to use the different functions in docx module to read the entire file by paragraphs. We use the below command to get the docx module into our environment.

    Làm cách nào để chạy một docx trong Python?

    Nhưng chúng ta có thể thao tác với các tài liệu từ này trong Python bằng mô-đun Python-DOCX ...
    Bước đầu tiên là cài đặt mô-đun bên thứ ba này Python-Docx. Bạn có thể sử dụng PIP PIP PIP Cài đặt Python-DOCX.
    Sau khi cài đặt nhập, DOCX, không phải là Python-docx, ..
    Sử dụng DOCX. Lớp học tài liệu để bắt đầu làm việc với tài liệu từ ..

    Làm thế nào để bạn tự động hóa một tài liệu từ trong Python?

    Tạo tài liệu Word bằng Python trước tiên, tạo một thể hiện của lớp tài liệu.Tiếp theo, tạo một thể hiện của lớp DocumentBuilder với đối tượng tài liệu làm đối số.Sau đó, chèn/ghi các phần tử để thêm một số văn bản, đoạn văn, bảng hoặc hình ảnh bằng đối tượng DocumentBuilder.create an instance of the Document class. Next, create an instance of the DocumentBuilder class with the Document object as an argument. After that, insert/write elements to add some text, paragraphs, tables, or images using the DocumentBuilder object.

    Làm cách nào để chỉnh sửa một tài liệu từ trong Python?

    Cách chỉnh sửa tài liệu Microsoft Word bằng Python..
    Từ tài liệu nhập tài liệu docx = tài liệu ("sơ yếu lý lịch.docx") đoạn = tài liệu.Đoạn văn [0] in (đoạn. ....
    Rik Voorhaar ..
    đoạn văn.....
    Document = document ("Resume.docx") với Open ('Resume.xml', 'W') là f: f.....
    Document = document ("Resume.docx") đoạn = Document ..