Hướng dẫn docx2python github - docx2python trên github

".format(x) forxinrow]) # wrap each row in tagsfor (i,), tableinenum_at_depth(tables, 1): tables[i] ="".join("{x}".format(x) forxintable) # wrap each table in

docx2python

Trích xuất các tiêu đề, chân trang, văn bản, chú thích, chú thích, thuộc tính và hình ảnh cho một đối tượng Python.

Lưu ý cho người dùng / người đóng góp

Tôi sẽ thực hiện rất ít mã hóa vào năm 2022. Tôi sẽ giải quyết các lỗi "Hiển thị nút chặn" trong docx2python và tôi sẽ chấp nhận các yêu cầu kéo nếu chúng hoàn thành với

  • Tệp ví dụ
    for name, image in result.images.items():
        with open(name, 'wb') as image_destination:
            write(image_destination, image)
    
    5 hiển thị tính năng vấn đề được giải quyết hoặc thiếu
  • Một tệp kiểm tra mới với các thử nghiệm (pytest) cho sửa chữa hoặc tính năng mới

for name, image in result.images.items():
    with open(name, 'wb') as image_destination:
        write(image_destination, image)
6 có thể giúp nếu bạn muốn mở rộng docx2python.

Quay lại với Docx2python

Để tóm tắt về những gì mới trong docx2python 2, hãy cuộn xuống mới trong docx2python phiên bản 2New in docx2python Version 2

Mã này là sự mở rộng/co thắt của Python-DOCX2TXT (Bản quyền (C) 2015 Ankush Shah). Mã ban đầu chủ yếu là biến mất, nhưng một số xương vẫn có thể ở đây.

Các tính năng được chia sẻ::

  • Trích xuất văn bản từ các tệp docx
  • Trích xuất hình ảnh từ các tệp docx

additions:

  • Trích xuất chú thích và chú thích
  • Chuyển đổi đạn và danh sách được đánh số thành ASCII với thụt
  • Chuyển đổi siêu liên kết thành
    for name, image in result.images.items():
        with open(name, 'wb') as image_destination:
            write(image_destination, image)
    
    7
  • Giữ lại một số cấu trúc của tệp gốc (bên dưới)
  • Trích xuất các thuộc tính tài liệu (người tạo, LastModifiedBy, v.v.)
  • Chèn trình giữ chỗ hình ảnh trong văn bản (
    for name, image in result.images.items():
        with open(name, 'wb') as image_destination:
            write(image_destination, image)
    
    8)
  • Chèn chú thích văn bản đơn giản và tham chiếu cuối cùng trong văn bản (
    for name, image in result.images.items():
        with open(name, 'wb') as image_destination:
            write(image_destination, image)
    
    9)
  • .
  • Trích xuất phương trình toán học
  • Trích xuất các lựa chọn người dùng từ hộp kiểm và menu thả xuống

subtractions:

  • Không có giao diện dòng lệnh
  • sẽ chỉ hoạt động với Python 3.7+

Cài đặt

Sử dụng

from docx2python import docx2python

# extract docx content
docx2python('path/to/file.docx')

# extract docx content, write images to image_directory
docx2python('path/to/file.docx', 'path/to/image_directory')

# extract docx content, ignore images
docx2python('path/to/file.docx', extract_image=False)

# extract docx content with basic font styles converted to html
docx2python('path/to/file.docx', html=True)

