ในโพสต์นี้ ฉันจะแสดงวิธีสร้างแผนภูมิใน excel โดยใช้ Python - โมดูล Openpyxl เราจะสร้างสเปรดชีต excel ตั้งแต่เริ่มต้นด้วยชื่อผู้เล่นเทนนิสแกรนด์สแลมเป็นข้อมูลสำหรับการสร้างแผนภูมิแท่งโดยใช้โมดูล openpyxl
แนะนำตัว..
Microsoft office ได้เริ่มให้บริการส่วนขยายใหม่สำหรับแผ่นงาน Microsoft Excel ซึ่งก็คือ .xlsx จาก Office 2007 เพื่อรองรับการจัดเก็บแถวและคอลัมน์เพิ่มเติม การเปลี่ยนแปลงนี้ได้ย้ายแผ่นงาน Excel ไปเป็นรูปแบบไฟล์ XML ที่มีการบีบอัดไฟล์ ZIP โลกถูกปกครองโดยสเปรดชีตของ Microsoft ผู้คนใช้สเปรดชีตเพื่อวัตถุประสงค์ต่างๆ และกรณีการใช้งานหนึ่งมีไว้สำหรับการแสดงข้อมูลเป็นภาพ
เตรียมตัวให้พร้อม..
โมดูล xlrd ของ Python ซึ่งเป็นทางเลือกสำหรับโมดูล Openpyxl นั้นรองรับรูปแบบ Excel ได้ดี อย่างไรก็ตาม โมดูลนี้สามารถดำเนินการอ่านอย่างเดียวบนสเปรดชีต excel โมดูล openpyxl สามารถดำเนินการทั้งการอ่านและเขียนบนแผ่นงาน Excel
ทำอย่างไร..
1). ก่อนอื่นให้เราติดตั้งโมดูล openpyxl โดยใช้
pip install openpyxl.
2).กำหนดข้อมูลสำหรับสร้างสเปรดชีต excel ใหม่
# import the module import openpyxl # Define your file name and data file_name = "charts.xlsx" file_data = (['player', 'titles'], ['Federer', 20], ['Nadal', 20], ['Djokovic', 17], ['Murray', 3])
3).สร้างไฟล์ Excel ใหม่ สิ่งนี้จะสร้างแผ่นงานเริ่มต้นที่เรียกว่าแผ่นงาน
xlsxfile = openpyxl.Workbook()
print(f" *** The sheets inside the excel_file are = {xlsxfile.sheetnames}")
new_workbook = xlsxfile['Sheet']
*** The sheets inside the excel_file are = ['Sheet']
4).เพิ่มข้อมูลเกี่ยวกับนักเทนนิสและชื่อแกรนด์สแลมของพวกเขาลงในแผ่นงานนี้
for row, (player,titles) in enumerate(file_data, 1):
new_workbook['A{}'.format(row)].value = player
new_workbook['B{}'.format(row)].value = titles 5).สุดท้ายบันทึกข้อมูลไปยังไฟล์ file_name
xlsxfile.save(file_name)
6) โหลดไฟล์ลงในหน่วยความจำและแสดงรายการชีตทั้งหมด โปรดทราบว่าเราได้สร้างแผ่นงานเพียงแผ่นเดียวซึ่งเป็นส่วนหนึ่งของขั้นตอนที่ 2
import openpyxl excel_file_data = openpyxl.load_workbook(file_name) excel_file_data.sheetnames
['Sheet']
7) รับชีตแรกและรับค่าของเซลล์ เช่น A2 และ B2
sheet_values = excel_file_data['Sheet']
print(f" *** One of the value from the sheet is - {sheet_values['A2'].value} - {sheet_values['B2'].value}")
*** One of the value from the sheet is - Federer - 20
8) แสดงผลแถวและคอลัมน์ทั้งหมดในสเปรดชีตของเรา เพื่อให้แน่ใจว่าเราได้แทรกข้อมูลของเราอย่างถูกต้องสำหรับแผนภูมิ
for row in sheet_values: for cell in row: print(cell.value)
player titles Federer 20 Nadal 20 Djokovic 17 Murray 3
9) นำเข้าโมดูล BarChart อ้างอิงจาก openpyxl.chart และสร้างวัตถุ Barchart
from openpyxl.chart import BarChart, Reference chart = BarChart()
#10. # Fill the basic information like chart title,.. chart.title = "Players & Grand Slams" chart.y_axis.title = 'Titles' chart.x_axis.title = 'Tennis Players'
#11. # Now we will create a reference to the data and append the data to the chart. data = Reference(sheet_values, min_row=2, max_row=5, min_col=1, max_col=2) chart.add_data(data, from_rows=True, titles_from_data=True)
#12. # Finally, Add the chart to the sheet and save the file. new_workbook.add_chart(chart, "A6") xlsxfile.save(file_name)
ขั้นตอนที่ 11 สร้างกล่องอ้างอิงผ่านวัตถุอ้างอิง ตั้งแต่แถวที่ 2 คอลัมน์ 1 ถึงแถวที่ 5 คอลัมน์ 2 ซึ่งเป็นพื้นที่ที่มีข้อมูลของเรา ไม่รวมส่วนหัวนอกหลักสูตร
ข้อมูลจะถูกเพิ่มลงในแผนภูมิด้วยเมธอด .add_data() from_rows - ทำให้แต่ละแถวเป็นชุดข้อมูลที่แตกต่างกัน titles_from_data - ใช้คอลัมน์แรกเพื่อตั้งชื่อซีรีส์
ตัวอย่าง
เราจะนำทุกอย่างที่อธิบายข้างต้นมารวมกัน
"""
Program: Create charts in excel using Python with openpyxl params: NA
output: Creates a chart.xlsx file with tennis players grandslam titles and a barchart representation of the data
"""
# import the module
import openpyxl
# Define your file name and data
file_name = "charts.xlsx"
file_data = ([ 'player' , 'titles' ], [ 'Federer' , 20 ], [ 'Nadal' , 20 ], [ 'Djokovic' , 17 ], [ 'Murray' , 3 ])
# create an excel spreadsheet
xlsxfile = openpyxl . Workbook ()
print ( f " *** The sheets inside the excel_file are = { xlsxfile . sheetnames } " )
new_workbook = xlsxfile [ 'Sheet' ]
for row , ( player , titles ) in enumerate ( file_data , 1 ):
new_workbook [ 'A {} ' . format ( row )] . value = player
new_workbook [ 'B {} ' . format ( row )] . value = titles
# save the spreadsheet
xlsxfile .save ( file_name )
# read the data
excel_file_data = openpyxl . load_workbook ( file_name )
excel_file_data . sheetnames
sheet_values = excel_file_data [ 'Sheet' ]
print ( f " *** One of the value from the sheet is - { sheet_values [ 'A2' ] . value } - { sheet_values [ 'B2' ] . value } " )
for row in sheet_values :
for cell in row :
print ( cell . value ) # barchart creation from openpyxl.chart
import BarChart , Reference chart = BarChart ()
# Fill the basic information like chart title,..
chart . title = "Players & Grand Slams"
chart . y_axis . title = 'Titles'
chart . x_axis . title = 'Tennis Players'
# Now we will create a reference to the data and append the data to the chart.
data = Reference ( sheet_values , min_row = 2 , max_row = 5 , min_col = 1 , max_col = 2 )
chart .
add_data ( data , from_rows = True , titles_from_data = True )
# Finally, Add the chart to the sheet and save the file.
new_workbook . add_chart ( chart , "A6" )
xlsxfile . save ( file_name ) *** The sheets inside the excel_file are = ['Sheet'] *** One of the value from the sheet is - Federer - 20 player titles Federer 20 Nadal 20 Djokovic 17 Murray 3
ผลลัพธ์
เมื่อรันโปรแกรมด้านบนนี้ charts.xlsx จะถูกสร้างขึ้นในไดเร็กทอรีเดียวกันกับโค้ดด้านล่างนี้
