Function to impose pages for the back side (even pages), reversed and rotated
code
def impose_back_pages(pages, arrangement):
page_index = 0
total_pages_local = len(pages)
# Lặp qua các trang chẵn
while page_index < total_pages_local:
new_page = back_doc.new_page(width=new_page_width_pt, height=new_page_height_pt)
arranged_rects = []
# Tính toán x_offset và y_offset để căn giữa nội dung
total_grid_width = arrangement['entries_per_row'] * arrangement['entry_width']
total_grid_height = arrangement['entries_per_column'] * arrangement['entry_height']
x_offset = (new_page_width_pt - total_grid_width) / 2
y_offset = (new_page_height_pt - total_grid_height) / 2
# Lặp qua các dòng từ dưới lên trên
for row in reversed(range(arrangement['entries_per_column'])):
y = y_offset + row * arrangement['entry_height']
# Lặp qua các cột từ trái sang phải
for col in range(arrangement['entries_per_row']):
x = x_offset + col * arrangement['entry_width']
if page_index >= total_pages_local:
break
pno = pages[page_index]
rect = fitz.Rect(
x,
y,
x + arrangement['entry_width'],
y + arrangement['entry_height']
)
if arrangement['rotate']:
rotation = (90 + 180) % 360 # Xoay 270 độ
else:
rotation = 180 # Xoay 180 độ
new_page.show_pdf_page(rect, doc, pno=pno, rotate=rotation)
arranged_rects.append((rect, row, col))
page_index += 1
if page_index >= total_pages_local:
break đọc mã này tôi muốn nó không xoay 90 độ và đảo ngược ví trị các hàng thay vì các cppjr đang từ trái sang phải sẽ đổi thành từ phải sang trái
Assistant
Để đạt được những thay đổi mà bạn mong muốn, bạn cần chỉnh sửa hàm impose_back_pages như sau:
Loại bỏ việc xoay 90 độ:
Trong mã của bạn, dòng sau đang xoay trang 270 độ (90 + 180 = 270):
python
rotation = (90 + 180) % 360 # Xoay 270 độ
Nếu bạn không muốn xoay 90 độ, bạn có thể loại bỏ phần cộng 90 và chỉ xoay 180 độ:
python
rotation = 180 # Xoay 180 độ
Hoặc nếu bạn không muốn xoay trang, đặt rotation thành 0:
python
rotation = 0 # Không xoay
Đảo ngược vị trí các cột để sắp xếp từ phải sang trái:
Hiện tại, mã của bạn đang lặp qua các cột từ trái sang phải:
python
for col in range(arrangement['entries_per_row']):
Để đảo ngược thứ tự cột từ phải sang trái, bạn có thể dùng hàm reversed() để lặp qua các cột theo thứ tự ngược lại:
python
for col in reversed(range(arrangement['entries_per_row'])):
Dưới đây là mã đã được chỉnh sửa theo yêu cầu của bạn:
python
def impose_back_pages(pages, arrangement):
page_index = 0
total_pages_local = len(pages)
# Lặp qua các trang chẵn
while page_index < total_pages_local:
new_page = back_doc.new_page(width=new_page_width_pt, height=new_page_height_pt)
arranged_rects = []
# Tính toán x_offset và y_offset để căn giữa nội dung
total_grid_width = arrangement['entries_per_row'] * arrangement['entry_width']
total_grid_height = arrangement['entries_per_column'] * arrangement['entry_height']
x_offset = (new_page_width_pt - total_grid_width) / 2
y_offset = (new_page_height_pt - total_grid_height) / 2
# Lặp qua các dòng từ dưới lên trên
for row in reversed(range(arrangement['entries_per_column'])):
y = y_offset + row * arrangement['entry_height']
# Lặp qua các cột từ phải sang trái
for col in reversed(range(arrangement['entries_per_row'])):
x = x_offset + col * arrangement['entry_width']
if page_index >= total_pages_local:
break
pno = pages[page_index]
rect = fitz.Rect(
x,
y,
x + arrangement['entry_width'],
y + arrangement['entry_height']
)
# Loại bỏ xoay 90 độ
if arrangement['rotate']:
rotation = 180 # Xoay 180 độ
else:
rotation = 0 # Không xoay
new_page.show_pdf_page(rect, doc, pno=pno, rotate=rotation)
arranged_rects.append((rect, row, col))
page_index += 1
if page_index >= total_pages_local:
break
Giải thích chi tiết các thay đổi:
Loại bỏ xoay 90 độ:
Thay đổi dòng tính toán rotation để không còn cộng 90 độ.
Nếu arrangement['rotate'] là True, chỉ xoay 180 độ; nếu False, không xoay.
Đảo ngược thứ tự cột:
Sử dụng reversed() trong vòng lặp cột để lặp từ cột cuối về cột đầu, tức là từ phải sang trái.
Với các thay đổi này, mã của bạn sẽ không còn xoay 90 độ và các trang sẽ được sắp xếp từ phải sang trái như bạn mong muốn.
Hy vọng điều này giúp ích cho bạn! Nếu bạn có thêm câu hỏi hoặc cần hỗ trợ thêm, xin hãy cho tôi biết.