Lưu ý trên tính năng HTML:

  • Hỗ trợ
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    0Italic,
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    1Bold,
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    2UnderLine,
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    3Strike,
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    4Superscript,
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    5SubScript,
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    6Small Caps,
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    7All CAPS,
    [  # document
        [  # table
            [  # row
                [  # cell
                    "Paragraph 1",
                    "Paragraph 2",
                    "-- bulleted list",
                    "-- continuing bulleted list",
                    "1)  numbered list",
                    "2)  continuing numbered list"
                    "    a)  sublist",
                    "        i)  sublist of sublist",
                    "3)  keeps track of indention levels",
                    "    a)  resets sublist counters"
                ]
            ]
         ]
     ]
    8HIGHT.
  • Các siêu liên kết sẽ luôn được xuất khẩu dưới dạng HTML (
    for name, image in result.images.items():
        with open(name, 'wb') as image_destination:
            write(image_destination, image)
    
    7), ngay cả khi
    from docx2python.iterators import enum_cells
    
    def remove_empty_paragraphs(tables):
        for (i, j, k), cell in enum_cells(tables):
            tables[i][j][k] = [x for x in cell if x]
    2, bởi vì tôi không thể nghĩ về một đại diện chính tắc hơn.
  • Mỗi thẻ mở trong một đoạn sẽ được đóng trong đoạn đó (và, khi thích hợp, mở lại trong đoạn tiếp theo). Nếu hai đoạn sau được in đậm, chúng sẽ được trả lại là
    from docx2python.iterators import enum_cells
    
    def remove_empty_paragraphs(tables):
        for (i, j, k), cell in enum_cells(tables):
            tables[i][j][k] = [x for x in cell if x]
    3,
    from docx2python.iterators import enum_cells
    
    def remove_empty_paragraphs(tables):
        for (i, j, k), cell in enum_cells(tables):
            tables[i][j][k] = [x for x in cell if x]
    4. Đây là cố ý để làm cho mỗi đoạn văn của riêng mình.
  • Nếu bạn chỉ định
    from docx2python.iterators import enum_cells
    
    def remove_empty_paragraphs(tables):
        for (i, j, k), cell in enum_cells(tables):
            tables[i][j][k] = [x for x in cell if x]
    5,
    from docx2python.iterators import enum_cells
    
    def remove_empty_paragraphs(tables):
        for (i, j, k), cell in enum_cells(tables):
            tables[i][j][k] = [x for x in cell if x]
    6,
    from docx2python.iterators import enum_cells
    
    def remove_empty_paragraphs(tables):
        for (i, j, k), cell in enum_cells(tables):
            tables[i][j][k] = [x for x in cell if x]
    7 và
    from docx2python.iterators import enum_cells
    
    def remove_empty_paragraphs(tables):
        for (i, j, k), cell in enum_cells(tables):
            tables[i][j][k] = [x for x in cell if x]
    8 trong văn bản tài liệu của bạn sẽ được mã hóa là
    from docx2python.iterators import enum_cells
    
    def remove_empty_paragraphs(tables):
        for (i, j, k), cell in enum_cells(tables):
            tables[i][j][k] = [x for x in cell if x]
    9,
    >>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
    >>> remove_empty_paragraphs(tables)
        [[[['a', 'b'], ['a', 'd']]]]
    
    0 và
    >>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
    >>> remove_empty_paragraphs(tables)
        [[[['a', 'b'], ['a', 'd']]]]
    
    1

Giá trị trả về

Hàm

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
2 Trả về một đối tượng với một số thuộc tính.

Tiêu đề - Nội dung của các tiêu đề DOCX ở định dạng trả về được mô tả trong tài liệu này - contents of the docx headers in the return format described herein

Footer - Nội dung của Footers Docx ở định dạng trở lại được mô tả trong tài liệu này - contents of the docx footers in the return format described herein

Cơ thể - Nội dung của Docx ở định dạng trở lại được mô tả trong tài liệu này - contents of the docx in the return format described herein

Chú thích - Nội dung của DOCX ở định dạng trở lại được mô tả trong tài liệu này - contents of the docx in the return format described herein

EndNotes - Nội dung của DOCX ở định dạng trả về được mô tả trong tài liệu này - contents of the docx in the return format described herein

Tài liệu - Tiêu đề + Body + Footer (Chỉ đọc) - header + body + footer (read only)

Văn bản - Tất cả văn bản docx là một chuỗi, tương tự như những gì bạn nhận được từ

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
3 - all docx text as one string, similar to what you'd get from
>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
3

Thuộc tính - Tên thuộc tính docx được ánh xạ tới các giá trị (ví dụ:

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
4) - docx property names mapped to values (e.g.,
>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
4)

Hình ảnh - Tên hình ảnh được ánh xạ tới hình ảnh ở định dạng nhị phân. Ghi vào hệ thống tập tin với - image names mapped to images in binary format. Write to filesystem with

for name, image in result.images.items():
    with open(name, 'wb') as image_destination:
        write(image_destination, image)

DOCX_READER - Một phiên bản docxreader (xem

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
5) với một số phương thức để trích xuất các phần XML. - a DocxReader (see
>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
5) instance with several methods for extracting xml portions.

Định dạng trả lại

Một số cấu trúc sẽ được duy trì. Văn bản sẽ được trả lại trong một danh sách lồng nhau, với các đoạn văn luôn ở độ sâu 4 (tức là,

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
6 sẽ là một đoạn văn).

Nếu docx của bạn không có bảng, đầu ra sẽ xuất hiện dưới dạng một bảng có tất cả nội dung trong một ô:

