Код: Выделить всё
import openpyxl as opyxl
from openpyxl.styles import Font
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties, Font, RegularTextRun
from openpyxl.xml.functions import fromstring
from openpyxl.chart.shapes import GraphicalProperties
#The Point of This Program is to create a cleaner program
def graph_formatting(file, font_style = 'Times New Roman', fill_color = "000000", xt_name = "Default", yt_name = "Default", font_size = 1000, bold = '0'):
wb = opyxl.load_workbook(file)
for sheets in wb.chartsheets: #loops through available chartsheets
dechart = sheets._charts[0]
#This specifies the axis format using XML;
xml = f"""
{yt_name}
"""
dechart.y_axis.title.tx.rich = RichText.from_tree(fromstring(xml)) #Change the format using specified XML;
#Same idea
xml = f"""
{xt_name}
"""
dechart.x_axis.title.tx.rich = RichText.from_tree(fromstring(xml))
#Changes rest of the of charts, uch as axis labels
font = Font(typeface=font_style) #set the font
size = font_size #set the font size
cp = CharacterProperties(latin=font, sz=size, b=False)
pp = ParagraphProperties(defRPr=cp)
dechart.x_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)]) #changes the x axis label font
dechart.y_axis.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=cp), endParaRPr=cp)]) #changes the y axis label font
#chart styling for the minor details
dechart.graphical_properties = GraphicalProperties()
dechart.graphical_properties.line.noFill = True
dechart.graphical_properties.line.prstDash = None
dechart.x_axis.graphicalProperties.line.solidFill = fill_color #changes the line color
dechart.y_axis.graphicalProperties.line.noFill=False #draws the y axis line
dechart.y_axis.majorGridlines = None #gets rid of gridline
dechart.y_axis.majorTickMark = 'out' #create tickmarks for x and y axis
dechart.x_axis.majorTickMark = 'out'
#wb.save(path)
if dechart:
wb.save(file)
Код: Выделить всё
{yt_name}
Идеальный рабочий процесс с этой программой:
- Я создаю диаграмму в Excel.
- Я форматирую диаграмму в Excel с помощью этой программы.
- Два дня спустя мой начальник попросите меня создать еще одну диаграмму в том же файле.
- Я создаю еще одну диаграмму.
Я форматирую диаграмму с помощью той же программы.
Подробнее здесь: https://stackoverflow.com/questions/790 ... n-openpyxl