英文:
How can I export excel charts in higher resolutions within python?
问题
目前我已经找到一种在Python中导出Excel图表的方法。然而,这些图像的质量非常低。以下是我现在拥有的代码:
# 导入模块
from win32com.client import Dispatch
workdir = 'C:/Users/example/'
excel_file = 'filename.xlsx'
sheet_name = 'Sheetname'
output_directory = workdir + 'output/'
app = Dispatch('Excel.Application')
workbook = app.Workbooks.Open(Filename=excel_file)
app.DisplayAlerts = False
dpi = 300 # 设置所需分辨率(每英寸的点数)
i = 1
for sheet in workbook.Worksheets:
sheet.Activate() # 激活工作表
for chartObject in sheet.ChartObjects():
print(sheet.Name + ':' + chartObject.Name)
chart = chartObject.Chart
# chart.ChartArea.Width = chart.ChartArea.Width * dpi / 72
# chart.ChartArea.Height = chart.ChartArea.Height * dpi / 72
chart.Export(output_directory + str(i) + '.png', FilterName="PNG")
i = i+1
workbook.Close(SaveChanges=False, Filename=excel_file)
app.Quit()
然而,上述图片的质量很低。我尝试过改变宽度和高度,但这会破坏图表。例如,所有的文本和线条都会变得越来越小。它不必是.png格式。只要是高分辨率的图片,首先保存为pdf/jpg也是可以的。
英文:
Currently I have found a way to export excel charts within python. However, these images are in a very low quality. This is what I have now:
#Importing the modules
from win32com.client import Dispatch
workdir = 'C:/Users/example/'
excel_file = 'filename.xlsx'
sheet_name = 'Sheetname'
output_directory = workdir + 'output/'
app = Dispatch('Excel.Application')
workbook = app.Workbooks.Open(Filename=excel_file)
app.DisplayAlerts = False
dpi = 300 # Set the desired resolution (dots per inch)
i = 1
for sheet in workbook.Worksheets:
sheet.Activate() # Activate the sheet
for chartObject in sheet.ChartObjects():
print(sheet.Name + ':' + chartObject.Name)
chart = chartObject.Chart
#chart.ChartArea.Width = chart.ChartArea.Width * dpi / 72
#chart.ChartArea.Height = chart.ChartArea.Height * dpi / 72
chart.Export(output_directory + str(i) + '.png', FilterName="PNG")
i = i+1
workbook.Close(SaveChanges=False, Filename=excel_file)
app.Quit()
However, above pictures are thus in low quality. I tried to change the width and height, but this ruins the charts. So are for example all the text and lines getting smaller and smaller. It doesn't have to be .png. It would be fine to first save it as pdf/jpg as long as it is a high resolution picture.
答案1
得分: 1
根据问题中链接的答案,增加 ActiveWindow.Zoom
级别以在导出图表之前进行设置,然后重置它。将缩放设置为 400 对我来说产生了一个清晰的大图表。
# 导入模块
from win32com.client import Dispatch
workdir = 'C:/Users/example/'
excel_file = 'filename.xlsx'
sheet_name = 'Sheetname'
output_directory = workdir + 'output/'
app = Dispatch('Excel.Application')
workbook = app.Workbooks.Open(Filename=excel_file)
app.DisplayAlerts = False
dpi = 300 # 设置所需的分辨率(每英寸点数)
i = 1
for sheet in workbook.Worksheets:
sheet.Activate() # 激活工作表
for chartObject in sheet.ChartObjects():
print(sheet.Name + ':' + chartObject.Name)
chart = chartObject.Chart
zoom = app.ActiveWindow.Zoom # 注意原始缩放级别
app.ActiveWindow.Zoom = 400 # 增加缩放级别
chart.Export(output_directory + str(i) + '.png', FilterName="PNG")
app.ActiveWindow.Zoom = zoom # 然后在导出图表后重置它
i = i + 1
workbook.Close(SaveChanges=False, Filename=excel_file)
app.Quit()
英文:
As per the answer given in the question BigBen linked to, increase the ActiveWindow.Zoom
level before exporting the chart, then reset it. Setting the zoom to 400 resulted in a nice big, crisp chart for me.
#Importing the modules
from win32com.client import Dispatch
workdir = 'C:/Users/example/'
excel_file = 'filename.xlsx'
sheet_name = 'Sheetname'
output_directory = workdir + 'output/'
app = Dispatch('Excel.Application')
workbook = app.Workbooks.Open(Filename=excel_file)
app.DisplayAlerts = False
dpi = 300 # Set the desired resolution (dots per inch)
i = 1
for sheet in workbook.Worksheets:
sheet.Activate() # Activate the sheet
for chartObject in sheet.ChartObjects():
print(sheet.Name + ':' + chartObject.Name)
chart = chartObject.Chart
zoom = app.ActiveWindow.Zoom # Take note of the original zoom level
app.ActiveWindow.Zoom = 400 # Bump it right up
chart.Export(output_directory + str(i) + '.png', FilterName="PNG")
app.ActiveWindow.Zoom = zoom # Then reset it after exporting the chart
i = i+1
workbook.Close(SaveChanges=False, Filename=excel_file)
app.Quit()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论