Python docx thêm ngắt dòng

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.sharedimport Inches

 

# Create an instance of a word document

from docx.enum.text import WD_ALIGN_PARAGRAPH
0
from docx.enum.text import WD_ALIGN_PARAGRAPH
1
from docx.enum.text import WD_ALIGN_PARAGRAPH
2

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
3

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH
7
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
9

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______182_______1
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx7# Import docx NOT python-docx8
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

import0

import1_______1_______1 import3import4

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

import6

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______183_______8
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx7# Import docx NOT python-docx8

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

docx5

docx6docx7

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

đầ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_PARAGRAPH
0
from docx.enum.text import WD_ALIGN_PARAGRAPH
1
from docx.enum.text import WD_ALIGN_PARAGRAPH
2

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
3

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH
7
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
9

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______182_______1
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx7# Import docx NOT python-docx8
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

import3

import1____1_______1 import6

 

import6

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______183_______8
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx7# Import docx NOT python-docx8

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

docx5

docx6docx7

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

đầ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.sharedimport Inches

 

# Create an instance of a word document

from docx.enum.text import WD_ALIGN_PARAGRAPH
0
from docx.enum.text import WD_ALIGN_PARAGRAPH
1
from docx.enum.text import WD_ALIGN_PARAGRAPH
2

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
3

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH
7
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
9

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______09
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx7
from docx.enum.text import WD_ALIGN_PARAGRAPH
16
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

from docx.enum.text import WD_ALIGN_PARAGRAPH
18

from docx.enum.text import WD_ALIGN_PARAGRAPH
19_______1_______1 import3
from docx.enum.text import WD_ALIGN_PARAGRAPH
22_______1_______8

from docx.enum.text import WD_ALIGN_PARAGRAPH
24
from docx.enum.text import WD_ALIGN_PARAGRAPH
1 import3
from docx.enum.text import WD_ALIGN_PARAGRAPH
22_______1_______8

 

import6

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______31
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx7

from docx.enum.text import WD_ALIGN_PARAGRAPH
16
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

docx5

docx6docx7

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

đầ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_PARAGRAPH
46import
from docx.enum.text import WD_ALIGN_PARAGRAPH
48

 

# Create an instance of a word document

from docx.enum.text import WD_ALIGN_PARAGRAPH
0
from docx.enum.text import WD_ALIGN_PARAGRAPH
1
from docx.enum.text import WD_ALIGN_PARAGRAPH
2

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
3

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH
7
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
59

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______61
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx7
from docx.enum.text import WD_ALIGN_PARAGRAPH
16
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

from docx.enum.text import WD_ALIGN_PARAGRAPH
70
from docx.enum.text import WD_ALIGN_PARAGRAPH
1
from docx.enum.text import WD_ALIGN_PARAGRAPH
72

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
73

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______75
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx7
from docx.enum.text import WD_ALIGN_PARAGRAPH
16
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

from docx.enum.text import WD_ALIGN_PARAGRAPH
70
from docx.enum.text import WD_ALIGN_PARAGRAPH
1
from docx.enum.text import WD_ALIGN_PARAGRAPH
86

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
87

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______89
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx7
from docx.enum.text import WD_ALIGN_PARAGRAPH
16
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

from docx.enum.text import WD_ALIGN_PARAGRAPH
70
from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx00

 

# Import docx NOT python-docx01

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______182_______03
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx7
from docx.enum.text import WD_ALIGN_PARAGRAPH
16
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

from docx.enum.text import WD_ALIGN_PARAGRAPH
70
from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx14

 

# Import docx NOT python-docx15

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______182_______17
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx7
from docx.enum.text import WD_ALIGN_PARAGRAPH
16
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

from docx.enum.text import WD_ALIGN_PARAGRAPH
70
from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx28

 

docx5

docx6docx7

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

đầ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.sharedimport Inches

 

# Create an instance of a word document

from docx.enum.text import WD_ALIGN_PARAGRAPH
0
from docx.enum.text import WD_ALIGN_PARAGRAPH
1
from docx.enum.text import WD_ALIGN_PARAGRAPH
2

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
3

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH
7
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

# Import docx NOT python-docx50

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______182_______52
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx58# Import docx NOT python-docx59 # Import docx NOT python-docx60

# Import docx NOT python-docx61 # Import docx NOT python-docx62# Import docx NOT python-docx63 # Import docx NOT python-docx64# Import docx NOT python-docx65# Import docx NOT python-docx66

# Import docx NOT python-docx67# Import docx NOT python-docx63 # Import docx NOT python-docx69

# Import docx NOT python-docx70_______1_______1 import3import4

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

# Import docx NOT python-docx75

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______182_______77
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx58# Import docx NOT python-docx59 # Import docx NOT python-docx85

# Import docx NOT python-docx61 # Import docx NOT python-docx62# Import docx NOT python-docx63 # Import docx NOT python-docx64# Import docx NOT python-docx65# Import docx NOT python-docx91

# Import docx NOT python-docx67# Import docx NOT python-docx63 # Import docx NOT python-docx69

# Import docx NOT python-docx95

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 import3import4
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

docx5

docx6docx7

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

đầ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.sharedimport Inches

 

# Create an instance of a word document

from docx.enum.text import WD_ALIGN_PARAGRAPH
0
from docx.enum.text import WD_ALIGN_PARAGRAPH
1
from docx.enum.text import WD_ALIGN_PARAGRAPH
2

 

from docx.enum.text import WD_ALIGN_PARAGRAPH
3

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______1_______5_______1_______6
from docx.enum.text import WD_ALIGN_PARAGRAPH
7
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

import21

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______182_______52
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx58# Import docx NOT python-docx59 # Import docx NOT python-docx60

# Import docx NOT python-docx61 # Import docx NOT python-docx62# Import docx NOT python-docx63 # Import docx NOT python-docx64# Import docx NOT python-docx65# Import docx NOT python-docx91

# Import docx NOT python-docx67# Import docx NOT python-docx63 # Import docx NOT python-docx69

# Import docx NOT python-docx70_______1_______1 import3# Import docx NOT python-docx65import4

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

import47

from docx.enum.text import WD_ALIGN_PARAGRAPH
4_______182_______77
from docx.enum.text import WD_ALIGN_PARAGRAPH
6# Import docx NOT python-docx3
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

# Import docx NOT python-docx5

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 # Import docx NOT python-docx58# Import docx NOT python-docx59 # Import docx NOT python-docx85

# Import docx NOT python-docx61 # Import docx NOT python-docx62# Import docx NOT python-docx63 # Import docx NOT python-docx64# Import docx NOT python-docx65# Import docx NOT python-docx91

# Import docx NOT python-docx67# Import docx NOT python-docx63 # Import docx NOT python-docx69

# Import docx NOT python-docx95

from docx.enum.text import WD_ALIGN_PARAGRAPH
1 import3# Import docx NOT python-docx65import4
from docx.enum.text import WD_ALIGN_PARAGRAPH
8

 

docx5

docx6docx7

from docx.enum.text import WD_ALIGN_PARAGRAPH
8

đầ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

Chủ Đề