Làm cách nào để xuất sang pdf trong python?
Đôi khi, bạn có thể cần thêm chức năng 'xuất sang pdf' vào ứng dụng hoặc trang web python của mình. Trong bài viết này, chúng ta sẽ xem xét cách xuất dữ liệu sang PDF bằng python. Bạn có thể sử dụng các bước này để xuất HTML sang PDF hoặc thậm chí là văn bản thuần túy sang PDF Show
Cách tạo tệp PDF bằng PythonChúng tôi sẽ sử dụng thư viện fpdf để tạo PDF. Nó là viết tắt của pdf miễn phí và thường được sử dụng trong các ứng dụng python để tạo tệp pdf Bạn có thể cài đặt nó bằng lệnh sau $ sudo pip install fpdf 1. Tạo tệp PDF từ văn bảnTrong trường hợp này, chúng tôi sẽ nhập thư viện fpdf, thêm một trang, đặt phông chữ của nó và chèn một ô có văn bản của chúng tôi vào đó. Cuối cùng, chúng tôi lưu nó dưới dạng tệp pdf #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf") 2. Lưu tệp văn bản dưới dạng PDFĐôi khi bạn có thể cần chuyển đổi tệp văn bản thành PDF. Bạn cũng có thể làm điều này với fpdf. Trong trường hợp này, chúng tôi sẽ lặp qua tệp văn bản của mình, từng dòng một và thêm từng dòng một vào tệp pdf của chúng tôi, trước khi lưu nó. Trong ví dụ trên, chúng tôi đã thêm hai dòng vào tệp PDF của mình. Đây chỉ là một phần mở rộng của nó. Giả sử bạn muốn lưu dữ liệu tệp văn bản. txt thành dữ liệu. pdf. Đây là một ví dụ để tạo tệp pdf từ nó #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) 3. Chuyển đổi trang web sang PDFNếu bạn muốn chuyển đổi trang web/HTML sang PDF thì chúng tôi khuyên bạn nên sử dụng pdfkit thay thế. Chúng tôi đã cung cấp hướng dẫn từng bước để chuyển đổi trang web sang PDF bằng pdfkit. Đây là cách chuyển đổi trang web sang PDF 4. Chuyển đổi DocX sang PDFNếu bạn muốn chuyển đổi tệp DocX sang PDF thì nên sử dụng docx2pdf được xây dựng riêng cho mục đích này Chúng tôi đã cung cấp hướng dẫn chi tiết về cách chuyển đổi tệp DocX sang tài liệu PDF. Đây là cách chuyển đổi tệp DocX sang PDF Như bạn có thể thấy, python là một ngôn ngữ rất linh hoạt cho phép bạn chuyển đổi các loại dữ liệu đầu vào khác nhau sang tài liệu PDF. Xuất sang PDF là một chức năng hữu ích và được tìm kiếm trong hầu hết các trang web và ứng dụng. Bạn có thể sử dụng bất kỳ phương pháp nào ở trên để tạo PDF từ dữ liệu của mình và chia sẻ nó với người dùng của mình Thật hữu ích khi biết cách tạo và sửa đổi các tệp PDF trong Python. PDF, hay Định dạng Tài liệu Di động, là một trong những định dạng phổ biến nhất để chia sẻ tài liệu qua Internet. Các tệp PDF có thể chứa văn bản, hình ảnh, bảng, biểu mẫu và đa phương tiện như video và hoạt ảnh, tất cả trong một tệp Sự phong phú của các loại nội dung này có thể khiến việc làm việc với các tệp PDF trở nên khó khăn. Có rất nhiều loại dữ liệu khác nhau cần giải mã khi mở file PDF. May mắn thay, hệ sinh thái Python có một số gói tuyệt vời để đọc, thao tác và tạo tệp PDF Trong hướng dẫn này, bạn sẽ học cách
Ghi chú. Hướng dẫn này được điều chỉnh từ chương “Tạo và sửa đổi tệp PDF” trong Python Basics. Giới thiệu thực tế về Python 3 Cuốn sách sử dụng trình soạn thảo IDLE tích hợp sẵn của Python để tạo và chỉnh sửa các tệp Python cũng như tương tác với trình bao Python, do đó, thỉnh thoảng bạn sẽ thấy các tham chiếu đến IDLE trong suốt hướng dẫn này. Tuy nhiên, bạn sẽ không gặp vấn đề gì khi chạy mã ví dụ từ trình chỉnh sửa và môi trường bạn chọn Đồng thời, bạn sẽ có một số cơ hội để hiểu sâu hơn bằng cách làm theo các ví dụ. Bạn có thể tải xuống các tài liệu được sử dụng trong các ví dụ bằng cách nhấp vào liên kết bên dưới Tải tài liệu mẫu. Nhấp vào đây để lấy tài liệu bạn sẽ sử dụng để tìm hiểu về cách tạo và sửa đổi tệp PDF trong hướng dẫn này Trích xuất văn bản từ PDFTrong phần này, bạn sẽ học cách đọc tệp PDF và trích xuất văn bản bằng gói 26. Tuy nhiên, trước khi bạn có thể làm điều đó, bạn cần cài đặt nó với 27
Xác minh cài đặt bằng cách chạy lệnh sau trong thiết bị đầu cuối của bạn
Đặc biệt chú ý đến thông tin phiên bản. Tại thời điểm viết bài này, phiên bản mới nhất của 26 là 29. Nếu bạn đang mở IDLE, thì bạn cần phải khởi động lại nó trước khi có thể sử dụng gói 26Loại bỏ các quảng cáoMở tệp PDFHãy bắt đầu bằng cách mở một tệp PDF và đọc một số thông tin về nó. Bạn sẽ sử dụng tệp 31 nằm trong thư mục 32 trong kho lưu trữ đồng hànhMở cửa sổ tương tác của IDLE và nhập lớp 33 từ gói 26>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")4 Để tạo một phiên bản mới của lớp 33, bạn sẽ cần đường dẫn đến tệp PDF mà bạn muốn mở. Hãy làm điều đó ngay bây giờ bằng cách sử dụng mô-đun 36>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")7 Biến 37 hiện chứa đường dẫn đến phiên bản PDF của Jane Austen’s Pride and PrejudiceGhi chú. Bạn có thể cần thay đổi 37 để nó tương ứng với vị trí của thư mục 39 trên máy tính của bạnBây giờ hãy tạo phiên bản 33>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)2 Bạn chuyển đổi 37 thành chuỗi vì 33 không biết cách đọc từ đối tượng 43Nhớ lại từ chương 12, “Đầu vào và đầu ra tệp”, rằng tất cả các tệp đang mở phải được đóng trước khi chương trình kết thúc. Đối tượng 33 làm tất cả những điều này cho bạn, vì vậy bạn không cần phải lo lắng về việc mở hoặc đóng tệp PDFBây giờ bạn đã tạo một phiên bản 33, bạn có thể sử dụng nó để thu thập thông tin về PDF. Ví dụ: 46 trả về số trang có trong tệp PDF>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)9 Lưu ý rằng 46 được viết bằng mixCase, không phải Lower_case_with_underscores như khuyến nghị trong PEP 8. Hãy nhớ rằng, PEP 8 là một bộ hướng dẫn, không phải quy tắc. Đối với Python, mixCase hoàn toàn có thể chấp nhận đượcGhi chú. 26 được điều chỉnh từ gói 49. 49 được viết vào năm 2005, chỉ bốn năm sau khi PEP 8 được xuất bảnVào thời điểm đó, nhiều lập trình viên Python đang chuyển từ các ngôn ngữ mà mixCase phổ biến hơn Bạn cũng có thể truy cập một số thông tin tài liệu bằng thuộc tính 51>>> 5Đối tượng được trả về bởi 51 trông giống như một cuốn từ điển, nhưng nó không thực sự giống như vậy. Bạn có thể truy cập từng mục trong 51 dưới dạng thuộc tínhVí dụ: để lấy tiêu đề, hãy sử dụng thuộc tính 54>>> 9Đối tượng 51 chứa siêu dữ liệu PDF, được đặt khi tạo PDFLớp 33 cung cấp tất cả các phương thức và thuộc tính cần thiết mà bạn cần để truy cập dữ liệu trong tệp PDF. Hãy khám phá những gì bạn có thể làm với tệp PDF và cách bạn có thể làm điều đóLoại bỏ các quảng cáoTrích xuất văn bản từ một trangCác trang PDF được biểu diễn bằng 26 với lớp 58. Bạn sử dụng phiên bản 58 để tương tác với các trang trong tệp PDF. Bạn không cần trực tiếp tạo phiên bản 58 của riêng mình. Thay vào đó, bạn có thể truy cập chúng thông qua phương thức 62 của đối tượng 33Có hai bước để trích xuất văn bản từ một trang PDF
31 có 68 trang. Mỗi trang có một chỉ mục giữa 69 và 70. Bạn có thể lấy 58 đại diện cho một trang cụ thể bằng cách chuyển chỉ mục của trang tới 64>>> 8 62 trả về một 58>>> 1Bạn có thể trích xuất văn bản của trang bằng 75>>> 0Lưu ý rằng đầu ra hiển thị ở đây đã được định dạng để phù hợp hơn trên trang này. Đầu ra bạn thấy trên máy tính của mình có thể được định dạng khác Mỗi đối tượng 33 có một thuộc tính 77 mà bạn có thể sử dụng để lặp qua tất cả các trang trong PDF theo thứ tựVí dụ: vòng lặp 78 sau đây in văn bản từ mọi trang trong PDF Kiêu hãnh và định kiến>>> 1Hãy kết hợp mọi thứ bạn đã học và viết một chương trình trích xuất tất cả văn bản từ tệp 31 và lưu nó vào tệp 80Để tất cả chúng cùng nhauMở một cửa sổ soạn thảo mới trong IDLE và nhập đoạn mã sau 2Hãy phá vỡ điều đó
Khi bạn lưu và chạy chương trình, nó sẽ tạo một tệp mới trong thư mục chính của bạn có tên là 84 chứa toàn bộ nội dung của tài liệu 31. Mở nó lên và kiểm tra xem nó raLoại bỏ các quảng cáoKiểm tra việc hiểu của bạnMở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn Tập thể dục. In văn bản từ PDFHiển thị/Ẩn Trong thư mục 32 trong kho lưu trữ đồng hành cho bài viết này, có một tệp tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")401. Tạo một phiên bản 33 để đọc PDF và sử dụng nó để in văn bản từ trang đầu tiênBạn có thể mở rộng khối bên dưới để xem giải pháp Giải pháp. In văn bản từ PDFHiển thị/Ẩn Thiết lập đường dẫn đến tệp PDF 3Bây giờ bạn có thể tạo phiên bản 33 4Hãy nhớ rằng các đối tượng 33 chỉ có thể được khởi tạo bằng chuỗi đường dẫn, không phải đối tượng 83Bây giờ, hãy sử dụng 62 để lấy trang đầu tiên 5Hãy nhớ rằng, các trang được lập chỉ mục bắt đầu bằng 0 Sau đó sử dụng 66 để trích xuất văn bản 6Cuối cùng, in văn bản 7Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo Trích xuất các trang từ PDFTrong phần trước, bạn đã học cách trích xuất toàn bộ văn bản từ tệp PDF và lưu vào tệp 80. Bây giờ, bạn sẽ tìm hiểu cách trích xuất một trang hoặc nhiều trang từ một tệp PDF hiện có và lưu chúng vào một tệp PDF mớiBạn có thể sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 để tạo tệp PDF mới. Hãy cùng khám phá lớp học này và tìm hiểu các bước cần thiết để tạo PDF bằng cách sử dụng 26Sử dụng lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf") 409Lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 tạo tệp PDF mới. Trong cửa sổ tương tác của IDLE, nhập lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 và tạo một phiên bản mới có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 >>> 8Các đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 giống như các tệp PDF trống. Bạn cần thêm một số trang vào chúng trước khi có thể lưu chúng vào tệp Hãy tiếp tục và thêm một trang trống vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 >>> 9Các tham số #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")417 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")418 là bắt buộc và xác định kích thước của trang theo đơn vị gọi là điểm. Một điểm bằng 1/72 inch, vì vậy đoạn mã trên thêm một trang trống một inch vuông vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")420 trả về một phiên bản 58 mới đại diện cho trang mà bạn đã thêm vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")40 Trong ví dụ này, bạn đã chỉ định thể hiện 58 được trả về bởi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")420 cho biến 95, nhưng trong thực tế, bạn thường không cần phải làm điều này. Nghĩa là, bạn thường gọi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")420 mà không gán giá trị trả về cho bất kỳ thứ gì >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")41 Để ghi nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 vào tệp PDF, hãy chuyển một đối tượng tệp ở chế độ ghi nhị phân sang #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")428 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")42 Thao tác này tạo một tệp mới trong thư mục làm việc hiện tại của bạn có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")429. Nếu bạn mở tệp bằng trình đọc PDF, chẳng hạn như Adobe Acrobat, thì bạn sẽ thấy tài liệu có một trang trống một inch vuông Chi tiết kỹ thuật. Lưu ý rằng bạn lưu tệp PDF bằng cách chuyển đối tượng tệp sang phương thức #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")431 của đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409, chứ không phải phương thức #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")431 của đối tượng tệp Đặc biệt, đoạn mã sau sẽ không hoạt động >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")43 Cách tiếp cận này có vẻ lạc hậu đối với nhiều lập trình viên mới, vì vậy hãy chắc chắn rằng bạn tránh được sai lầm này Các đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 có thể ghi vào các tệp PDF mới, nhưng chúng không thể tạo nội dung mới từ đầu ngoài các trang trống Đây có vẻ là một vấn đề lớn, nhưng trong nhiều trường hợp, bạn không cần phải tạo nội dung mới. Thông thường, bạn sẽ làm việc với các trang được trích xuất từ tệp PDF mà bạn đã mở bằng phiên bản 33Ghi chú. Bạn sẽ tìm hiểu cách tạo tệp PDF từ đầu bên dưới, trong phần “Tạo tệp PDF từ đầu. ” Trong ví dụ bạn đã thấy ở trên, có ba bước để tạo một tệp PDF mới bằng cách sử dụng 26
Bạn sẽ thấy mẫu này lặp đi lặp lại khi tìm hiểu nhiều cách khác nhau để thêm trang vào phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409Loại bỏ các quảng cáo Trích xuất một trang từ PDFHãy cùng xem lại bản PDF Kiêu hãnh và định kiến mà bạn đã làm việc trong phần trước. Bạn sẽ mở tệp PDF, trích xuất trang đầu tiên và tạo một tệp PDF mới chỉ chứa một trang được trích xuất duy nhất Mở cửa sổ tương tác của IDLE và nhập 33 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 từ 26 cũng như lớp 83 từ mô-đun 36>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")44 Bây giờ hãy mở tệp 31 bằng phiên bản 33>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")45 Chuyển chỉ số 69 đến 62 để nhận được một 58 đại diện cho trang đầu tiên của PDF>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")46 Bây giờ hãy tạo một phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 mới và thêm #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")451 vào đó với #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")452 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")47 Phương thức #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")452 thêm một trang vào tập hợp các trang trong đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414, giống như #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")420. Sự khác biệt là nó yêu cầu một 58 hiện cóBây giờ hãy ghi nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 vào một tệp mới >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")48 Bây giờ bạn có một tệp PDF mới được lưu trong thư mục làm việc hiện tại của bạn có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")458, chứa trang bìa của tệp 31. khá gọn gàngTrích xuất nhiều trang từ một tệp PDFHãy trích xuất chương đầu tiên từ 31 và lưu nó vào một tệp PDF mớiNếu bạn mở 31 bằng trình xem PDF, thì bạn có thể thấy chương đầu tiên nằm ở trang thứ hai, thứ ba và thứ tư của PDF. Vì các trang được lập chỉ mục bắt đầu bằng 69, nên bạn sẽ cần trích xuất các trang ở chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")464 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")465 Bạn có thể thiết lập mọi thứ bằng cách nhập các lớp bạn cần và mở tệp PDF >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")49 Mục tiêu của bạn là trích xuất các trang tại các chỉ số #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")464 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")465, thêm các trang này vào một phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 mới, sau đó ghi chúng vào một tệp PDF mới Một cách để thực hiện điều này là lặp qua phạm vi số bắt đầu từ ________ 1463 và kết thúc ở ________ 1465, trích xuất trang ở mỗi bước của vòng lặp và thêm nó vào phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")70 Vòng lặp lặp qua các số #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")464 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")465 vì #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")476 không bao gồm điểm cuối bên phải. Ở mỗi bước trong vòng lặp, trang tại chỉ mục hiện tại được trích xuất bằng 62 và được thêm vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 bằng cách sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")452 Bây giờ #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 có ba trang, bạn có thể kiểm tra với 46>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")71 Cuối cùng, bạn có thể ghi các trang được trích xuất vào một tệp PDF mới >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")72 Bây giờ bạn có thể mở tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")482 trong thư mục làm việc hiện tại của mình để chỉ đọc chương đầu tiên của Kiêu hãnh và định kiến Một cách khác để trích xuất nhiều trang từ PDF là tận dụng lợi thế của việc #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")483 hỗ trợ ký hiệu lát cắt. Hãy làm lại ví dụ trước bằng cách sử dụng 77 thay vì lặp qua một đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")485 Bắt đầu bằng cách khởi tạo một đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 mới >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")73 Bây giờ lặp qua một lát của 77 từ các chỉ số bắt đầu từ #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463 và kết thúc tại #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")489 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")74 Hãy nhớ rằng các giá trị trong một lát nằm trong phạm vi từ mục ở chỉ mục đầu tiên trong lát cho đến, nhưng không bao gồm, mục ở chỉ mục thứ hai trong lát. Vì vậy, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")490 trả về một iterable chứa các trang có chỉ số #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")464 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")465 Cuối cùng, ghi nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 vào tệp đầu ra >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")75 Bây giờ hãy mở tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")495 trong thư mục làm việc hiện tại của bạn và so sánh nó với tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")482 mà bạn đã tạo bằng cách lặp qua đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")485. Chúng chứa các trang giống nhau Đôi khi bạn cần trích xuất mọi trang từ PDF. Bạn có thể sử dụng các phương pháp được minh họa ở trên để thực hiện việc này, nhưng 26 cung cấp một lối tắt. Phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 có phương thức #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")700 mà bạn có thể sử dụng để nối các trang từ phiên bản 33Để sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")700, hãy truyền một phiên bản 33 cho tham số #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")704 của phương thức. Ví dụ: đoạn mã sau sao chép mọi trang từ PDF Kiêu hãnh và Định kiến sang phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")76 #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 hiện chứa mọi trang trong 82Loại bỏ các quảng cáoKiểm tra việc hiểu của bạnMở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn Tập thể dục. Trích xuất trang cuối cùng của PDFHiển thị/Ẩn Trích xuất trang cuối cùng từ tệp 31 và lưu nó vào một tệp mới có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")709 trong thư mục chính của bạn Bạn có thể mở rộng khối bên dưới để xem giải pháp Giải pháp. Trích xuất trang cuối cùng của PDFHiển thị/Ẩn Thiết lập đường dẫn đến tệp 31#import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")77 Bây giờ bạn có thể tạo phiên bản 33 4Hãy nhớ rằng các đối tượng 33 chỉ có thể được khởi tạo bằng chuỗi đường dẫn, không phải đối tượng 83Sử dụng thuộc tính 77 để có thể lặp lại trên tất cả các trang trong PDF. Trang cuối cùng có thể được truy cập với chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")715 #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")79 Bây giờ bạn có thể tạo một phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 và thêm trang cuối cùng vào đó #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)20 Cuối cùng, ghi nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 vào tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")709 trong thư mục chính của bạn #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)21 Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo Nối và hợp nhất các tệp PDFHai tác vụ phổ biến khi làm việc với tệp PDF là nối và hợp nhất nhiều tệp PDF thành một tệp duy nhất Khi bạn nối hai hoặc nhiều tệp PDF, bạn nối các tệp này với nhau thành một tài liệu duy nhất. Ví dụ: một công ty có thể kết hợp nhiều báo cáo hàng ngày thành một báo cáo hàng tháng vào cuối tháng Hợp nhất hai tệp PDF cũng kết hợp các tệp PDF thành một tệp duy nhất. Nhưng thay vì nối tệp PDF thứ hai vào cuối tệp PDF đầu tiên, việc hợp nhất cho phép bạn chèn tệp đó vào sau một trang cụ thể trong tệp PDF đầu tiên. Sau đó, nó sẽ đẩy tất cả các trang của tệp PDF đầu tiên sau điểm chèn vào cuối tệp PDF thứ hai Trong phần này, bạn sẽ tìm hiểu cách ghép nối và hợp nhất các tệp PDF bằng cách sử dụng gói #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 của gói 26Sử dụng lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf") 720Lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 rất giống với lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 mà bạn đã học ở phần trước. Bạn có thể sử dụng cả hai lớp để ghi tệp PDF. Trong cả hai trường hợp, bạn thêm các trang vào các thể hiện của lớp và sau đó ghi chúng vào một tệp Sự khác biệt chính giữa hai loại này là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 chỉ có thể nối thêm hoặc nối các trang vào cuối danh sách các trang đã có trong trình soạn thảo, trong khi đó #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 có thể chèn hoặc nối các trang ở bất kỳ vị trí nào Hãy tiếp tục và tạo phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 đầu tiên của bạn. Trong cửa sổ tương tác của IDLE, hãy nhập mã sau để nhập lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 và tạo một phiên bản mới >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)22 Các đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 trống khi chúng được khởi tạo lần đầu tiên. Bạn sẽ cần thêm một số trang vào đối tượng của mình trước khi có thể làm bất cứ điều gì với nó Có một số cách để thêm trang vào đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")729 và cách bạn sử dụng tùy thuộc vào những gì bạn cần thực hiện
Bạn sẽ xem xét cả hai phương pháp trong phần này, bắt đầu với #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")730Loại bỏ các quảng cáo Nối các tệp PDF với #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf") 730Thư mục 32 có một thư mục con có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")737 chứa ba báo cáo chi phí cho một nhân viên tên là Peter Python Peter cần nối ba tệp PDF này và gửi chúng cho chủ lao động của mình dưới dạng một tệp PDF duy nhất để anh ấy có thể được hoàn trả một số chi phí liên quan đến công việc Bạn có thể bắt đầu bằng cách sử dụng mô-đun 36 để nhận danh sách các đối tượng 83 cho từng báo cáo trong số ba báo cáo chi phí trong thư mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")740 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)23 Sau khi nhập lớp 83, bạn cần xây dựng đường dẫn đến thư mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")740. Lưu ý rằng bạn có thể cần thay đổi mã ở trên để có được đường dẫn chính xác trên máy tính của mình Khi bạn có đường dẫn đến thư mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")740 được gán cho biến #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")744, bạn có thể sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")745 để có thể lặp lại đường dẫn đến tệp PDF trong thư mục Hãy xem những gì trong thư mục >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)24 Tên của ba tệp được liệt kê, nhưng chúng không theo thứ tự. Hơn nữa, thứ tự của các tệp bạn thấy trong đầu ra trên máy tính của bạn có thể không khớp với đầu ra được hiển thị ở đây Nói chung, thứ tự của các đường dẫn được trả về bởi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")745 không được đảm bảo, vì vậy bạn cần phải tự sắp xếp chúng. Bạn có thể làm điều này bằng cách tạo một danh sách chứa ba đường dẫn tệp và sau đó gọi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")747 trong danh sách đó >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)25 Hãy nhớ rằng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")747 sắp xếp danh sách tại chỗ, vì vậy bạn không cần gán giá trị trả về cho một biến. Danh sách #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")737 sẽ được sắp xếp theo thứ tự abc theo tên tệp sau khi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")750 được gọi Để xác nhận rằng việc sắp xếp đã hoạt động, hãy lặp lại #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")737 và in ra tên tệp >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)26 Trông tôt đây Bây giờ bạn có thể nối ba tệp PDF. Để làm điều đó, bạn sẽ sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")752, yêu cầu một đối số chuỗi biểu thị đường dẫn đến tệp PDF. Khi bạn gọi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")730, tất cả các trang trong tệp PDF được thêm vào tập hợp các trang trong đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 Hãy xem điều này trong hành động. Đầu tiên, nhập lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 và tạo một thể hiện mới >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)22 Bây giờ lặp qua các đường dẫn trong danh sách #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")737 đã sắp xếp và nối chúng vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")729 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)28 Lưu ý rằng mỗi đối tượng 83 trong #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")740 được chuyển đổi thành một chuỗi có #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")760 trước khi được chuyển đến #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")761 Với tất cả các tệp PDF trong thư mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")740 được nối trong đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")729, điều cuối cùng bạn cần làm là ghi mọi thứ vào tệp PDF đầu ra. Phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 có phương thức #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")431 hoạt động giống như phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")438 Mở một tệp mới ở chế độ ghi nhị phân, sau đó chuyển đối tượng tệp sang phương thức #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")767 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)29 Bây giờ bạn có một tệp PDF trong thư mục làm việc hiện tại của mình có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")768. Mở nó ra bằng một trình đọc PDF và bạn sẽ tìm thấy cả ba báo cáo chi phí trong cùng một tệp PDFLoại bỏ các quảng cáo Hợp nhất các tệp PDF với #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf") 732Để hợp nhất hai hoặc nhiều tệp PDF, hãy sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")770. Phương pháp này tương tự như #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")730, ngoại trừ việc bạn phải chỉ định vị trí trong tệp PDF đầu ra để chèn tất cả nội dung từ tệp PDF mà bạn đang hợp nhất Hãy xem một ví dụ. Goggle, Inc. chuẩn bị báo cáo quý nhưng quên kèm theo mục lục. Peter Python đã nhận thấy lỗi và nhanh chóng tạo một tệp PDF với mục lục bị thiếu. Bây giờ anh ấy cần hợp nhất tệp PDF đó vào báo cáo gốc Có thể tìm thấy cả PDF báo cáo và mục lục PDF trong thư mục con #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")772 của thư mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")773. Báo cáo nằm trong một tệp có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")774 và mục lục nằm trong một tệp có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")775 Trong cửa sổ tương tác của IDLE, nhập lớp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 và tạo các đối tượng 83 cho các tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")774 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")775 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)90 Điều đầu tiên bạn sẽ làm là thêm báo cáo PDF vào phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 mới bằng cách sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")730 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)91 Giờ đây, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")729 có một số trang trong đó, bạn có thể hợp nhất mục lục PDF vào đó ở đúng vị trí. Nếu bạn mở tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")774 bằng trình đọc PDF, thì bạn sẽ thấy trang đầu tiên của báo cáo là trang tiêu đề. Trang thứ hai là phần giới thiệu và các trang còn lại chứa các phần báo cáo khác nhau Bạn muốn chèn mục lục sau trang tiêu đề và ngay trước phần giới thiệu. Vì các chỉ mục trang PDF bắt đầu bằng 69 trong 26, bạn cần chèn mục lục sau trang có chỉ mục 69 và trước trang có chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463 Để làm điều đó, hãy gọi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")788 với hai đối số
Đây là những gì trông giống như >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)92 Mỗi trang trong mục lục PDF được chèn vào trước trang ở chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463. Vì mục lục PDF chỉ có một trang nên nó được chèn vào chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463. Trang hiện đang ở chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463 sau đó được chuyển sang chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")464. Trang hiện đang ở chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")464 được chuyển sang chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")465, v.v. Bây giờ hãy viết tệp PDF đã hợp nhất thành một tệp đầu ra >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)93 Bây giờ bạn có tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")796 trong thư mục làm việc hiện tại của mình. Mở nó lên bằng trình đọc PDF và kiểm tra xem mục lục đã được chèn vào đúng vị trí chưa Nối và hợp nhất các tệp PDF là các hoạt động phổ biến. Mặc dù phải thừa nhận rằng các ví dụ trong phần này hơi giả tạo, nhưng bạn có thể hình dung một chương trình sẽ hữu ích như thế nào khi hợp nhất hàng nghìn tệp PDF hoặc để tự động hóa các tác vụ thông thường mà nếu không thì con người sẽ mất rất nhiều thời gian để hoàn thành Kiểm tra việc hiểu của bạnMở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn Tập thể dục. Nối hai tệp PDFHiển thị/Ẩn Trong thư mục 32 trong kho lưu trữ đồng hành cho bài viết này, có hai tệp có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")798 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")799 Sử dụng phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)200, nối hai tệp bằng cách sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")730. Lưu các tệp PDF được nối vào một tệp mới có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)202 nếu thư mục chính trên máy tính của bạn Bạn có thể mở rộng khối bên dưới để xem giải pháp Giải pháp. Nối hai tệp PDFHiển thị/Ẩn Thiết lập đường dẫn đến tệp PDF #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)94 Bây giờ bạn có thể tạo phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")720 #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)95 Bây giờ, lặp qua các đường dẫn trong mỗi tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)204 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")730 đến #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")729 #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)96 Cuối cùng, viết nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")729 vào một tệp có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)202 trong thư mục chính của bạn #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)97 Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo Loại bỏ các quảng cáoXoay và cắt xén trang PDFCho đến nay, bạn đã học cách trích xuất văn bản và trang từ PDF cũng như cách nối và hợp nhất hai hoặc nhiều tệp PDF. Đây là tất cả các thao tác phổ biến với PDF, nhưng 26 có nhiều tính năng hữu ích khácGhi chú. Hướng dẫn này được điều chỉnh từ chương “Tạo và sửa đổi tệp PDF” trong Python Basics. Giới thiệu thực tế về Python 3. Nếu bạn thích những gì bạn đang đọc, thì hãy nhớ xem phần còn lại của cuốn sách Trong phần này, bạn sẽ học cách xoay và cắt các trang trong tệp PDF Xoay trangBạn sẽ bắt đầu bằng cách học cách xoay trang. Đối với ví dụ này, bạn sẽ sử dụng tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)210 trong thư mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")773. Tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)210 chứa một phiên bản đáng yêu của tác phẩm Vịt con xấu xí của Hans Christian Andersen, ngoại trừ việc mỗi trang đánh số lẻ đều được xoay ngược chiều kim đồng hồ 90 độ Hãy khắc phục điều đó. Trong cửa sổ tương tác IDLE mới, hãy bắt đầu bằng cách nhập các lớp 33 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 từ 26, cũng như lớp 83 từ mô-đun 36>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")44 Bây giờ hãy tạo một đối tượng 83 cho tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)210 >>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)99 Cuối cùng, tạo các phiên bản 33 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 mới >>> 50Mục tiêu của bạn là sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 để tạo một tệp PDF mới trong đó tất cả các trang đều có hướng chính xác. Các trang đánh số chẵn trong PDF đã được định hướng đúng, nhưng các trang đánh số lẻ được xoay chín mươi độ ngược chiều kim đồng hồ Để khắc phục sự cố, bạn sẽ sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)223. Phương thức này nhận một đối số số nguyên, theo độ và xoay trang theo chiều kim đồng hồ theo độ đó. Ví dụ: #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)224 xoay trang PDF theo chiều kim đồng hồ 90 độ Ghi chú. Ngoài #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)225, lớp 58 còn có #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)227 để xoay trang ngược chiều kim đồng hồ Có một số cách bạn có thể thực hiện để xoay trang trong PDF. Chúng ta sẽ thảo luận về hai cách khác nhau để thực hiện. Cả hai đều dựa trên #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)225, nhưng họ thực hiện các cách tiếp cận khác nhau để xác định trang nào sẽ được xoay vòng Kỹ thuật đầu tiên là lặp qua các chỉ mục của các trang trong PDF và kiểm tra xem mỗi chỉ mục có tương ứng với một trang cần được xoay không. Nếu vậy, bạn sẽ gọi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)225 để xoay trang và sau đó thêm trang vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 Đây là những gì trông giống như >>> 51Lưu ý rằng trang sẽ được xoay nếu chỉ mục là số chẵn. Điều đó có vẻ lạ vì các trang được đánh số lẻ trong PDF là những trang được xoay không chính xác. Tuy nhiên, số trang trong PDF bắt đầu bằng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463, trong khi chỉ số trang bắt đầu bằng 69. Điều đó có nghĩa là các trang PDF được đánh số lẻ có các chỉ số chẵnNếu điều đó khiến bạn quay cuồng, đừng lo lắng. Ngay cả sau nhiều năm xử lý những thứ như thế này, các lập trình viên chuyên nghiệp vẫn bị vấp ngã bởi những thứ này Ghi chú. Khi bạn thực hiện vòng lặp 78 ở trên, bạn sẽ thấy một loạt kết quả trong cửa sổ tương tác của IDLE. Đó là bởi vì #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)225 trả về một phiên bản 58Bạn có thể bỏ qua đầu ra này ngay bây giờ. Khi bạn thực thi các chương trình từ cửa sổ soạn thảo của IDLE, đầu ra này sẽ không hiển thị Bây giờ bạn đã xoay tất cả các trang trong PDF, bạn có thể ghi nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 vào một tệp mới và kiểm tra xem mọi thứ đã hoạt động chưa >>> 52Bây giờ bạn sẽ có một tệp trong thư mục làm việc hiện tại của mình có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)237, với tất cả các trang từ tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)210 đều được xoay chính xác Vấn đề với phương pháp bạn vừa sử dụng để xoay trang trong tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)210 là nó phụ thuộc vào việc biết trước trang nào cần được xoay. Trong một kịch bản trong thế giới thực, việc xem qua toàn bộ tệp PDF để ghi lại những trang nào cần xoay là không thực tế Trên thực tế, bạn có thể xác định trang nào cần được xoay mà không cần biết trước. Vâng, đôi khi bạn có thể Hãy xem cách thực hiện, bắt đầu với phiên bản 33 mới>>> 53Bạn cần làm điều này vì bạn đã thay đổi các trang trong phiên bản 33 cũ bằng cách xoay chúng. Vì vậy, bằng cách tạo một phiên bản mới, bạn đang bắt đầu làm mớiPhiên bản 58 duy trì một từ điển các giá trị chứa thông tin về trang>>> 54Rất tiếc. Trộn lẫn với tất cả những thứ trông vô nghĩa đó là một khóa có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243, bạn có thể thấy khóa này ở dòng đầu ra thứ tư ở trên. Giá trị của khóa này là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)244 Bạn có thể truy cập khóa #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243 trên một 58 bằng cách sử dụng ký hiệu chỉ số dưới, giống như bạn có thể làm trên một đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)247 của Python >>> 55Nếu bạn nhìn vào khóa #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243 cho trang thứ hai trong 82, bạn sẽ thấy rằng nó có giá trị là 69>>> 56Tất cả điều này có nghĩa là trang ở chỉ mục 69 có giá trị xoay là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)244 độ. Nói cách khác, nó đã được xoay chín mươi độ ngược chiều kim đồng hồ. Trang tại chỉ mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")463 có giá trị xoay vòng là 69, vì vậy nó hoàn toàn không được xoayNếu bạn xoay trang đầu tiên bằng cách sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)225, thì giá trị của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243 sẽ thay đổi từ #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)244 thành 69>>> 57Bây giờ bạn đã biết cách kiểm tra khóa #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243, bạn có thể sử dụng nó để xoay các trang trong tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)210 Điều đầu tiên bạn cần làm là khởi tạo lại các đối tượng 82 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 để bạn có một khởi đầu mới >>> 50Bây giờ hãy viết một vòng lặp lặp qua các trang trong #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)263 iterable, kiểm tra giá trị của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243 và xoay trang nếu giá trị đó là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)244 >>> 59Vòng lặp này không chỉ ngắn hơn một chút so với vòng lặp trong giải pháp đầu tiên mà còn không dựa vào bất kỳ kiến thức nào trước đó về trang nào cần được xoay. Bạn có thể sử dụng một vòng lặp như thế này để xoay các trang trong bất kỳ tệp PDF nào mà không cần phải mở và xem nó Để hoàn thành giải pháp, hãy ghi nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 vào một tệp mới >>> 90Bây giờ bạn có thể mở tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)267 trong thư mục làm việc hiện tại của mình và so sánh nó với tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)237 mà bạn đã tạo trước đó. Họ nên trông giống hệt nhau Ghi chú. Một lời cảnh báo về khóa #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243. nó không được đảm bảo để tồn tại trên một trang Nếu khóa #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243 không tồn tại, thì điều đó thường có nghĩa là trang chưa được xoay. Tuy nhiên, đó không phải lúc nào cũng là một giả định an toàn Nếu một 58 không có khóa #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243, thì một #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)273 sẽ xuất hiện khi bạn cố gắng truy cập nó. Bạn có thể bắt ngoại lệ này bằng khối #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)274 Giá trị của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243 có thể không phải lúc nào cũng như bạn mong đợi. Ví dụ: nếu bạn quét tài liệu giấy với trang được xoay 90 độ ngược chiều kim đồng hồ thì nội dung của tệp PDF sẽ xuất hiện xoay. Tuy nhiên, khóa #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)243 có thể có giá trị 69Đây là một trong nhiều điều kỳ quặc có thể khiến việc làm việc với các tệp PDF trở nên khó chịu. Đôi khi, bạn sẽ chỉ cần mở một tệp PDF trong chương trình đọc PDF và tìm ra mọi thứ theo cách thủ công Loại bỏ các quảng cáoCắt xén trangMột thao tác phổ biến khác với các tệp PDF là cắt xén trang. Bạn có thể cần làm điều này để chia một trang thành nhiều trang hoặc chỉ trích xuất một phần nhỏ của trang, chẳng hạn như chữ ký hoặc hình Ví dụ: thư mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")773 bao gồm một tệp có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)279. Bản PDF này chứa một phần của Nàng tiên cá nhỏ của Hans Christian Andersen Mỗi trang trong PDF này có hai cột. Hãy chia mỗi trang thành hai trang, một trang cho mỗi cột Để bắt đầu, hãy nhập các lớp 33 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 từ 26 và lớp 83 từ mô-đun 36>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")44 Bây giờ hãy tạo một đối tượng 83 cho tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)279 >>> 92Tiếp theo, tạo một đối tượng 33 mới và lấy trang đầu tiên của PDF>>> 93Để cắt trang, trước tiên bạn cần biết thêm một chút về cách các trang được cấu trúc. Các trường hợp 58 như #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")451 có thuộc tính #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)290 đại diện cho một khu vực hình chữ nhật xác định ranh giới của trang Bạn có thể sử dụng cửa sổ tương tác của IDLE để khám phá #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)290 trước khi sử dụng nó để cắt trang >>> 94Thuộc tính #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)290 trả về một #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)293. Đối tượng này được xác định trong gói 26 và đại diện cho một vùng hình chữ nhật trên trangDanh sách #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)295 trong đầu ra xác định diện tích hình chữ nhật. Hai số đầu tiên là tọa độ x và y của góc dưới bên trái của hình chữ nhật. Số thứ ba và số thứ tư lần lượt là chiều rộng và chiều cao của hình chữ nhật. Đơn vị của tất cả các giá trị là điểm, bằng 1/72 inch #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)296 đại diện cho một vùng hình chữ nhật có gốc tọa độ ở góc dưới bên trái, chiều rộng là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)297 điểm hoặc 11 inch và chiều cao là 612 điểm hoặc 8. 5 inch. Đó là các kích thước của một trang cỡ chữ tiêu chuẩn theo hướng ngang, được sử dụng cho tệp PDF mẫu Nàng tiên cá nhỏ. Một trang PDF có kích thước bằng chữ theo hướng dọc sẽ trả về kết quả đầu ra #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)298 Một #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)293 có bốn thuộc tính trả về tọa độ của các góc của hình chữ nhật. #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)900, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)901, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)902 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)903. Cũng giống như các giá trị chiều rộng và chiều cao, các tọa độ này được tính theo điểm Bạn có thể sử dụng bốn thuộc tính này để lấy tọa độ của từng góc của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)293 >>> 95Mỗi thuộc tính trả về một #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)905 chứa tọa độ của góc được chỉ định. Bạn có thể truy cập các tọa độ riêng lẻ bằng dấu ngoặc vuông giống như bất kỳ bộ Python nào khác >>> 96Bạn có thể thay đổi tọa độ của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)906 bằng cách gán một bộ dữ liệu mới cho một trong các thuộc tính của nó >>> 97Khi bạn thay đổi tọa độ #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)902, thuộc tính #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)903 sẽ tự động điều chỉnh để duy trì hình dạng hình chữ nhật >>> 98Khi bạn thay đổi tọa độ của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)293 được trả về bởi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)290, bạn sẽ cắt trang một cách hiệu quả. Đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")451 hiện chỉ chứa thông tin có trong ranh giới của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)293 mới Hãy tiếp tục và ghi trang đã cắt vào một tệp PDF mới >>> 99Nếu bạn mở tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)913 trong thư mục làm việc hiện tại của mình, thì bạn sẽ thấy rằng phần trên cùng của trang đã bị xóa Bạn sẽ cắt trang như thế nào để chỉ hiển thị văn bản ở phía bên trái của trang? . Bạn có thể đạt được điều này bằng cách thay đổi tọa độ #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)903 của đối tượng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)290. Hãy xem nó hoạt động như thế nào Trước tiên, bạn cần lấy các đối tượng 33 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 mới vì bạn vừa thay đổi trang đầu tiên trong 82 và thêm nó vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 >>> 50Bây giờ lấy trang đầu tiên của PDF >>> 81Lần này, hãy làm việc với một bản sao của trang đầu tiên để trang bạn vừa trích xuất vẫn nguyên vẹn. Bạn có thể làm điều đó bằng cách nhập mô-đun #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)920 từ thư viện chuẩn của Python và sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)921 để tạo một bản sao của trang >>> 82Bây giờ bạn có thể thay đổi #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)922 mà không thay đổi thuộc tính của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")451. Bằng cách đó, bạn có thể sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")451 sau này để trích xuất văn bản ở phía bên phải của trang Bây giờ bạn cần làm một chút toán học. Bạn đã nhận ra rằng bạn cần di chuyển góc trên bên phải của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)290 lên chính giữa trên cùng của trang. Để làm điều đó, bạn sẽ tạo một #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)905 mới với thành phần đầu tiên bằng một nửa giá trị ban đầu và gán nó cho thuộc tính #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)903 Đầu tiên, lấy tọa độ hiện tại của góc trên bên phải của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)290 >>> 83Sau đó, tạo một #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)905 mới có tọa độ đầu tiên bằng một nửa giá trị của tọa độ hiện tại và tọa độ thứ hai giống với tọa độ ban đầu >>> 84Cuối cùng, gán tọa độ mới cho thuộc tính #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)903 >>> 85Bây giờ bạn đã cắt trang gốc để chỉ chứa văn bản ở bên trái. Tiếp theo, hãy trích xuất phần bên phải của trang Trước tiên hãy lấy một bản sao mới của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")451 >>> 86Di chuyển góc #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)902 thay vì góc #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)903 >>> 87Thao tác này đặt góc trên bên trái thành cùng tọa độ mà bạn đã di chuyển góc trên bên phải khi trích xuất phần bên trái của trang. Vì vậy, #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)934 bây giờ là một hình chữ nhật có góc trên bên trái nằm ở giữa trên cùng của trang và có góc trên bên phải nằm ở trên cùng bên phải của trang Cuối cùng, thêm các trang #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)922 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)936 vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 và ghi chúng vào một tệp PDF mới >>> 88Bây giờ hãy mở tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)938 bằng trình đọc PDF. Bạn sẽ thấy một tệp có hai trang, trang đầu tiên chứa văn bản từ phía bên trái của trang đầu tiên ban đầu và trang thứ hai chứa văn bản từ phía bên phải ban đầuLoại bỏ các quảng cáo Kiểm tra việc hiểu của bạnMở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn Tập thể dục. Xoay các trang trong PDFHiển thị/Ẩn Trong thư mục 32 trong kho lưu trữ đồng hành cho bài viết này, có một tệp tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)940 Tạo một tệp mới có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)941 trong thư mục chính của máy tính chứa tất cả các trang từ #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)940, nhưng mỗi trang được xoay ngược chiều kim đồng hồ 90 độ Bạn có thể mở rộng khối bên dưới để xem giải pháp Giải pháp. Xoay các trang trong PDFHiển thị/Ẩn Thiết lập đường dẫn đến tệp PDF 89Bây giờ bạn có thể tạo các phiên bản 33 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 10Lặp lại các trang trong ________ 382, xoay tất cả chúng 90 độ bằng cách sử dụng ________ 2227 và thêm chúng vào #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 11Cuối cùng, viết nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 vào một tệp có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)941 trong thư mục chính của máy tính của bạn 12Mã hóa và giải mã PDFĐôi khi các tệp PDF được bảo vệ bằng mật khẩu. Với gói 26, bạn có thể làm việc với các tệp PDF được mã hóa cũng như thêm mật khẩu bảo vệ cho các tệp PDF hiện cóGhi chú. Hướng dẫn này được điều chỉnh từ chương “Tạo và sửa đổi tệp PDF” trong Python Basics. Giới thiệu thực tế về Python 3. Nếu bạn thích những gì bạn đang đọc, thì hãy nhớ xem phần còn lại của cuốn sách Mã hóa tệp PDFBạn có thể thêm mật khẩu bảo vệ vào tệp PDF bằng phương pháp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)951 của phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)952. Nó có hai tham số chính
Hãy sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)951 để thêm mật khẩu vào tệp PDF. Đầu tiên, mở tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)956 trong thư mục #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")773 >>> 13Bây giờ hãy tạo một phiên bản #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 mới và thêm các trang từ 82 vào đó>>> #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")76 Tiếp theo, thêm mật khẩu #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)960 với #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)961 >>> 15Khi bạn chỉ đặt #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)953, đối số #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)954 sẽ mặc định là cùng một chuỗi. Vì vậy, dòng mã trên đặt cả mật khẩu người dùng và chủ sở hữu Cuối cùng, ghi tệp PDF được mã hóa vào một tệp đầu ra trong thư mục chính của bạn có tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)964 >>> 16Khi bạn mở tệp PDF bằng trình đọc PDF, bạn sẽ được nhắc nhập mật khẩu. Nhập #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)960 để mở PDF Nếu bạn cần đặt mật khẩu chủ sở hữu riêng cho PDF, hãy chuyển chuỗi thứ hai cho tham số #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)954 >>> 17Trong ví dụ này, mật khẩu người dùng là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)960 và mật khẩu chủ sở hữu là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)968 Khi bạn mã hóa tệp PDF bằng mật khẩu và cố gắng mở tệp đó, bạn phải cung cấp mật khẩu trước khi có thể xem nội dung của tệp. Bảo vệ này mở rộng để đọc từ PDF trong chương trình Python. Tiếp theo, hãy xem cách giải mã tệp PDF bằng 26Giải mã PDFĐể giải mã tệp PDF được mã hóa, hãy sử dụng phương pháp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)970 của phiên bản 33#import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)970 có một tham số duy nhất gọi là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)973 mà bạn có thể sử dụng để cung cấp mật khẩu để giải mã. Các đặc quyền bạn có khi mở tệp PDF tùy thuộc vào đối số bạn đã chuyển đến tham số #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)973 Hãy mở tệp #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)964 được mã hóa mà bạn đã tạo trong phần trước và sử dụng 26 để giải mã nóĐầu tiên, tạo một phiên bản 33 mới với đường dẫn đến tệp PDF được bảo vệ>>> 18Trước khi bạn giải mã PDF, hãy xem điều gì sẽ xảy ra nếu bạn cố lấy trang đầu tiên >>> 19Một ngoại lệ #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)978 được đưa ra, thông báo cho bạn biết rằng tệp PDF chưa được giải mã Ghi chú. Tracback ở trên đã được rút ngắn để làm nổi bật những phần quan trọng. Truy nguyên bạn thấy trên máy tính của mình sẽ dài hơn nhiều Hãy tiếp tục và giải mã tệp ngay bây giờ >>> 00#import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)970 trả về một số nguyên đại diện cho sự thành công của việc giải mã
Khi bạn đã giải mã tệp, bạn có thể truy cập nội dung của tệp PDF >>> 01Giờ đây, bạn có thể trích xuất văn bản và cắt hoặc xoay trang theo nội dung yêu thích của mình Kiểm tra việc hiểu của bạnMở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn Tập thể dục. Mã hóa PDFHiển thị/Ẩn Trong thư mục 32 trong kho lưu trữ đồng hành cho bài viết này, có một tệp tên là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)984 Sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)985, mã hóa tệp bằng mật khẩu người dùng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)986. Lưu tệp được mã hóa dưới dạng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)987 trong thư mục chính trên máy tính của bạn Bạn có thể mở rộng khối bên dưới để xem giải pháp Giải pháp. Mã hóa PDFHiển thị/Ẩn Thiết lập đường dẫn đến tệp PDF 02Bây giờ hãy tạo các phiên bản 33 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")409 10Bạn có thể nối tất cả các trang từ 82 đến #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 bằng cách sử dụng #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")700 04Bây giờ sử dụng _____ 2993 để đặt mật khẩu người dùng thành ________ 2994 05Cuối cùng, viết nội dung của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # create a cell, specify its alignment and position pdf.cell(200, 10, txt = "Fedingo.com", ln = 1, align = 'C') # add another cell pdf.cell(200, 10, txt = "Useful tech articles", ln = 2, align = 'C') # save the pdf with name .pdf pdf.output("fedingo.pdf")414 vào một tệp có tên #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)987 trong thư mục chính của máy tính của bạn 06Tạo tệp PDF từ đầuGói 26 rất tốt để đọc và sửa đổi các tệp PDF hiện có, nhưng nó có một hạn chế lớn. bạn không thể sử dụng nó để tạo một tệp PDF mới. Trong phần này, bạn sẽ sử dụng Bộ công cụ ReportLab để tạo tệp PDF từ đầuReportLab là một giải pháp đầy đủ tính năng để tạo tệp PDF. Có phiên bản thương mại phải trả tiền để sử dụng, nhưng cũng có phiên bản mã nguồn mở có tính năng hạn chế Ghi chú. Phần này không phải là phần giới thiệu đầy đủ về ReportLab, mà là một ví dụ về những gì có thể Để biết thêm ví dụ, hãy xem trang đoạn mã của ReportLab Cài đặt #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15) # open the text file in read mode f = open("data.txt", "r") # insert the texts in pdf for x in f: pdf.cell(200, 10, txt = x, ln = 1, align = 'C') # save the pdf with name .pdf pdf.output("data.pdf") 998Để bắt đầu, bạn cần cài đặt #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998 với 27 07Bạn có thể xác minh cài đặt với 501 08Tại thời điểm viết bài, phiên bản mới nhất của #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998 là 3. 5. 34. Nếu bạn đang mở IDLE, thì bạn cần phải khởi động lại nó trước khi có thể sử dụng gói #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998 Sử dụng lớp $ python3 -m pip show PyPDF2 Name: PyPDF2 Version: 1.26.0 Summary: PDF toolkit Home-page: http://mstamy2.github.com/PyPDF2 Author: Mathieu Fenniak Author-email: [email protected] License: UNKNOWN Location: c:\\users\\david\\python38-32\\lib\\site-packages Requires: Required-by: 504Giao diện chính để tạo tệp PDF với #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998 là lớp 504, lớp này nằm trong mô-đun 507Mở một cửa sổ tương tác IDLE mới và nhập thông tin sau để nhập lớp 504>>> 09Khi bạn tạo một phiên bản 504 mới, bạn cần cung cấp một chuỗi có tên tệp PDF mà bạn đang tạo. Hãy tiếp tục và tạo một phiên bản 504 mới cho tệp 511>>> 10Bây giờ bạn có một phiên bản 504 mà bạn đã gán cho tên biến 513 và được liên kết với một tệp trong thư mục làm việc hiện tại của bạn có tên là 511. Tuy nhiên, tệp 511 chưa tồn tạiHãy thêm một số văn bản vào PDF. Để làm điều đó, bạn sử dụng 516>>> 11Hai đối số đầu tiên được chuyển đến 516 xác định vị trí trên canvas nơi văn bản được viết. Cái đầu tiên chỉ định khoảng cách từ cạnh trái của canvas và cái thứ hai chỉ định khoảng cách từ cạnh dưới cùngCác giá trị được chuyển đến 516 được đo bằng điểm. Vì một điểm bằng 1/72 inch, nên 519 vẽ chuỗi 520 một inch từ bên trái và một inch từ cuối trangĐể lưu tệp PDF vào một tệp, hãy sử dụng 521>>> 12Bây giờ bạn có một tệp PDF trong thư mục làm việc hiện tại của mình có tên là 511. Bạn có thể mở nó bằng trình đọc PDF và xem văn bản 523 ở cuối trangCó một số điều cần lưu ý về tệp PDF bạn vừa tạo
Bạn không bị mắc kẹt với các cài đặt này Đặt Kích thước TrangKhi bạn khởi tạo một đối tượng 504, bạn có thể thay đổi kích thước trang bằng tham số 525 tùy chọn. Tham số này chấp nhận một bộ giá trị dấu phẩy động đại diện cho chiều rộng và chiều cao của trang theo điểmVí dụ: để đặt kích thước trang thành chiều rộng 526 inch x chiều cao 527 inch, bạn sẽ tạo 504 sau 13 529 đại diện cho một tờ giấy cỡ chữ vì 526 nhân với 531 là 532 và 527 nhân với 531 là #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)297 Nếu việc tính toán để chuyển đổi điểm sang inch hoặc centimet không phải là sở thích của bạn, thì bạn có thể sử dụng mô-đun ________ 3536 để giúp bạn chuyển đổi. Mô-đun 537 chứa một số đối tượng trợ giúp, chẳng hạn như 538 và 539, giúp đơn giản hóa các chuyển đổi của bạnTiếp tục và nhập các đối tượng 538 và 539 từ mô-đun 536>>> 14Bây giờ bạn có thể kiểm tra từng đối tượng để xem chúng là gì >>> 15Cả 539 và 538 đều là các giá trị dấu phẩy động. Chúng đại diện cho số điểm có trong mỗi đơn vị. 538 là 546 điểm và 539 là 548 điểmĐể sử dụng các đơn vị, hãy nhân tên đơn vị với số lượng đơn vị mà bạn muốn chuyển đổi thành điểm. Ví dụ: đây là cách sử dụng 538 để đặt kích thước trang thành chiều rộng 526 inch x chiều cao 527 inch>>> 16Bằng cách chuyển một bộ tới 525, bạn có thể tạo bất kỳ kích thước trang nào bạn muốn. Tuy nhiên, gói #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998 có một số kích thước trang tích hợp tiêu chuẩn dễ làm việc hơn Các kích thước trang được đặt trong mô-đun 554. Ví dụ: để đặt kích thước trang thành chữ cái, bạn có thể nhập đối tượng 555 từ mô-đun 556 và chuyển nó tới tham số 525 khi khởi tạo 504 của bạn>>> 17Nếu bạn kiểm tra đối tượng 555, thì bạn sẽ thấy rằng đó là một bộ số float>>> 18Mô-đun 560 chứa nhiều kích thước trang tiêu chuẩn. Dưới đây là một số với kích thước của chúngKích thước trangKích thước 561210 mm x 297 mm 5558. 5 trong x 11 trong_______35638. 5 trong x 14 trong________ 356411 trong x 17 trongNgoài những thứ này, mô-đun còn chứa các định nghĩa cho tất cả các khổ giấy tiêu chuẩn ISO 216 Đặt thuộc tính phông chữBạn cũng có thể thay đổi phông chữ, cỡ chữ và màu phông chữ khi viết văn bản vào 504Để thay đổi phông chữ và cỡ chữ, bạn có thể sử dụng 566. Đầu tiên, tạo một phiên bản 504 mới với tên tệp 568 và kích thước trang chữ cái>>> 19Sau đó đặt phông chữ thành Times New Roman với kích thước là 569 điểm>>> 20Cuối cùng, viết chuỗi 570 vào canvas và lưu nó>>> 21Với các cài đặt này, văn bản sẽ được viết một inch từ bên trái của trang và mười inch từ dưới cùng. Mở tệp 568 trong thư mục làm việc hiện tại của bạn và kiểm tra nóCó ba phông chữ có sẵn theo mặc định
Mỗi phông chữ có các biến thể in đậm và in nghiêng. Đây là danh sách tất cả các biến thể phông chữ có sẵn trong #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998
Bạn cũng có thể đặt màu phông chữ bằng cách sử dụng 588. Trong ví dụ sau, bạn tạo một tệp PDF có chữ màu xanh tên là 589 22 590 là một đối tượng được nhập từ mô-đun 591. Mô-đun này chứa một số màu phổ biến. Có thể tìm thấy danh sách đầy đủ các màu trong mã nguồn #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998 Các ví dụ trong phần này nêu bật những điều cơ bản khi làm việc với đối tượng 504. Nhưng bạn chỉ trầy xước bề mặt. Với #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998, bạn có thể tạo bảng, biểu mẫu và thậm chí cả đồ họa chất lượng cao từ đầu Hướng dẫn sử dụng ReportLab chứa rất nhiều ví dụ về cách tạo tài liệu PDF từ đầu. Đó là một nơi tuyệt vời để bắt đầu nếu bạn muốn tìm hiểu thêm về cách tạo tệp PDF bằng Python Kiểm tra việc hiểu của bạnMở rộng khối bên dưới để kiểm tra sự hiểu biết của bạn Tập thể dục. Tạo PDF từ đầuHiển thị/Ẩn Tạo một tệp PDF trong thư mục chính trên máy tính của bạn có tên là 595 với các trang có kích thước chữ cái chứa văn bản 596 được đặt cách mép trái hai inch và cách mép dưới của trang tám inchBạn có thể mở rộng khối bên dưới để xem giải pháp Giải pháp. Tạo PDF từ đầuHiển thị/Ẩn Thiết lập phiên bản 504 với các trang có kích thước chữ cái 23Bây giờ vẽ chuỗi 596 hai inch từ bên trái và tám inch từ phía dưới 24Cuối cùng, lưu 513 để ghi tệp PDF 25Khi bạn đã sẵn sàng, bạn có thể chuyển sang phần tiếp theo Sự kết luận. Tạo và sửa đổi tệp PDF bằng PythonTrong hướng dẫn này, bạn đã học cách tạo và sửa đổi tệp PDF bằng gói 26 và #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998. Nếu bạn muốn làm theo các ví dụ bạn vừa xem, thì hãy nhớ tải xuống tài liệu bằng cách nhấp vào liên kết bên dưới Tải tài liệu mẫu. Nhấp vào đây để lấy tài liệu bạn sẽ sử dụng để tìm hiểu về cách tạo và sửa đổi tệp PDF trong hướng dẫn này Với 26, bạn đã học cách
Bạn cũng đã có phần giới thiệu về cách tạo tệp PDF từ đầu với gói #import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998. Bạn đã học cách
#import fpdf from fpdf import FPDF #create object instance pdf = FPDF() #add page pdf.add_page() # set font style and size pdf.set_font("Arial", size = 15)998 là một công cụ tạo PDF mạnh mẽ và bạn mới chỉ vạch ra bề nổi của những gì có thể. Nếu bạn thích những gì bạn học được trong ví dụ này từ Python Basics. Giới thiệu thực tế về Python 3, thì hãy nhớ xem phần còn lại của cuốn sách Mã hóa vui vẻ Đánh dấu là đã hoàn thành 🐍 Thủ thuật Python 💌 Nhận một Thủ thuật Python ngắn và hấp dẫn được gửi đến hộp thư đến của bạn vài ngày một lần. Không có thư rác bao giờ. Hủy đăng ký bất cứ lúc nào. Được quản lý bởi nhóm Real Python Gửi cho tôi thủ thuật Python » Giới thiệu về David Amos David là một nhà văn, lập trình viên và nhà toán học đam mê khám phá toán học thông qua mã » Thông tin thêm về DavidMỗi hướng dẫn tại Real Python được tạo bởi một nhóm các nhà phát triển để nó đáp ứng các tiêu chuẩn chất lượng cao của chúng tôi. Các thành viên trong nhóm đã làm việc trong hướng dẫn này là Aldren Joanna Gia-cốp Bậc thầy Kỹ năng Python trong thế giới thực Với quyền truy cập không giới hạn vào Python thực Tham gia với chúng tôi và có quyền truy cập vào hàng nghìn hướng dẫn, khóa học video thực hành và cộng đồng các Pythonistas chuyên gia Nâng cao kỹ năng Python của bạn » Bậc thầy Kỹ năng Python trong thế giới thực Tham gia với chúng tôi và có quyền truy cập vào hàng ngàn hướng dẫn, khóa học video thực hành và cộng đồng Pythonistas chuyên gia Nâng cao kỹ năng Python của bạn » Bạn nghĩ sao? Đánh giá bài viết này Tweet Chia sẻ Chia sẻ EmailBài học số 1 hoặc điều yêu thích mà bạn đã học được là gì? Mẹo bình luận. Những nhận xét hữu ích nhất là những nhận xét được viết với mục đích học hỏi hoặc giúp đỡ các sinh viên khác. Nhận các mẹo để đặt câu hỏi hay và nhận câu trả lời cho các câu hỏi phổ biến trong cổng thông tin hỗ trợ của chúng tôi |