[  # document
    [  # table
        [  # row
            [  # cell
                "Paragraph 1",
                "Paragraph 2",
                "-- bulleted list",
                "-- continuing bulleted list",
                "1)  numbered list",
                "2)  continuing numbered list"
                "    a)  sublist",
                "        i)  sublist of sublist",
                "3)  keeps track of indention levels",
                "    a)  resets sublist counters"
            ]
        ]
     ]
 ]

Các ô bảng sẽ xuất hiện dưới dạng tế bào bảng. Văn bản bên ngoài bảng sẽ xuất hiện dưới dạng ô bảng.

Một tài liệu docx có thể là các bảng trong các bảng trong các bảng. Docx2python làm phẳng hầu hết điều này để dễ dàng điều hướng hơn trong nội dung.

Làm việc với đầu ra

Gói này cung cấp một số chức năng trợ giúp tài liệu trong mô -đun

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
7. Dưới đây là một vài công thức nấu ăn có thể với các chức năng này:

from docx2python.iterators import enum_cells

def remove_empty_paragraphs(tables):
    for (i, j, k), cell in enum_cells(tables):
        tables[i][j][k] = [x for x in cell if x]

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]

from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in
tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}
tagstables="".join(['
{x}
'.format(x) for x in tables]) return [""] + tables + [""]

>>> tables = [[[['a', 'b'], ['a', 'd']]]]
>>> html_map(tables)

    
        
'(0, 0, 0, 0) a' '(0, 0, 0, 1) b' '(0, 0, 1, 0) a' '(0, 0, 1, 1) d'

Xem các chức năng trợ giúp.

Một số bản in đẹp về hộp kiểm:

MS Word có các hộp kiểm có thể được kiểm tra bất cứ lúc nào và các hộp khác chỉ có thể được kiểm tra khi biểu mẫu bị khóa. Bản in trước như.

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
8 (Hộp kiểm mở) hoặc
>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
9 (hộp kiểm chéo). Mà mô -đun này, sau này cũng sẽ như vậy. Tôi đã cho các hộp kiểm một giá trị cứu trợ là
from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
0 nếu XML không giống như tôi mong đợi, bởi vì tôi không có các tệp kiểm tra vài nghìn với các hộp kiểm (như tôi đã làm với hầu hết các yếu tố hình thức khác). Hộp kiểm nên hoạt động, nhưng vui lòng cho tôi biết nếu bạn gặp bất kỳ điều gì không.

Mới trong Docx2Python phiên bản 2

Hợp nhất các lần chạy liên tiếp với định dạng giống hệt

MS Word sẽ phá vỡ văn bản chạy tùy ý, thường ở giữa một từ.


    work to im


    prove docx2python

Điều này làm cho những thứ như tìm kiếm và thay thế thuật toán có vấn đề. Docx2python hiện không viết các tệp DOCX, nhưng tôi thường sử dụng các mẫu DOCX với trình giữ chỗ (ví dụ:

from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
1) sau đó thay thế các giữ chỗ đó bằng dữ liệu. Điều này sẽ không hoạt động nếu người giữ chỗ của bạn bị hỏng (ví dụ:
from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
2,
from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
3,
from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
4).

Docx2python V1 hợp nhất các hoạt động như vậy khi xuất văn bản. Docx2python v2 sẽ hợp nhất các lần chạy như vậy trong XML như một bước xử lý trước. Điều này sẽ cho phép tiết kiệm XML "sửa chữa" như vậy sau này.

Hợp nhất các liên kết liên tiếp với HREF giống hệt nhau

MS Word sẽ phá vỡ các liên kết, cung cấp cho mỗi liên kết một

from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
5 khác nhau, ngay cả khi các
from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
6 này trỏ đến cùng một địa chỉ.

  # rID13 points to https://github.com/ShayHill/docx2python
    
        docx2py
    

  # rID14 ALSO points to https://github.com/ShayHill/docx2python
    
        thon
    

Điều này tương tự như các lần chạy chia tay, nhưng nguyên nhân sâu hơn một chút. Docx2python v1 tạo ra một mớ hỗn độn của những điều này.

docx2py
thon

Docx2python V2 sẽ hợp nhất các liên kết như vậy với nhau trong XML như một bước xử lý trước. Như trên, điều này sẽ cho phép tiết kiệm XML "sửa chữa" như vậy sau này.

Xử lý chính xác các đoạn văn lồng nhau

MS Word sẽ làm tổ đoạn văn

for name, image in result.images.items():
    with open(name, 'wb') as image_destination:
        write(image_destination, image)
0

Tôi đã không thể tạo một đoạn văn như vậy, nhưng tôi đã tìm thấy một vài tệp có chúng. Docx2pyhon v1 sẽ bỏ qua các thẻ HTML đóng khi một đoạn mới được mở trước khi đoạn văn cũ được đóng lại.

for name, image in result.images.items():
    with open(name, 'wb') as image_destination:
        write(image_destination, image)
1

Docx2python V2 sẽ xử lý chính xác các trường hợp như vậy, nhưng điều này sẽ yêu cầu thay đổi nội bộ đáng kể về cách Docx2python mở ra và đóng các đoạn văn.

for name, image in result.images.items():
    with open(name, 'wb') as image_destination:
        write(image_destination, image)
2

Phong cách đoạn văn

Các thay đổi nội bộ cho phép dễ dàng truy cập vào các kiểu đoạn văn (ví dụ:

from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
7). Docx2python V1 bỏ qua những điều này, ngay cả với
from docx2python.iterators import enum_cells

def remove_empty_paragraphs(tables):
    for (i, j, k), cell in enum_cells(tables):
        tables[i][j][k] = [x for x in cell if x]
5. Docx2python v2 sẽ chụp các kiểu đoạn văn.

for name, image in result.images.items():
    with open(name, 'wb') as image_destination:
        write(image_destination, image)
3

Xuất XML

Để cho phép chỉnh sửa ánh sáng được mô tả ở trên (ví dụ: tìm kiếm và thay thế), docx2python v2 sẽ cho phép người dùng truy cập vào

for name, image in result.images.items():
    with open(name, 'wb') as image_destination:
        write(image_destination, image)
4

Người dùng chỉ có thể đi xa với điều này. Một tệp DOCX được xây dựng từ các thư mục chứa đầy các tệp XML. Không có tệp XML nào được khép kín. Nhưng tìm kiếm và thay thế là đủ để tạo các mẫu tài liệu (tài liệu với trình giữ chỗ cho dữ liệu) và điều đó khá hữu ích.

phơi bày một số chức năng trung gian

Điều hướng qua XML rất đơn giản với

from docx2python.iterators import enum_at_depth

def html_map(tables) -> str:
    """Create an HTML map of document contents.

    Render this in a browser to visually search for data.
    
    :tables: value could come from, e.g.,
        * docx_to_text_output.document
        * docx_to_text_output.body
    """

    # prepend index tuple to each paragraph
    for (i, j, k, l), paragraph in enum_at_depth(tables, 4):
        tables[i][j][k][l] = " ".join([str((i, j, k, l)), paragraph])

    # wrap each paragraph in 
 tags
    for (i, j, k), cell in enum_at_depth(tables, 3):
        tables[i][j][k] = "".join(["
{x}
"
.format(x) for x in cell]) # wrap each cell in tags for (i, j), row in enum_at_depth(tables, 2): tables[i][j] = "".join(["{x}".format(x) for x in row]) # wrap each row in tags for (i,), table in enum_at_depth(tables, 1): tables[i] = "".join("{x}".format(x) for x in table) # wrap each table in tagstables="".join(['
{x}
'
.format(x) for x in tables]) return [""] + tables + [""]
9. Đó là một bước riêng biệt để thực hiện bất cứ điều gì bạn tìm thấy và mang nó ra khỏi XML. Chẳng hạn, bạn có thể muốn lặp lại một tài liệu, tìm kiếm các đoạn văn với một định dạng cụ thể, sau đó rút văn bản ra khỏi các đoạn đó. Docx2python V1 đã không tách hoặc phơi bày "iter tài liệu" và "kéo nội dung". Docx2python V2 tách biệt và phơi bày các bước này. Điều này sẽ cho phép mở rộng dễ dàng hơn.

Xem mô -đun

>>> tables = [[[['a', 'b'], ['a', '', 'd', '']]]]
>>> remove_empty_paragraphs(tables)
    [[[['a', 'b'], ['a', 'd']]]]
5 và các ví dụ đơn giản trong mô -đun
>>> tables = [[[['a', 'b'], ['a', 'd']]]]
>>> html_map(tables)

    
        
'(0, 0, 0, 0) a' '(0, 0, 0, 1) b' '(0, 0, 1, 0) a' '(0, 0, 1, 1) d'
1.

Xem Tiện ích.py ​​để biết ví dụ về các tính năng mới chính.