[英]From Python web app: insert data into spreadsheet (e.g. LibreOffice / Excel), calculate and save as pdf
I am facing the problem, that I would like to push data (one large dataframe and one image) from my python web app (running on Tornado Webserver and Ubuntu) into a spreadsheet, calculate, save as pdf and the deliver to the frontend.我面临的问题是,我想将 python 网络应用程序(在 Tornado Webserver 和 Ubuntu 上运行)中的数据(一个大数据框和一个图像)推送到电子表格中,计算,另存为 pdf 并交付到前端。
I took a look at several libs like openpyxl for writing Sheets in MS Excel, but that would solve just one part.我查看了几个像 openpyxl 这样的库,用于在 MS Excel 中编写表格,但这只能解决一个问题。 I was thinking about using LibreOffice and pyoo, but it seems that I need the same python version on my backend as shipped with LibeOffice when importing pyuno.我正在考虑使用 LibreOffice 和 pyoo,但在导入 pyuno 时,我的后端似乎需要与 LibeOffice 附带的相同的 python 版本。
Does somebody has solved a similar issue and have a recommendation how to solve this?有人已经解决了类似的问题,并建议如何解决这个问题?
Thanks谢谢
I came up to a let's say not pretty, but rare solution that works very flexible for me.我想出了一个让我们说不漂亮但罕见的解决方案,它对我来说非常灵活。
While executing the last call, all integrated formulas are recalculated/updated and the pdf is created ( you have to configure calc so that auto calc is enabled when files are opened )在执行最后一次调用时,重新计算/更新所有集成公式并创建 pdf(您必须配置 calc 以便在打开文件时启用自动计算)
import openpyxl
import pandas as pd
from subprocess import call
d = {'col1': [1, 2], 'col2': [3, 4]}
df = pd.DataFrame(data=d)
now = datetime.datetime.now().strftime("%Y%m%d_%H%M_%f")
wb_template_name = 'Template.xlsx'
wb_temp_name = now + wb_template_name
wb = openpyxl.load_workbook(wb_template_name)
ws = wb['dataframe_sheet']
pdf_convert_cmd = 'soffice --headless --convert-to pdf ' + wb_temp_name
for r in dataframe_to_rows(df, index=True, header=True):
ws.append(r)
wb.save(wb_temp_name)
call(pdf_convert_cmd, shell=True)
The reason why I'm doing this, is that I would like to be able to style the layout of the pdf independently from the data.我这样做的原因是我希望能够独立于数据来设置 pdf 的布局。 I use named ranges or lookups that are referenced to the separate dataframe-sheet in excel.我使用引用到 excel 中单独数据框表的命名范围或查找。
I didn't try the image insertion yet, but this should work similar.我还没有尝试插入图像,但这应该类似。 I think there could be a way to increase the performance while simply dump the dataframe into the xlsx file (which is a zipped file of xmls), so that you don't need openpyxl.我认为可能有一种方法可以提高性能,同时只需将数据帧转储到 xlsx 文件(它是 xmls 的压缩文件)中,这样您就不需要 openpyxl。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.