No such file or directory: …\\AppData\\Local\\Temp\\tmpm56n0wp6\\temp.png

huangapple go评论149阅读模式
英文:

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 = &quot;C:.../Desktop/Informes/names/&quot;
nombrepng= &quot;name.png&quot;

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 &quot;C:\AppData\Local\Programs\Python\Python311\Lib\site-packages\dataframe_image\_screenshot.py&quot;, line 158, in possibly_enlarge
        return self.take_screenshot()
               ^^^^^^^^^^^^^^^^^^^^^^
      File &quot;C:\AppData\Local\Programs\Python\Python311\Lib\site-packages\dataframe_image\_screenshot.py&quot;, line 135, in take_screenshot
        with open(temp_img, &quot;rb&quot;) as f:
             ^^^^^^^^^^^^^^^^^^^^
    FileNotFoundError: [Errno 2] No such file or directory: &#39;C:\\AppData\\Local\\Temp\\tmpm56n0wp6\\temp.png&#39;

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=&#39;matplotlib&#39;
https://github.com/dexplo/dataframe_image/issues/79#issuecomment-1468371096

dfi.export(df, &quot;table.png&quot;, table_conversion=&#39;matplotlib&#39;)

然而,我失去了我样式化数据框的着色。

英文:

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=&#39;matplotlib&#39; :
https://github.com/dexplo/dataframe_image/issues/79#issuecomment-1468371096

dfi.export(df, &quot;table.png&quot;, table_conversion=&#39;matplotlib&#39;)

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, &quot;table.png&quot;, 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[&#39;{}{}&#39;.format(i,j)].fill = PatternFill(patternType=&#39;solid&#39;, fgColor=&#39;003F89&#39;)
ws[&#39;{}{}&#39;.format(i,j)].alignment = Alignment(horizontal=&#39;center&#39;, vertical=&#39;center&#39;, wrap_text=True)
ws[&#39;{}{}&#39;.format(i,j)].font = Font(bold=True, color=&#39;FFFFFF&#39;, name=&#39;Arial&#39;)
ws[&#39;E2&#39;].fill = PatternFill(patternType=&#39;solid&#39;, fgColor=&#39;0000FF&#39;)
percent_cols = [&#39;B&#39;, &#39;D&#39;, &#39;H&#39;]
dat_rows = [3, 4, 5, 6, 7, 8, 9, 10]
for i in percent_cols:
for j in dat_rows:
ws[&#39;{}{}&#39;.format(i,j)].number_format = &#39;0.0%&#39;
ws[&#39;{}{}&#39;.format(i,j)].alignment = Alignment(horizontal=&#39;center&#39;, vertical=&#39;center&#39;, wrap_text=True)
for i in dat_rows:
ws[&#39;E{}&#39;.format(i)].number_format = &#39;0&#39;
ws[&#39;E{}&#39;.format(i)].alignment = Alignment(horizontal=&#39;center&#39;, vertical=&#39;center&#39;, wrap_text=True)
for i in dat_rows:
ws[&#39;C{}&#39;.format(i)].number_format = &#39;0,000&#39;
base_10_cols = [&#39;F&#39;, &#39;G&#39;, &#39;I&#39;]
for i in base_10_cols:
for j in dat_rows:
ws[&#39;{}{}&#39;.format(i,j)].number_format = &#39;0.00&#39;
ws[&#39;{}{}&#39;.format(i,j)].alignment = Alignment(horizontal=&#39;center&#39;, vertical=&#39;center&#39;, wrap_text=True)
eh_first = FormatObject(type=&#39;num&#39;, val=.6)
eh_last = FormatObject(type=&#39;num&#39;, val=1)
eh_mid = FormatObject(type=&#39;num&#39;, val=.8)
colors = [Color(&#39;F8696B&#39;), Color(&#39;FFFFFF&#39;), Color(&#39;5A8AC6&#39;)]
eh_cs3 = ColorScale(cfvo=[eh_first, eh_mid, eh_last], color=colors)
eh_rule = Rule(type=&#39;colorScale&#39;, colorScale=eh_cs3)
eh_style = Font(bold=True, name=&#39;Arial&#39;)
eh_rule2 = Rule(type=&#39;expression&#39;, formula=[&#39;B3&gt;={}&#39;.format(a1)], dxf=DifferentialStyle(font=Font(bold=True, name=&#39;Arial&#39;)))
ws.conditional_formatting.add(&#39;B3:B10&#39;, eh_rule)
ws.conditional_formatting.add(&#39;B3:B10&#39;, eh_rule2)
cm_style = Font(bold=True, name=&#39;Arial&#39;)
cm_rule2 = Rule(type=&#39;expression&#39;, formula=[&#39;D3&gt;={}&#39;.format(b1)], dxf=DifferentialStyle(font=Font(bold=True, name=&#39;Arial&#39;)))
ws.conditional_formatting.add(&#39;D3:D10&#39;, cm_rule2)
co_first = FormatObject(type=&#39;num&#39;, val=918)
co_last = FormatObject(type=&#39;num&#39;, val=1000)
co_mid = FormatObject(type=&#39;num&#39;, val=959)
co_cs3 = ColorScale(cfvo=[co_first, co_mid, co_last], color=colors)
co_rule = Rule(type=&#39;colorScale&#39;, colorScale=co_cs3)
co_style = Font(bold=True, name=&#39;Arial&#39;)
co_rule2 = Rule(type=&#39;expression&#39;, formula=[&#39;E3&gt;={}&#39;.format(c1)], dxf=DifferentialStyle(font=Font(bold=True, name=&#39;Arial&#39;)))
ws.conditional_formatting.add(&#39;E3:E10&#39;, co_rule)
ws.conditional_formatting.add(&#39;E3:E10&#39;, co_rule2)
de_first = FormatObject(type=&#39;num&#39;, val=9.5)
de_last = FormatObject(type=&#39;num&#39;, val=10)
de_mid = FormatObject(type=&#39;num&#39;, val=9.75)
de_cs3 = ColorScale(cfvo=[de_first, de_mid, de_last], color=colors)
de_rule = Rule(type=&#39;colorScale&#39;, colorScale=de_cs3)
de_style = Font(bold=True, name=&#39;Arial&#39;)
de_rule2 = Rule(type=&#39;expression&#39;, formula=[&#39;F3&gt;={}&#39;.format(d1)], dxf=DifferentialStyle(font=Font(bold=True, name=&#39;Arial&#39;)))
ws.conditional_formatting.add(&#39;F3:F10&#39;, de_rule)
ws.conditional_formatting.add(&#39;F3:F10&#39;, de_rule2)
ag_first = FormatObject(type=&#39;num&#39;, val=9.3)
ag_last = FormatObject(type=&#39;num&#39;, val=10)
ag_mid = FormatObject(type=&#39;num&#39;, val=9.65)
ag_cs3 = ColorScale(cfvo=[ag_first, ag_mid, ag_last], color=colors)
ag_rule = Rule(type=&#39;colorScale&#39;, colorScale=ag_cs3)
ag_style = Font(bold=True, name=&#39;Arial&#39;)
ag_rule2 = Rule(type=&#39;expression&#39;, formula=[&#39;G3&gt;={}&#39;.format(e1)], dxf=DifferentialStyle(font=Font(bold=True, name=&#39;Arial&#39;)))
ws.conditional_formatting.add(&#39;G3:G10&#39;, ag_rule)
ws.conditional_formatting.add(&#39;G3:G10&#39;, ag_rule2)
ti_first = FormatObject(type=&#39;num&#39;, val=0.48)
ti_last = FormatObject(type=&#39;num&#39;, val=1)
ti_mid = FormatObject(type=&#39;num&#39;, val=.74)
ti_cs3 = ColorScale(cfvo=[ti_first, ti_mid, ti_last], color=colors)
ti_rule = Rule(type=&#39;colorScale&#39;, colorScale=ti_cs3)
ti_style = Font(bold=True, name=&#39;Arial&#39;)
ti_rule2 = Rule(type=&#39;expression&#39;, formula=[&#39;H3&gt;={}&#39;.format(f1)], dxf=DifferentialStyle(font=Font(bold=True, name=&#39;Arial&#39;)))
ws.conditional_formatting.add(&#39;H3:H10&#39;, ti_rule)
ws.conditional_formatting.add(&#39;H3:H10&#39;, ti_rule2)
fa_first = FormatObject(type=&#39;num&#39;, val=9.1)
fa_last = FormatObject(type=&#39;num&#39;, val=10)
fa_mid = FormatObject(type=&#39;num&#39;, val=9.55)
fa_cs3 = ColorScale(cfvo=[fa_first, fa_mid, fa_last], color=colors)
fa_rule = Rule(type=&#39;colorScale&#39;, colorScale=fa_cs3)
fa_style = Font(bold=True, name=&#39;Arial&#39;)
fa_rule2 = Rule(type=&#39;expression&#39;, formula=[&#39;I3&gt;={}&#39;.format(x1)], dxf=DifferentialStyle(font=Font(bold=True, name=&#39;Arial&#39;)))
ws.conditional_formatting.add(&#39;I3:I10&#39;, fa_rule)
ws.conditional_formatting.add(&#39;I3:I10&#39;, fa_rule2)
nf_cols = [&#39;A&#39;, &#39;C&#39;, &#39;D&#39;]
for i in nf_cols:
for j in dat_rows:
ws[&#39;{}{}&#39;.format(i,j)].fill = PatternFill(patternType=&#39;solid&#39;, fgColor=&#39;FFFFFF&#39;)
ws[&#39;{}{}&#39;.format(i,j)].alignment = Alignment(horizontal=&#39;center&#39;, vertical=&#39;center&#39;, wrap_text=True)
thin_border = Border(left=Side(style=&#39;thin&#39;, color=&#39;FFFFFF&#39;), 
right=Side(style=&#39;thin&#39;, color=&#39;FFFFFF&#39;), 
top=Side(style=&#39;thin&#39;, color=&#39;FFFFFF&#39;), 
bottom=Side(style=&#39;thin&#39;, color=&#39;FFFFFF&#39;))
for i in columns:
for j in range(10):
ws[&#39;{}{}&#39;.format(i,j+1)].border = thin_border
ws[&#39;E2&#39;].border = Border(left=Side(style=&#39;thin&#39;, color=&#39;000000&#39;), 
right=Side(style=&#39;thin&#39;, color=&#39;000000&#39;), 
top=Side(style=&#39;thin&#39;, color=&#39;000000&#39;))
for i in [3, 4, 5, 6, 7, 8, 9, 10]:
ws[&#39;E{}&#39;.format(i)].border = Border(left=Side(style=&#39;thin&#39;, color=&#39;000000&#39;), 
right=Side(style=&#39;thin&#39;, color=&#39;000000&#39;))
ws[&#39;E10&#39;].border = Border(bottom=Side(style=&#39;thin&#39;, color=&#39;000000&#39;),
left=Side(style=&#39;thin&#39;, color=&#39;000000&#39;), 
right=Side(style=&#39;thin&#39;, color=&#39;000000&#39;))

And for the export:

#export tables as images
excel2img.export_img(&quot;excel_file_name.xlsx&quot;,&quot;image_1.png&quot;, &quot;&quot;,&quot;sheet_name!A1:I10&quot;)
excel2img.export_img(&quot;excel_file_name.xlsx&quot;,&quot;image_2.png&quot;, &quot;&quot;,&quot;sheet_name2!A1:I10&quot;)

I hope this helps.

huangapple
  • 本文由 发表于 2023年3月10日 01:07:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75687844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定