ทำตามบทความนี้เพื่อ สร้างตาราง Excel โดยใช้ Python โดยมีรายละเอียดในการตั้งค่า IDE รายการขั้นตอนที่แสดงตรรกะของโปรแกรม และโค้ดตัวอย่างที่รันได้ซึ่งสาธิต วิธีสร้างตารางใน Excel โดยใช้ Python คุณจะได้เรียนรู้การปรับแต่งตารางและแสดงข้อมูลเพิ่มเติมต่างๆ เพื่อทำให้ตารางสมบูรณ์ เช่น การแสดงค่ารวมโดยอัตโนมัติ
ขั้นตอนในการเพิ่มตารางลงใน Excel โดยใช้ Python
- ตั้งค่า IDE เป็น Aspose.Cells สำหรับ Python ผ่าน Java สำหรับการสร้างตาราง
- สร้างไฟล์ใหม่หรือโหลดไฟล์ Excel ที่มีอยู่ซึ่งมีแหล่งข้อมูลสำหรับตาราง
- กรอก sheet ด้วยข้อมูลตัวอย่างสำหรับไฟล์ Excel ใหม่
- เข้าถึงชีตเป้าหมายและเพิ่ม list object สำหรับช่วงของข้อมูล
- ตั้งค่ารูปแบบตารางด้วยตัวแจงนับ TableStyleType
- ตั้งค่าแสดงค่าสถานะทั้งหมดและจำนวนค่าในคอลัมน์ที่ระบุ
- บันทึกไฟล์ Excel เอาต์พุตด้วยตารางที่จัดรูปแบบ
ขั้นตอนเหล่านี้อธิบาย วิธีสร้างตารางใน Excel โดยใช้ Python เริ่มต้นกระบวนการโดยการเข้าถึงแผ่นงานเป้าหมายที่มีข้อมูล และเพิ่มวัตถุรายการเป็นตารางโดยระบุช่วงของเซลล์ที่มีข้อมูลอยู่ เมื่อคุณสร้างตารางแล้ว ให้ตั้งค่าสไตล์ตารางและแฟล็กเพื่อแสดงผลรวมและการคำนวณผลรวมเฉพาะตามความต้องการ
รหัสเพื่อเพิ่มตาราง Excel โดยใช้ Python
import jpype | |
import asposecells as cells | |
import random | |
jpype.startJVM() | |
from asposecells.api import License, Workbook, TableStyleType, TotalsCalculation | |
def create_sample_data(sheet): | |
# Fill workbook with some dummy data | |
titles = ["Employee", "Quarter", "Product", "Country", "Sale"] | |
employees = ["David", "James", "Miya"] | |
products = ["Chai", "Chang", "Geitost", "Maxilaku"] | |
countries = ["USA", "China", "Turkiye", "Germany", "India", "Italy"] | |
for idx, title in enumerate(titles): | |
sheet.getCells().get(0, idx).setValue(title) | |
for i in range(1, 20): | |
sheet.getCells().get(i, 0).setValue(random.choice(employees)) | |
sheet.getCells().get(i, 1).setValue(random.randint(1, 4)) | |
sheet.getCells().get(i, 2).setValue(random.choice(products)) | |
sheet.getCells().get(i, 3).setValue(random.choice(countries)) | |
sheet.getCells().get(i, 4).setValue(random.randint(0, 2000)) | |
# Instantiate a license | |
license = License() | |
license.setLicense("License.lic") | |
# Create a workbook. | |
wb = Workbook() | |
# Optionally call this function if workbook has no data | |
create_sample_data(wb.getWorksheets().get(0)) | |
# Obtain the first sheet | |
sheet = wb.getWorksheets().get(0) | |
# Add a new list object with 20 rows and 5 columns | |
listObject = sheet.getListObjects().get(sheet.getListObjects().add("A1", "E20", True)) | |
# Set table style | |
listObject.setTableStyleType(TableStyleType.TABLE_STYLE_MEDIUM_10) | |
# Show the flag to display the Total for all numbers | |
listObject.setShowTotals(True) | |
# Set the second column calculation type | |
listObject.getListColumns().get(1).setTotalsCalculation(TotalsCalculation.COUNT) | |
# Saving the Excel file | |
wb.save("output.xlsx") | |
print("Table created successfully!!!") |
ข้อมูลโค้ดนี้แสดง วิธีสร้างตารางข้อมูลใน Excel โดยใช้ Python เมธอด create_sample_data() เป็นทางเลือก และคุณสามารถข้ามได้หากไฟล์ Excel มีข้อมูลที่ต้องการอยู่แล้ว ใช้ตัวแจงนับ TableStyleType เพื่อสร้างสไตล์ตาราง และเลือกหนึ่งในตัวเลือก TotalsCalculation เช่น COUNT ตามที่แสดงในส่วนย่อยโค้ดนี้ คุณสามารถเลือกจากประเภทอื่น ๆ ที่จัดทำโดยตัวแจงนับดังกล่าว
บทความนี้สอนเราให้สร้างตาราง Excel และจัดรูปแบบ สำหรับการสร้างตารางสรุป โปรดดูบทความเกี่ยวกับ วิธีสร้างตารางเดือยใน Python