英文:
Problem with DataFrame_Image No such file or directory: ...\\AppData\\Local\\Temp\\tmpm56n0wp6\\temp.png
问题
ubicacion = "C:.../Desktop/Informes/names/"
nombrepng = "name.png"
def df_to_image(df, ubicacion, nombre):
if os.path.exists(ubicacion + nombre):
os.remove(ubicacion + nombre)
dfi.export(df, ubicacion + nombre)
else:
dfi.export(df, ubicacion + nombre)
When i try to save a dataframe in a png it gives me this error:
File "C:\AppData\Local\Programs\Python\Python311\Lib\site-packages\dataframe_image\_screenshot.py", line 158, in possibly_enlarge
return self.take_screenshot()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\AppData\Local\Programs\Python\Python311\Lib\site-packages/dataframe_image/_screenshot.py", line 135, in take_screenshot
with open(temp_img, "rb") as f:
^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\AppData\\Local\\Temp\\tmpm56n0wp6\\temp.png'
What is very strange is that yesterday it worked correctly for me and today it gives me that error for almost all the scripts in which create a png. A few still work for me but most don't.
On my other computer they work perfectly but on the notebook they don't. I already tried to reinstall all the libraries and python.
<details>
<summary>英文:</summary>
```python
ubicacion = "C:.../Desktop/Informes/names/"
nombrepng= "name.png"
def df_to_image(df, ubicacion, nombre):
if os.path.exists(ubicacion+nombre):
os.remove(ubicacion+nombre)
dfi.export(df, ubicacion+nombre)
else:
dfi.export(df, ubicacion+nombre)
df_to_image(df, ubicacion, nombrepng)
When i try to save a dataframe in a png it gives me this error:
File "C:\AppData\Local\Programs\Python\Python311\Lib\site-packages\dataframe_image\_screenshot.py", line 158, in possibly_enlarge
return self.take_screenshot()
^^^^^^^^^^^^^^^^^^^^^^
File "C:\AppData\Local\Programs\Python\Python311\Lib\site-packages\dataframe_image\_screenshot.py", line 135, in take_screenshot
with open(temp_img, "rb") as f:
^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\AppData\\Local\\Temp\\tmpm56n0wp6\\temp.png'
What is very strange is that yesterday it worked correctly for me and today it gives me that error for almost all the scripts in which create a png. A few still work for me but most don't.
On my other computer they work perfectly but on the notebook they don't. I already tried to reinstall all the libraries and python
答案1
得分: 2
在GitHub上有一个开放的问题:https://github.com/dexplo/dataframe_image/issues/79
这里提出的解决方法对我有效,只需使用选项 table_conversion='matplotlib'
:
https://github.com/dexplo/dataframe_image/issues/79#issuecomment-1468371096
dfi.export(df, "table.png", table_conversion='matplotlib')
然而,我失去了我样式化数据框的着色。
英文:
There is an open issue on github: https://github.com/dexplo/dataframe_image/issues/79
The workaround proposed here worked for me, by just using option table_conversion='matplotlib'
:
https://github.com/dexplo/dataframe_image/issues/79#issuecomment-1468371096
dfi.export(df, "table.png", table_conversion='matplotlib')
However I lost the colorization of my styled dataframe.
答案2
得分: 0
我尝试添加"matplotlib",但颜色丢失,变得无用。
我通过在导出时添加fontsize和dpi来解决了这个问题,
示例:
dfi.export(df, "table.png", fontsize=8, dpi=150)
希望能帮助。
英文:
I tried adding "matplotlib" but the coloring was lost, making it useless.
I solved it by adding fontsize and dpi in export,
example:
dfi.export(df, "table.png", fontsize=8, dpi=150)
Hope it helps
答案3
得分: 0
鉴于今年内图书馆停止工作的次数已经是第二次了,我们决定重新编写脚本以利用不同的工具。我们过去通常使用这个库进行每日报告,但我们的新解决方案似乎更加稳定。
我们转而使用 openpyxl
来构建/格式化我们的 Excel 表格,然后使用 excel2img
将图像导出为 .png 文件。
我知道这并不是您一直在GitHub上尝试的每个解决方案的直接解决方案,但在花了几周的时间尝试了每个人在GitHub上建议的解决方案后,转用 openpyxl
是我们想出的最佳解决方案。
关于我们的脚本,我不能分享太多细节,但这里是我们正在使用的库:
from PIL import Image
import PIL
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Font, colors, Color, Alignment, PatternFill, GradientFill, Border, Side
from openpyxl.styles import NamedStyle
from openpyxl.styles.differential import DifferentialStyle
from openpyxl.formatting import Rule
from openpyxl.formatting.rule import ColorScale, FormatObject
from openpyxl.chart import LineChart, Reference
from openpyxl.chart.axis import DateAxis
from openpyxl.drawing.spreadsheet_drawing import TwoCellAnchor
import excel2img
以下是我们如何使用这些库来实现与使用 dataframe_image
时类似的结果的简要示例:
# 代码示例已被省略
至于导出:
# 将表格导出为图像
excel2img.export_img("excel_file_name.xlsx", "image_1.png", "", "sheet_name!A1:I10")
excel2img.export_img("excel_file_name.xlsx", "image_2.png", "", "sheet_name2!A1:I10")
希望这有所帮助。
英文:
Given that this was the second time this year that the library stopped working, we have decided to rewrite out scripts to utilize different tools. We used to use this library for daily reporting, but our new solution seems to be more stable.
We switched over to using openpyxl
to build/format our tables using excel, and then use excel2img
to export the image into a .png file.
I know that it is not really a direct solution to the problem you've been experiencing, but after spending a few weeks trying every solution people had suggested in GitHub, switching over to openpyxl
was the best solution we came up with.
I cannot share too much detail about our scripts, but here are the libraries we are using:
from PIL import Image
import PIL
from openpyxl.workbook import Workbook
from openpyxl import load_workbook
from openpyxl.styles import Font, colors, Color, Alignment, PatternFill, GradientFill, Border, Side
from openpyxl.styles import NamedStyle
from openpyxl.styles.differential import DifferentialStyle
from openpyxl.formatting import Rule
from openpyxl.formatting.rule import ColorScale, FormatObject
from openpyxl.chart import LineChart, Reference
from openpyxl.chart.axis import DateAxis
from openpyxl.drawing.spreadsheet_drawing import TwoCellAnchor
import excel2img
Here is a brief example of how we are using these libraries to achieve a similar result as what we had when using dataframe_image
:
header_rows = [1, 2]
for i in columns:
for j in header_rows:
ws['{}{}'.format(i,j)].fill = PatternFill(patternType='solid', fgColor='003F89')
ws['{}{}'.format(i,j)].alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
ws['{}{}'.format(i,j)].font = Font(bold=True, color='FFFFFF', name='Arial')
ws['E2'].fill = PatternFill(patternType='solid', fgColor='0000FF')
percent_cols = ['B', 'D', 'H']
dat_rows = [3, 4, 5, 6, 7, 8, 9, 10]
for i in percent_cols:
for j in dat_rows:
ws['{}{}'.format(i,j)].number_format = '0.0%'
ws['{}{}'.format(i,j)].alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
for i in dat_rows:
ws['E{}'.format(i)].number_format = '0'
ws['E{}'.format(i)].alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
for i in dat_rows:
ws['C{}'.format(i)].number_format = '0,000'
base_10_cols = ['F', 'G', 'I']
for i in base_10_cols:
for j in dat_rows:
ws['{}{}'.format(i,j)].number_format = '0.00'
ws['{}{}'.format(i,j)].alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
eh_first = FormatObject(type='num', val=.6)
eh_last = FormatObject(type='num', val=1)
eh_mid = FormatObject(type='num', val=.8)
colors = [Color('F8696B'), Color('FFFFFF'), Color('5A8AC6')]
eh_cs3 = ColorScale(cfvo=[eh_first, eh_mid, eh_last], color=colors)
eh_rule = Rule(type='colorScale', colorScale=eh_cs3)
eh_style = Font(bold=True, name='Arial')
eh_rule2 = Rule(type='expression', formula=['B3>={}'.format(a1)], dxf=DifferentialStyle(font=Font(bold=True, name='Arial')))
ws.conditional_formatting.add('B3:B10', eh_rule)
ws.conditional_formatting.add('B3:B10', eh_rule2)
cm_style = Font(bold=True, name='Arial')
cm_rule2 = Rule(type='expression', formula=['D3>={}'.format(b1)], dxf=DifferentialStyle(font=Font(bold=True, name='Arial')))
ws.conditional_formatting.add('D3:D10', cm_rule2)
co_first = FormatObject(type='num', val=918)
co_last = FormatObject(type='num', val=1000)
co_mid = FormatObject(type='num', val=959)
co_cs3 = ColorScale(cfvo=[co_first, co_mid, co_last], color=colors)
co_rule = Rule(type='colorScale', colorScale=co_cs3)
co_style = Font(bold=True, name='Arial')
co_rule2 = Rule(type='expression', formula=['E3>={}'.format(c1)], dxf=DifferentialStyle(font=Font(bold=True, name='Arial')))
ws.conditional_formatting.add('E3:E10', co_rule)
ws.conditional_formatting.add('E3:E10', co_rule2)
de_first = FormatObject(type='num', val=9.5)
de_last = FormatObject(type='num', val=10)
de_mid = FormatObject(type='num', val=9.75)
de_cs3 = ColorScale(cfvo=[de_first, de_mid, de_last], color=colors)
de_rule = Rule(type='colorScale', colorScale=de_cs3)
de_style = Font(bold=True, name='Arial')
de_rule2 = Rule(type='expression', formula=['F3>={}'.format(d1)], dxf=DifferentialStyle(font=Font(bold=True, name='Arial')))
ws.conditional_formatting.add('F3:F10', de_rule)
ws.conditional_formatting.add('F3:F10', de_rule2)
ag_first = FormatObject(type='num', val=9.3)
ag_last = FormatObject(type='num', val=10)
ag_mid = FormatObject(type='num', val=9.65)
ag_cs3 = ColorScale(cfvo=[ag_first, ag_mid, ag_last], color=colors)
ag_rule = Rule(type='colorScale', colorScale=ag_cs3)
ag_style = Font(bold=True, name='Arial')
ag_rule2 = Rule(type='expression', formula=['G3>={}'.format(e1)], dxf=DifferentialStyle(font=Font(bold=True, name='Arial')))
ws.conditional_formatting.add('G3:G10', ag_rule)
ws.conditional_formatting.add('G3:G10', ag_rule2)
ti_first = FormatObject(type='num', val=0.48)
ti_last = FormatObject(type='num', val=1)
ti_mid = FormatObject(type='num', val=.74)
ti_cs3 = ColorScale(cfvo=[ti_first, ti_mid, ti_last], color=colors)
ti_rule = Rule(type='colorScale', colorScale=ti_cs3)
ti_style = Font(bold=True, name='Arial')
ti_rule2 = Rule(type='expression', formula=['H3>={}'.format(f1)], dxf=DifferentialStyle(font=Font(bold=True, name='Arial')))
ws.conditional_formatting.add('H3:H10', ti_rule)
ws.conditional_formatting.add('H3:H10', ti_rule2)
fa_first = FormatObject(type='num', val=9.1)
fa_last = FormatObject(type='num', val=10)
fa_mid = FormatObject(type='num', val=9.55)
fa_cs3 = ColorScale(cfvo=[fa_first, fa_mid, fa_last], color=colors)
fa_rule = Rule(type='colorScale', colorScale=fa_cs3)
fa_style = Font(bold=True, name='Arial')
fa_rule2 = Rule(type='expression', formula=['I3>={}'.format(x1)], dxf=DifferentialStyle(font=Font(bold=True, name='Arial')))
ws.conditional_formatting.add('I3:I10', fa_rule)
ws.conditional_formatting.add('I3:I10', fa_rule2)
nf_cols = ['A', 'C', 'D']
for i in nf_cols:
for j in dat_rows:
ws['{}{}'.format(i,j)].fill = PatternFill(patternType='solid', fgColor='FFFFFF')
ws['{}{}'.format(i,j)].alignment = Alignment(horizontal='center', vertical='center', wrap_text=True)
thin_border = Border(left=Side(style='thin', color='FFFFFF'),
right=Side(style='thin', color='FFFFFF'),
top=Side(style='thin', color='FFFFFF'),
bottom=Side(style='thin', color='FFFFFF'))
for i in columns:
for j in range(10):
ws['{}{}'.format(i,j+1)].border = thin_border
ws['E2'].border = Border(left=Side(style='thin', color='000000'),
right=Side(style='thin', color='000000'),
top=Side(style='thin', color='000000'))
for i in [3, 4, 5, 6, 7, 8, 9, 10]:
ws['E{}'.format(i)].border = Border(left=Side(style='thin', color='000000'),
right=Side(style='thin', color='000000'))
ws['E10'].border = Border(bottom=Side(style='thin', color='000000'),
left=Side(style='thin', color='000000'),
right=Side(style='thin', color='000000'))
And for the export:
#export tables as images
excel2img.export_img("excel_file_name.xlsx","image_1.png", "","sheet_name!A1:I10")
excel2img.export_img("excel_file_name.xlsx","image_2.png", "","sheet_name2!A1:I10")
I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论