Thư viện Python cho phép Python thông qua. NET để chuyển đổi Word, PDF và nhiều định dạng tệp khác. Công cụ chuyển đổi mạnh mẽ phân tích bố cục và kiểu của tài liệu gốc, sau đó xuất kết quả kết hợp sang định dạng được yêu cầu
Chuyển đổi tài liệu từ định dạng này sang định dạng khác có thể phức tạp, trừ khi bạn có một công cụ chuyên nghiệp để thực hiện công việc. Sử dụng Python để chuyển đổi Word, PDF, JPG với độ linh hoạt và tốc độ tối đa. Chạy demo trực tuyến và kiểm tra chất lượng chuyển đổi cao nhất ngay trong trình duyệt. API chuyển đổi DOCX hỗ trợ một loạt các tùy chọn hữu ích
Hợp nhất Word, PDF, JPG thành một tệp
Đây là một cách đơn giản để chuyển đổi tài liệu văn phòng từ định dạng này sang định dạng khác trong khi để lại tất cả các chi tiết cấp thấp cho API chuyển đổi. Thư viện Python cho phép Python thông qua. NET để chuyển đổi tài liệu ở nhiều định dạng phổ biến với tính linh hoạt và hiệu quả tối đa
- Phần mềm soạn thảo văn bản. DOCX, DOC, RTF, DOT, DOTM, DOTX, DOCM
- Mở văn phòng. ODT, OTT
- Bố cục cố định. PDF, PostScript, XPS, OpenXPS
- Hình ảnh. PNG, JPG, GIF, BMP, TIFF, SVG, EMF
- mạng. HTML, MHTML
- Khác. MOBI, EPUB, Xaml, PCL, v.v.
Chuyển đổi nhiều tài liệu và lưu kết quả dưới dạng một tệp dễ dàng. Nếu bạn phát triển mã bằng Python, điều này sẽ đơn giản hơn bạn tưởng. Xem ví dụ về Python lặp qua các tài liệu và kết hợp chúng
Ví dụ mã trong Python để kết hợp nhiều tệp thành một
Sao chép
tập tin đầu vào
Tải lên một tài liệu
Tải lên tệp bạn muốn hợp nhất
Mã vận hành
Tải lên một tài liệu
Tải lên tệp bạn muốn hợp nhất
Định dạng đầu ra
Chọn định dạng đích từ danh sách
import aspose.words as aw
fileNames = [ "Input1.docx", "Input2.docx" ]
output = aw.Document[]
# Remove all content from the destination document before appending.
output.remove_all_children[]
for fileName in fileNames:
input = aw.Document[fileName]
# Append the source document to the end of the destination document.
output.append_document[input, aw.ImportFormatMode.KEEP_SOURCE_FORMATTING]
output.save["Output.pdf"];
Mã vận hành
Bạn có thể tự do sao chép mã này và sử dụng nó trong bất kỳ mục đích áp dụng nào
Cách hợp nhất PDF, Word, JPG và các định dạng tệp khác trong Python
- Cài đặt 'Aspose. Từ cho Python thông qua. NET'
- Thêm tham chiếu thư viện [nhập thư viện] vào dự án Python của bạn
- Mở tệp nguồn bằng Python
- Hợp nhất các tập tin thành một
- Gọi phương thức 'append_document[]', chuyển tên tệp đầu ra với phần mở rộng bắt buộc
- Nhận kết quả kết hợp dưới dạng một tệp riêng biệt
Thư viện Python để hợp nhất các tệp
Chúng tôi lưu trữ các gói Python của mình trong kho PyPi. Vui lòng làm theo hướng dẫn từng bước về cách cài đặt "Aspose. Từ cho Python thông qua. NET" vào môi trường nhà phát triển của bạn
yêu cầu hệ thống
Gói này tương thích với Python 3. 5, 3. 6, 3. 7, 3. 8 và 3. 9. Nếu bạn phát triển phần mềm cho Linux, vui lòng xem các yêu cầu bổ sung đối với gcc và libpython trong Tài liệu sản phẩm
Thông thường, với tư cách là nhà phân tích dữ liệu, bạn có thể thấy mình bị quá tải với nhiều tệp CSV cần được kết hợp với nhau trước khi bạn có thể bắt đầu phân tích dữ liệu có sẵn. Tuy nhiên, không phải lúc nào tất cả các tệp đều được trích xuất từ cùng một nguồn dữ liệu và có cùng cột dữ liệu hoặc theo cùng một cấu trúc dữ liệu
Trong hướng dẫn này, bạn sẽ tìm hiểu cách kết hợp nhiều CSV với cấu trúc cột tương tự hoặc khác nhau và cách sử dụng các hàm
#import the modules5,
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
#import the modules6,
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
#import the modules7 và
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
#import the modules8 để làm như vậy
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Trước khi chúng tôi làm điều đó, hãy xem cách nhập một tệp csv vào khung dữ liệu bằng gói Pandas
1. Nhập tệp vào DataFrames của gấu trúc
Để nhập một tệp vào khung dữ liệu, bạn chỉ cần sử dụng hàm
#import the modules9
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
#import pandas module
import pandas as pd#read the csv file into a dataframe
df = pd.read_csv["D:\Blog\Merge_Files\csv_files\csv_sample1.csv"]df
Khi bạn có nhiều tệp để làm việc, cách tốt nhất là dán tất cả các tệp vào một thư mục và sau đó đọc tất cả các tệp này bằng hàm
#import the modules9
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
2. Thiết lập thư mục làm việc
Một phương pháp là chuyển đường dẫn của thư mục vào một biến và sau đó liệt kê tất cả các tệp trong thư mục đó
#import the modules
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Mặt khác, nếu bạn muốn đọc các tệp từ cùng thư mục với tệp ipynb của mình, bạn có thể sử dụng mã bên dưới
#import the modules
import os
import pandas as pd#read the path
cwd = os.path.abspath['']#list all the files from the directory
file_list = os.listdir[cwd]
file_list
Phương pháp thứ ba là sử dụng hàm
#import the modules1 để chỉ liệt kê các tệp csv từ thư mục làm việc
import os
import pandas as pd#read the path
cwd = os.path.abspath['']#list all the files from the directory
file_list = os.listdir[cwd]
file_list
#import the modules1
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Giờ đây, để đọc nhiều tệp CSV có cấu trúc bảng tương tự, bạn có thể sử dụng các hàm
#import the modules2 HOẶC
import os
import pandas as pd#read the path
cwd = os.path.abspath['']#list all the files from the directory
file_list = os.listdir[cwd]
file_list
#import the modules3
import os
import pandas as pd#read the path
cwd = os.path.abspath['']#list all the files from the directory
file_list = os.listdir[cwd]
file_list
Hãy xem 3 tệp CSV mẫu mà chúng tôi sẽ làm việc với
csv_sample1. csv
csv_sample2. csv
csv_sample3. csv
Cả ba tệp đều có tiêu đề cột giống nhau ngoại trừ csv_Sample2. csv có một cột bổ sung có tên “Ngày sinh”. Ngoài ra, lưu ý rằng có 2 mục phổ biến giữa csv_Sample1. csv và csv_Sample2. csv, như được đánh dấu. Đây, mục nhập cho “Tom R. Powell” có các giá trị “Ngày tham gia” khác nhau trong cả hai tệp. Lưu ý cách các mục này được kết hợp trong tất cả các phương pháp được sử dụng bên dưới
3. Kết hợp nhiều tệp với cấu trúc bảng tương tự bằng gấu trúc. Khung dữ liệu. nối thêm []
Sử dụng đoạn mã dưới đây để đọc và kết hợp tất cả các tệp csv từ thư mục đã đặt trước đó
#import the modules4
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Kết quả sau khi sử dụng hàm
#import the modules5 như bên dưới
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Tại đây, bạn có thể thấy rằng tất cả các hàng dữ liệu từ các tệp đã được thêm vào bên dưới hàng khác. Tuy nhiên, các giá trị NaN đã được chèn vào cột “Ngày sinh” vì các giá trị này không có trong csv_sample1. csv và csv_sample3. tệp csv
4. Kết hợp nhiều tệp với cấu trúc bảng tương tự bằng gấu trúc. concat[]
Một cách khác để kết hợp các tệp là sử dụng
#import the modules5, như hình bên dưới
import os
import pandas as pd#read the path
cwd = os.path.abspath['']#list all the files from the directory
file_list = os.listdir[cwd]
file_list
#import the modules7
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Bây giờ, nếu bạn muốn nối các hàng dữ liệu của các tệp dựa trên các cột liên quan thì bạn có thể sử dụng hàm
#import the modules6
import os
import pandas as pd#read the path
cwd = os.path.abspath['']#list all the files from the directory
file_list = os.listdir[cwd]
file_list
5. Sử dụng gấu trúc. Khung dữ liệu. merge[] để nối các hàng dữ liệu
Trước tiên, hãy đọc các tệp thành các tệp dữ liệu riêng biệt như bên dưới
#import the modules9
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
#import the modules0
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Bây giờ, trong khi sử dụng
#import the modules7 giữa các khung dữ liệu này, bạn cần chỉ định các cột có liên quan mà bạn muốn nối các hàng
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
#import the modules2
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Hàm chỉ nối tất cả các hàng khi tất cả các giá trị của các cột đã chỉ định khớp nhau
Ở đây, chúng tôi đã sử dụng phương pháp nối ngoài để hợp nhất các tệp. Để tìm hiểu thêm về loại hợp nhất sẽ được thực hiện, bạn có thể tham khảo liên kết này. gấu trúc. hợp nhất[]
Trong ví dụ trên, chúng tôi đã chuyển một danh sách các tên cột mà chúng tôi muốn nối các hàng. Thay vào đó, nếu chúng ta chỉ nối các hàng trên cột “Email” thì chúng ta sẽ nhận được kết quả như bên dưới
#import the modules3
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Bây giờ, nếu bạn muốn tạo một khung dữ liệu với các giá trị giả sử, csv_sample1. csv và bất cứ nơi nào null, hãy lấy các giá trị từ một tệp khác, chẳng hạn như csv_sample2. csv sau đó sử dụng
#import the modules8
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
6. Cập nhật giá trị null trong các cột từ các cột khác bằng gấu trúc. tổ hợp_đầu tiên[]
Thay thế '_x' từ tiêu đề cột
#import the modules0
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list
Vượt qua tất cả các tên cột mà bạn muốn áp dụng
#import the modules8. Một cách dễ dàng là tìm nạp các cột có '_y' trong tiêu đề và sau đó xóa '_y' khỏi chúng, như bên dưới
import os
import pandas as pd#read the path
file_path = "D:\Blog\Merge_Files\csv_files"#list all the files from the directory
file_list = os.listdir[file_path]
file_list