create a new odt-file with python - python

I have a folder "myfolder" which was created when I had unzipt an odt-file. I had delete the content.xml in the folder. Now I want to add a file called "content.xml" with data in it (here in the variable "content" is the xml-styled text). I tried this:
with zipfile.ZipFile('myfolder', mode='a', compression=zipfile.ZIP_DEFLATED) as zf:
zf.writestr('content.xml', content)
I get an odt-file but it is damaged. when I unzip it there is only the content.xml in it. the mode parameter is 'a' so I thougt it will append the content.xml to the other files.
Can anybody help?

You can try to use the package odfpy.
you can see some info here and here
Odfpy is a library to read and write OpenDocument v. 1.2 files. The
main focus has been to prevent the programmer from creating invalid
documents. It has checks that raise an exception if the programmer
adds an invalid element, adds an attribute unknown to the grammar,
forgets to add a required attribute or adds text to an element that
doesn’t allow it. It's on pipy, so you can install it with
you can install it with pip:
pip install odfpy

Related

Unknown problem in Python. Printing does not work and files are not saved correctly

When I try to write something, such as variables, the code is renamed to the file name on the computer.
For example, if I write:
a = 20
f = 15
print(a+f)
then the code file will automatically be renamed to the first line, i.e. "a = 20"
Then, when I try to run the code, the program outputs nothing but "Python" and some incomprehensible words.
What could it be related to?
enter image description here
enter image description here
I installed the latest version of Visual Stuio Code with Python, they are new, so there should be no problems. But this time it went wrong.
After reinstalling the program, the problem remains.
First of all, if there is no special requirement, please do not use Code Runner to run the script, using the official extension Python is a better choice.
In addition, the dot on your file label means that you have not saved the file, you can add the following setting to enable automatic saving in the settings.
"files.autoSave": "afterDelay",
You may have created the file using the following method. File --> New File... --> Python File. At this time, the file has not been named, also not saved. You can see that there is no such file in the resource manager list at this time.
So the file label shows the first line of codes. This is a feature of vscode, you can refer to this link. And because the file has not been saved, there will be problems executing the script.
You can rename the script file directly (F2), or vscode will remind you to name the file when saving. Another way to create a file is to right click and choose New File..., enter filename and end with .py extension.

Protobuf Decode Error when reading bytes from withing Bitbucket

I have a protobuf file saved as bytes on Windows.
I am reading it as follows:
tlog = tlog_schema_pb2.TLog()
with open("tests/unittests/data/tlog.proto", "rb") as f:
tlog.ParseFromString(f.read())
It all fine. But when I push my changes with git to Bitbucket, there I get an error:
google.protobuf.message.DecodeError: Error parsing message with type 'globusdigital.tlogprocessing.TLog'
I cannot understand what can it be. What can change when I push to Bitbucket?
It appears that the problem was in pre-commit.
I had trailing-whitespace check there, and it was destructing protobuf file.
To solve it, just add
files: '\.pyi?$'
at the end of .pre-commit-config.yaml (would be cool to exclude files, but did not understand how)

Configure New NLTK Path

I am working with nltk in python. I imported the package and downloaded the additional data just fine, but I want to be able to append a new directory to store nltk_data.
When I tried this fix found at this link (How to config nltk data directory from code?)
nltk.data.path.append("path")
I received this error:
AttributeError: 'str' object has no attribute 'append'
What am I doing wrong?
you can try checking what nltk.data.path stores.
most probably you assigned the path as string (may be in a notebook session)
nltk.data.path.append = "folder path" (this is incorrect way)
you should first clear your session or restart your session if you made above change.

Allure not able to read output.xml file generated by AllureReportLibrary in Robot Framework

