简体   繁体   English

如何在转换成pdf之前使unoconv自动计算公式?

[英]How to make unoconv auto calculate formulae before converting into pdf?

I created a .xlsx file using the python module Xlsxwriter. 我使用python模块Xlsxwriter创建了一个.xlsx文件。

Works well but any cells that contain formula will show up as empty or 0 when I run 效果很好,但任何包含公式的单元格在运行时都会显示为空或0

unoconv -f pdf spreadsheet.xlsx

How do I force the unoconv autocalculate the values before converting to pdf? 在转换为pdf之前,如何强制unoconv自动计算值?

As of this writing, apparently unoconv is purposely not doing any recalculation because it can cause errors. 在撰写本文时,显然unoconv故意不进行任何重新计算,因为它可能导致错误。 See Issue 97 . 问题97 The relevant part of unoconv's source code is commented out. unoconv源代码的相关部分已被注释掉。

So one thing you could try is to go into your copy of unoconv and uncomment those lines (make them active again). 所以你可以尝试的一件事是进入你的unoconv副本并取消注释这些行(让它们再次激活)。 You may get errors or you may be fine. 您可能会收到错误,或者您可能没事。

Another thing you could try, if your formulas are simple, is to calculate them using Python and write the results along with the formulas using XlsxWriter. 如果您的公式很简单,您可以尝试的另一件事是使用Python计算它们并使用XlsxWriter将结果与公式一起编写。 Notice that the worksheet.write_formula() method has an optional value parameter. 请注意, worksheet.write_formula()方法具有可选的value参数。 Quoting from the official docs: 引用官方文档:

XlsxWriter doesn't calculate the result of a formula and instead stores the value 0 as the formula result. XlsxWriter不计算公式的结果,而是将值0存储为公式结果。 It then sets a global flag in the XLSX file to say that all formulas and functions should be recalculated when the file is opened. 然后,它在XLSX文件中设置一个全局标志,表示在打开文件时应重新计算所有公式和函数。 This is the method recommended in the Excel documentation and in general it works fine with spreadsheet applications. 这是Excel文档中推荐的方法,通常它适用于电子表格应用程序。 However, applications that don't have a facility to calculate formulas, such as Excel Viewer, or some mobile applications will only display the 0 results. 但是,没有计算公式的工具的应用程序(如Excel Viewer)或某些移动应用程序将仅显示0结果。

If required, it is also possible to specify the calculated result of the formula using the optional value parameter. 如果需要,还可以使用可选的value参数指定公式的计算结果。 This is occasionally necessary when working with non-Excel applications that don't calculate the result of the formula: [...] 当使用不计算公式结果的非Excel应用程序时,偶尔需要这样做:[...]

Note that if the formulas are such that you can calculate them with Python, and you only care about the final PDF (no one is going to use the spreadsheet, and after you convert it to PDF you just throw it away anyway), then you could write the values directly, without the formulas at all. 请注意,如果公式是这样的,您可以使用Python计算它们,并且您只关心最终的PDF(没有人会使用电子表格,并且在您将其转换为PDF后,无论如何都要扔掉它),那么你可以直接写入值,而不需要公式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM