วิธีแปลง CSV เป็น PDF ใน Python

บทแนะนำวิธีใช้สั้นๆ นี้ให้รายละเอียดเกี่ยวกับวิธี แปลง CSV เป็น PDF ใน Python หากต้องการเปลี่ยน CSV เป็น PDF แอปพลิเคชันที่ใช้ Python สามารถพัฒนาได้ทั้งในสภาพแวดล้อมที่ใช้ macO, Windows หรือ Linux ที่กำหนดค่าด้วย Python และ JDK คุณจะสังเกตเห็นคุณสมบัติต่างๆ และการใช้งานเพื่อปรับแต่งรูปลักษณ์ของ PDF ที่สร้างขึ้น

ขั้นตอนในการแปลง CSV เป็น PDF ใน Python

  1. กำหนดค่า PIP, Python3 และ Aspose.Cells สำหรับ Python โดยปฏิบัติตามหลักเกณฑ์
  2. สร้างอินสแตนซ์ของ TxtLoadOptions และตั้งค่าตัวคั่นที่ใช้ใน CSV ต้นทาง
  3. สร้างวัตถุ Workbook เพื่อโหลดไฟล์ CSV ต้นทางสำหรับการแปลงเป็น PDF
  4. ใช้อินสแตนซ์คลาส PdfSaveOptions เพื่อตั้งค่าคุณสมบัติสำหรับ PDF ที่ต้องการ
  5. ใช้วิธีบันทึกเพื่อบันทึก CSV เป็น PDF บนดิสก์

ในขั้นตอนเหล่านี้ มีการใช้แอปพลิเคชันที่ใช้ แปลง CSV เป็น PDF Python หลังจากกำหนดค่าสภาพแวดล้อมแล้ว สิ่งแรกที่คุณจะต้องทำคือตั้งค่าอักขระตัวคั่น CSV โดยใช้อินสแตนซ์ TxtLoadOptions จากนั้นใช้คลาสสมุดงาน ไฟล์ CSV จะถูกโหลดด้วย TxtLoadOptions ที่ให้มา ในขั้นตอนต่อมา อินสแตนซ์ PdfSaveOptions จะถูกสร้างขึ้นเพื่อตั้งค่าตัวเลือกการส่งออก PDF ต่างๆ และสุดท้าย PDF จะถูกบันทึกลงในดิสก์โดยใช้ตัวเลือก PDF ที่กำหนดไว้

รหัสเพื่อแปลง CSV เป็น PDF ใน Python

import jpype
import asposecells
jpype.startJVM(convertStrings=False)
from asposecells.api import Workbook, TxtLoadOptions, PdfSaveOptions, GridlineType, License
# Set product License
licenseCsvToPdf = License()
licenseCsvToPdf.setLicense("Aspose.Total.lic")
# Initialize TxtLoadOptions object to manage CSV file loading
txtLoadOptions = TxtLoadOptions()
# Include a separator character, if the source CSV does not contain default character like comma
txtLoadOptions.setSeparator('#')
# Add a workbook object by loading CSV file and using the loading options set above
workbook = Workbook("sample.csv", txtLoadOptions)
# Initialize PdfSaveOptions object to set properties of the output PDF file
pdfSaveOptions = PdfSaveOptions()
# Configure grid-line type to Hair when displaying data in the PDF file
pdfSaveOptions.setGridlineType(GridlineType.HAIR)
# Setting the print grid lines parameter to True for displaying table grid
workbook.getWorksheets().get(0).getPageSetup().setPrintGridlines(True)
# Configuring the table to display the complete text in PDF by calling autoFitColumns() functions
workbook.getWorksheets().get(0).autoFitColumns()
# Saving the generated PDF file using the above mentioned settings
workbook.save("ConvertedPdf.pdf", pdfSaveOptions)
jpype.shutdownJVM()

จากตัวอย่างข้างต้น คุณสามารถสังเกตได้ว่าการแปลง Python CSV เป็น PDF ทำได้อย่างง่ายดายด้วยโค้ดไม่กี่บรรทัด เราสามารถควบคุมกระบวนการโหลด CSV ได้อย่างยอดเยี่ยมในแง่ของการกำหนด AutoFitterOptions, AutoFilter, TextQualifier, IgnoreNotPrinted และตัวเลือกอื่นๆ อีกมากมายที่มีในวัตถุคลาส TxtLoadOptions ในทำนองเดียวกัน เราสามารถปรับแต่ง PDF ที่ต้องการได้โดยการตั้งค่า DefaultFont, FontEncoding, OptimizationType, DisplayDocTitle, OptimizationType และการกำหนดค่าอื่นๆ อีกมากมายในไฟล์ PDF เอาต์พุตโดยใช้คลาส PdfSaveOptions

ในหัวข้อนี้ เราได้เรียนรู้การแปลง CSV เป็น PDF โดยใช้ Python ภายในโค้ดไม่กี่บรรทัด หากคุณกำลังมองหาการสร้างไฟล์ Excel โดยใช้ python โปรดดูบทความเกี่ยวกับ วิธีสร้างไฟล์ Excel ใน Python

 ไทย