I am using Ride (RobotFramework IDE) and I have imported Library AllureReportLibrary in my project.
Using the Set Output Dir, I am creating a Directory C:/AutomationLogs/Allure and all the allure properties and xml files are getting generated in that path.
Set Output Dir C:/AutomationLogs/
Then I am using the "allure serve C:\AutomationLogs\Allure" command to try and generate the html report file in command prompt, but it shows the below error -
"Could not read result
C:\AutomationLogs\Allure\f56f4796-d30a-47f3-a988-d17f6c4e13ca-testsuite.xml:
{} com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot
deserialize va lue of type
ru.yandex.qatools.allure.model.SeverityLevel from String "None":
value not one of declared Enum instance names: [trivial, blocker,
minor, normal, critical]"
The xml file "f56f4796-d30a-47f3-a988-d17f6c4e13ca-testsuite.xml" was generated using the AllureReportLibrary
Also the index.html file which is generated after the command opens after this command and shows Allure Report unknown
unknown - unknown (Unknown) 0 test cases NaN%
I am using the below -
Allure version - 2.4.1
Ride version - RIDE 1.5.2.1 running on Python 2.7.12.
I am new to Robot Framework and Allure. Please let me know whether I have implemented it correctly and why I am facing the above error.
-Ryan M
I'm using the 1.1.1 version of Allure Adaptor for Robot Framework and the severity is picked from the test case tags and added as a label under the test-case element of the report.
However, it seems that Allure 2.6.0 is also expecting a valid value for the severity attribute of the test-case element.
In order to use Allure2 with the current reports I have altered AllureListener.py to also add the severity to the test case:
elif tag in SEVERITIES:
test.severity = tag
test.labels.append(TestLabel(
name='severity',
value=tag
))
If your output.xml has severity = None for any testcase then the allure-robotframework-adaptor will give the error that you have mentioned. Creating TestCase() object with severity='' in start_suitesetup method of AllureListener.py will do the trick.
def start_suitesetup(self, name, attributes):
....
....
test = TestCase(name=name,
description=description,
start=now(),
attachments=[],
labels=[],
parameters=[],
steps=[],
severity='')
How to create the Allure reports in Robot Framework ?
Initially, Download the Command line and UNzip the file and save the path of the bin folder in environment.
Link : http://repo.maven.apache.org/maven2/io/qameta/allure/allure-commandline/2.8.0/allure-commandline-2.8.0.zip
Unzip the above file then put it in the Environment folder.
Then Pip install the below modules
pip install allure-robotframework
pip install robotframework-allurereport
In robot file, Add the Library in Settings like,
Example :
Library AllureReportLibrary D:\eclipse\RobotFramework\results
Then Use the Below commands to run the robot code.
robot --listener allure_robotframework;D:\eclipse\RobotFramework\results
Example.txt
Finally,
Generate the HTML file by,
allure generate D:\eclipse\RobotFramework\results
Note : Use the same path what you used in the previous command to generate the HTml.file.
and
Open in Mozhila FireFox. It wont be work in Chrome. I dont know exactly why.
Regards,
Vijay

Openpyxl gives an inconsistent error message

I have the following function that will read from an excel workbook with the openpyxl library:
import openpyxl
def read_excel(path):
excel_workbook = openpyxl.load_workbook(path, read_only = True)
# other logic
return None
I can call that function like this:
read_excel("C:/Users/anon/Desktop/Current Projects/Test Files/Test.xlsm ")
And it returns this error:
openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support .xlsm file
format, please check you can open it with Excel first. Supported formats are: .xlsx,.xlsm,
.xltx,.xltm
That error message confuses me. It's telling me that it doesn't support the .xlsm file format, and that it supports the .xlsm file format. The file opens just fine in excel, why won't openpyxl read my Excel file?
There is an extra whitespace character in the error message after .xlsm. Remove the whitespace character at the end of the path string you call the function with, and the function runs without error.
read_excel("C:/Users/anon/Desktop/Current Projects/Test Files/Test.xlsm")
the same problem bothered me a lot also today, and finally I updated openpyxl from 2.3.2 to 2.3.5, and this problem disappeared.
Although I am using Anaconda, sometimes using pip to update the packages might be a good try.
I'm using PyQt5 and had the same problem. I found that adding _filter fixed the problem. The full line reads:
fileName, _filter = QtWidgets.QFileDialog.getOpenFileName(None, "Lists", "", "xlsx files *.xlsx")
First, change the cwd(). When passing the file name, you can just copy the name of the file and paste it instead of typing it manually. The error may arise from some undetected nuances.

Categories