Mô-đun cung cấp một số chức năng tiện lợi, cũng như lớp thực hiện tất cả công việc. Nếu bạn chỉ gói hoặc điền một hoặc hai chuỗi văn bản, các chức năng tiện lợi sẽ đủ tốt;
gói văn bản. quấn[văn bản , chiều rộng=70 , * , initial_indent='' , subsequent_indent='' , expand_tabs=True , replace_whitespace=True , fix_sentence_endings=Sai , break_long_words=True , drop_whitespace=True , break_on_hyphens=True , kích thước tab=8 , max_lines=Không có , giữ chỗ=' [. ]']Ngắt dòng một đoạn trong văn bản [một chuỗi] để mỗi dòng dài tối đa các ký tự. Tất cả các tùy chọn gói được lấy từ các thuộc tính cá thể của cá thể. Trả về danh sách các dòng đầu ra, không có dòng mới cuối cùng. Nếu đầu ra được bao bọc không có nội dung, danh sách trả về trống
Đây là lý do tại sao nó là một phép tính chỉ có thể được thực hiện bởi một công cụ kết xuất, bằng cách thực sự điều chỉnh văn bản có kích thước, phông chữ nhất định, v.v. vào một không gian và xem nó đi ra như thế nào
Mặc dù python-docx có vẻ giống như ma thuật, nhưng tất cả những gì nó làm là tạo một hệ thống phân cấp phần tử XML phức tạp và sau đó lưu trữ nó trong kho lưu trữ zip. Chúng tôi không có hoặc cần một công cụ kết xuất cho công việc đó. Điều này cũng có nghĩa là chúng tôi bị giới hạn ở những gì được ghi vào tệp XML
Chúng tôi gặp phải hạn chế tương tự khi muốn biết vị trí ngắt trang trong tài liệu. Đó thực chất là một phiên bản quy mô lớn hơn của cùng một vấn đề
Nếu bạn muốn giải quyết vấn đề này đủ tệ, bạn có thể viết một số mã hiển thị lấy kiểu chữ, đặt kích thước và lấy số liệu văn bản từ hệ điều hành, v.v. Tôi thực sự đã làm điều đó một lần trong dự án `python-pptx`. Nó không đẹp và hóa ra không chính xác lắm, nhưng nó đã hoàn thành công việc cụ thể đó. Bạn có thể xem nó ở đây
Tài liệu Word chứa văn bản được định dạng được bao bọc trong ba cấp độ đối tượng. Các đối tượng cấp thấp nhất, đối tượng đoạn văn cấp 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 những văn bản này bằng các trình soạn thảo văn bản thông thường. Tuy nhiên, chúng ta có thể thao tác với các tài liệu từ này trong python bằng cách sử dụng mô-đun python-docx. Lệnh Pip để cài đặt mô-đun này là
pip install python-docx
Mô-đun docx Python cho phép người dùng thao tác với tài liệu bằng cách thao tác với tài liệu hiện có hoặc tạo một tài liệu trống mới và thao tác với nó. Nó là một công cụ mạnh mẽ vì nó giúp bạn thao tác với tài liệu ở phạm vi rất lớn
giãn dòng
Để đặt khoảng cách dòng giữa văn bản trong đoạn văn, chúng tôi sử dụng định dạng đoạn văn cùng với khoảng cách dòng. Nó được sử dụng để đặt khoảng cách giữa mỗi dòng trong đoạn văn
cú pháp. cho. đoạn_format. line_spacing = Chiều dài
Tham số. Chiều dài. Đó là chiều dài của khoảng trống được để lại giữa các dòng. Nó lấy chiều dài làm đầu vào. Nó có thể được xác định bằng giá trị khoảng cách tuyệt đối hoặc bằng giá trị khoảng cách tương đối của line-height. Nếu đầu vào ở dạng pt, inch hoặc cm thì nó được coi là giá trị tuyệt đối và nếu đầu vào ở dạng float thì nó được coi là giá trị tương đối
ví dụ 1. Đặt khoảng cách dòng với giá trị khoảng cách tuyệt đối
Python3
# Import docx NOT python-docx
import
docx
from
docx.shared
import
Inches
# Create an instance of a word document
from docx.enum.text import WD_ALIGN_PARAGRAPH0
from docx.enum.text import WD_ALIGN_PARAGRAPH1
from docx.enum.text import WD_ALIGN_PARAGRAPH2
from docx.enum.text import WD_ALIGN_PARAGRAPH3
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH9
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______182_______1
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
7# Import docx NOT python-docx
8from docx.enum.text import WD_ALIGN_PARAGRAPH8
import
0
import
1_______1_______1 import
3import
4
from docx.enum.text import WD_ALIGN_PARAGRAPH8
import
6
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______183_______8
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
7# Import docx NOT python-docx
8
from docx.enum.text import WD_ALIGN_PARAGRAPH8
docx
5
docx
6docx
7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
đầu ra
ví dụ 2. Đặt khoảng cách dòng với giá trị tương đối
Python3
# Import docx NOT python-docx
import
docx
# Create an instance of a word document
from docx.enum.text import WD_ALIGN_PARAGRAPH0
from docx.enum.text import WD_ALIGN_PARAGRAPH1
from docx.enum.text import WD_ALIGN_PARAGRAPH2
from docx.enum.text import WD_ALIGN_PARAGRAPH3
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH9
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______182_______1
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
7# Import docx NOT python-docx
8from docx.enum.text import WD_ALIGN_PARAGRAPH8
import
3
import
1____1_______1 import
6
import
6
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______183_______8
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
7# Import docx NOT python-docx
8
from docx.enum.text import WD_ALIGN_PARAGRAPH8
docx
5
docx
6docx
7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
đầu ra
Giãn cách đoạn văn
Để áp dụng giãn cách đoạn văn cho các đoạn văn bản trong tài liệu Word, chúng tôi sử dụng. đoạn_format cùng với. dấu cách_trước và. dấu cách_sau. Nó chỉ định khoảng trống được để lại trước và sau đoạn văn tương ứng. Nó chỉ có thể lấy giá trị dương làm đầu vào, nếu chúng ta đưa ra bất kỳ giá trị âm nào, nó sẽ báo lỗi phạm vi
Sr. Không
khoảng cách
Sự miêu tả
1
dấu cách_trước
Nó thêm khoảng trống trước đoạn văn trong tài liệu từ
2
dấu cách_sau
Nó thêm khoảng trống sau đoạn văn trong tài liệu từ
ví dụ 3. Thêm đoạn có và không có khoảng cách trong tài liệu Word
Python3
# Import docx NOT python-docx
import
docx
from
docx.shared
import
Inches
# Create an instance of a word document
from docx.enum.text import WD_ALIGN_PARAGRAPH0
from docx.enum.text import WD_ALIGN_PARAGRAPH1
from docx.enum.text import WD_ALIGN_PARAGRAPH2
from docx.enum.text import WD_ALIGN_PARAGRAPH3
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH9
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______09
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
7from docx.enum.text import WD_ALIGN_PARAGRAPH16
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH18
from docx.enum.text import WD_ALIGN_PARAGRAPH19_______1_______1
import
3from docx.enum.text import WD_ALIGN_PARAGRAPH22_______1_______8
from docx.enum.text import WD_ALIGN_PARAGRAPH24
from docx.enum.text import WD_ALIGN_PARAGRAPH1
import
3from docx.enum.text import WD_ALIGN_PARAGRAPH22_______1_______8
import
6
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______31
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
7
from docx.enum.text import WD_ALIGN_PARAGRAPH16
from docx.enum.text import WD_ALIGN_PARAGRAPH8
docx
5
docx
6docx
7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
đầu ra
Căn ngang
Để thiết lập căn lề ngang trong văn bản, chúng ta sẽ sử dụng. đoạn_format. phương pháp căn chỉnh. Nó được sử dụng cùng với WD_PARAGRAPH_ALIGNMENT để đặt căn chỉnh của đoạn văn. Bạn phải nhập WD_PARAGRAPH_ALIGNMENT từ docx. liệt kê. văn bản trước khi sử dụng nó
from docx.enum.text import WD_ALIGN_PARAGRAPH
cú pháp. cho. đoạn_format. căn chỉnh = WD_ALIGN_PARAGRAPH. [Căn chỉnh]
Tham số.
căn chỉnh. Nó được sử dụng để thiết lập sự liên kết. Bạn có thể đặt căn chỉnh thành bất kỳ bên trái, Trung tâm, bên phải hoặc căn chỉnh hoàn toàn
Các sắp xếp khác nhau được mô tả dưới đây
Sr. Không
Tên liên kết
Sự miêu tả
1
TRUNG TÂM
Nó đặt căn chỉnh thành Trung tâm
2
BÊN TRÁI
Nó đặt căn chỉnh sang trái
3
ĐÚNG
Nó đặt căn chỉnh sang phải
4
BẰNG CHỨNG
Nó thiết lập sự liên kết để biện minh
5
PHÂN PHÁT
Nó đặt các ký tự theo cách chúng lấp đầy toàn bộ chiều rộng của đoạn văn
ví dụ 1. Thêm các đoạn văn với Căn chỉnh Ngang khác nhau
Python3
# Import docx NOT python-docx
import
docx
from
from docx.enum.text import WD_ALIGN_PARAGRAPH46
import
from docx.enum.text import WD_ALIGN_PARAGRAPH48
# Create an instance of a word document
from docx.enum.text import WD_ALIGN_PARAGRAPH0
from docx.enum.text import WD_ALIGN_PARAGRAPH1
from docx.enum.text import WD_ALIGN_PARAGRAPH2
from docx.enum.text import WD_ALIGN_PARAGRAPH3
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH59
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______61
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
7from docx.enum.text import WD_ALIGN_PARAGRAPH16
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH70
from docx.enum.text import WD_ALIGN_PARAGRAPH1
from docx.enum.text import WD_ALIGN_PARAGRAPH72
from docx.enum.text import WD_ALIGN_PARAGRAPH73
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______75
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
7from docx.enum.text import WD_ALIGN_PARAGRAPH16
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH70
from docx.enum.text import WD_ALIGN_PARAGRAPH1
from docx.enum.text import WD_ALIGN_PARAGRAPH86
from docx.enum.text import WD_ALIGN_PARAGRAPH87
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______89
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
7from docx.enum.text import WD_ALIGN_PARAGRAPH16
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH70
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
00
# Import docx NOT python-docx
01
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______182_______03
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
7from docx.enum.text import WD_ALIGN_PARAGRAPH16
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH70
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
14
# Import docx NOT python-docx
15
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______182_______17
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
7from docx.enum.text import WD_ALIGN_PARAGRAPH16
from docx.enum.text import WD_ALIGN_PARAGRAPH8
from docx.enum.text import WD_ALIGN_PARAGRAPH70
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
28
docx
5
docx
6docx
7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
đầu ra
thụt đầu dòng
Để đặt thụt lề trong văn bản, chúng tôi sẽ sử dụng. phương pháp định dạng đoạn văn. Để áp dụng thụt lề, chúng tôi sử dụng left_indent và right_indent với. đoạn_format và đặt giá trị của vết lõm. Bạn phải chỉ định thụt đầu dòng với giá trị độ dài i. e inch, pt hoặc cm. Bạn cũng có thể đặt một giá trị âm dưới dạng thụt đầu dòng, điều này sẽ khiến đoạn văn trùng với lề theo giá trị đã chỉ định
Sr. Không
thụt đầu dòng
Sự miêu tả
1
left_indent
Nó đặt thụt lề trái cho đoạn văn trong file word
2
right_indent
Nó đặt thụt lề bên phải của đoạn văn trong tệp từ
cú pháp
- Đối với vết lõm bên trái. cho. đoạn_format. left_indent = kích thước
- Đối với vết lõm bên phải. cho. đoạn_format. right_indent = kích thước
Tham số.
kích thước. Đó là giá trị mà chúng tôi muốn thụt lề trên đoạn văn của mình. Nó có thể tính bằng inch, pt hoặc cm… v.v.
ví dụ 2. Đặt thụt lề trái và phải của đoạn văn
Python3
# Import docx NOT python-docx
import
docx
from
docx.shared
import
Inches
# Create an instance of a word document
from docx.enum.text import WD_ALIGN_PARAGRAPH0
from docx.enum.text import WD_ALIGN_PARAGRAPH1
from docx.enum.text import WD_ALIGN_PARAGRAPH2
from docx.enum.text import WD_ALIGN_PARAGRAPH3
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
50
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______182_______52
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
58# Import docx NOT python-docx
59 # Import docx NOT python-docx
60# Import docx NOT python-docx
61 # Import docx NOT python-docx
62# Import docx NOT python-docx
63 # Import docx NOT python-docx
64# Import docx NOT python-docx
65# Import docx NOT python-docx
66
# Import docx NOT python-docx
67# Import docx NOT python-docx
63 # Import docx NOT python-docx
69
# Import docx NOT python-docx
70_______1_______1 import
3import
4
from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
75
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______182_______77
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
58# Import docx NOT python-docx
59 # Import docx NOT python-docx
85# Import docx NOT python-docx
61 # Import docx NOT python-docx
62# Import docx NOT python-docx
63 # Import docx NOT python-docx
64# Import docx NOT python-docx
65# Import docx NOT python-docx
91
# Import docx NOT python-docx
67# Import docx NOT python-docx
63 # Import docx NOT python-docx
69
# Import docx NOT python-docx
95
from docx.enum.text import WD_ALIGN_PARAGRAPH1
import
3import
4from docx.enum.text import WD_ALIGN_PARAGRAPH8
docx
5
docx
6docx
7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
đầu ra
ví dụ 3. Đặt giá trị âm cho thụt lề trái và phải của đoạn văn
Python3
# Import docx NOT python-docx
import
docx
from
docx.shared
import
Inches
# Create an instance of a word document
from docx.enum.text import WD_ALIGN_PARAGRAPH0
from docx.enum.text import WD_ALIGN_PARAGRAPH1
from docx.enum.text import WD_ALIGN_PARAGRAPH2
from docx.enum.text import WD_ALIGN_PARAGRAPH3
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
import
21
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______182_______52
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
58# Import docx NOT python-docx
59 # Import docx NOT python-docx
60# Import docx NOT python-docx
61 # Import docx NOT python-docx
62# Import docx NOT python-docx
63 # Import docx NOT python-docx
64# Import docx NOT python-docx
65# Import docx NOT python-docx
91
# Import docx NOT python-docx
67# Import docx NOT python-docx
63 # Import docx NOT python-docx
69
# Import docx NOT python-docx
70_______1_______1 import
3# Import docx NOT python-docx
65import
4
from docx.enum.text import WD_ALIGN_PARAGRAPH8
import
47
from docx.enum.text import WD_ALIGN_PARAGRAPH4_______182_______77
from docx.enum.text import WD_ALIGN_PARAGRAPH6
# Import docx NOT python-docx
3from docx.enum.text import WD_ALIGN_PARAGRAPH8
# Import docx NOT python-docx
5
from docx.enum.text import WD_ALIGN_PARAGRAPH1
# Import docx NOT python-docx
58# Import docx NOT python-docx
59 # Import docx NOT python-docx
85# Import docx NOT python-docx
61 # Import docx NOT python-docx
62# Import docx NOT python-docx
63 # Import docx NOT python-docx
64# Import docx NOT python-docx
65# Import docx NOT python-docx
91
# Import docx NOT python-docx
67# Import docx NOT python-docx
63 # Import docx NOT python-docx
69
# Import docx NOT python-docx
95
from docx.enum.text import WD_ALIGN_PARAGRAPH1
import
3# Import docx NOT python-docx
65import
4from docx.enum.text import WD_ALIGN_PARAGRAPH8
docx
5
docx
6docx
7
from docx.enum.text import WD_ALIGN_PARAGRAPH8
đầu ra
Bạn cũng có thể chỉ đặt thụt đầu dòng cho dòng đầu tiên của đoạn văn bằng cách sử dụng. đoạn_format cùng với. thuộc tính first_line_indent. Nó chỉ định độ dài thụt đầu dòng giữa dòng đầu tiên và các dòng khác
cú pháp. cho. đoạn_format. first_line_indent = Độ dài
Thông số.
Chiều dài. Đó là độ dài được để lại dưới dạng thụt đầu dòng ở dòng đầu tiên. Giá trị dương sẽ làm cho dòng thụt lề trong khi giá trị âm sẽ gây ra thụt đầu dòng